ervin's piece of web

White noise from a gearhead
  • rss
  • Home
  • About
  • Some Documents
  • Curriculum Vitae

libkimap – the genesis

ervin | October 22, 2009 | 09:31

I previously blogged about the new IMAP resource in Akonadi. We were aiminag at getting this resource from the start, but here is the not so secret story: this effort gave birth to another component, namely libkimap.

The journey started because we really wanted for KDE to have a strong and efficient IMAP support. Since we’re not really into reinventing the wheel if we can avoid that the journey started by looking at existing IMAP solutions we could reuse. The natural first contender of course was to reuse our old IMAP ioslave which we currently use in KMail and build the resource on it. But really it is showing its age now, and the interface such ioslaves expose are too limited for our needs (for those in the known of KIO internals: it would require an extensive use of the special() method, or command encoding in urls…). So we have been evaluating a few other contenders coming from various mail clients, but unfortunately most of them are either exposing synchronous API (not convenient for something event driven like an Akonadi resource), or very tied to a MIME implementation (which was a no-no as we needed to use the resource with kdepim’s MIME implementation).

Indeed this journey didn’t start quite well… We had to produce a new IMAP implementation. So a new quest started, one for an adequate API design. At that point we knew we wanted something: fast, memory efficient, asynchronous and easy to extend (as the IMAP RFC has plenty of extensions).

First, let’s examinate the “fast and memory efficient” constraints. Around the same time I got started on libkimap, Andras Mantia (fellow KDABian and hacker extraordinaire) was working on making the Akonadi server protocol handling faster and more memory efficient… And as a bonus, the Akonadi protocol for the lowest level parts, have quite some similarities with IMAP as it was modelled after it. So far so good, I could reuse Andras work on the new IMAP stream parser. It has been a two way collaboration as I also found bugs in there which got fixed. This parser is now the core of libkimap, although it is hidden from the public API, it is for a great part responsible of the good performances of the library.

Then, let’s solve the “asynchronous and easy to extend” constraints, those had a direct impact on the API. For that we rely on the good old “job based” API. You just need to create a Session object which holds a connection to an IMAP server and queue Jobs on it. Jobs are then executed sequentially. We have jobs for a lot of things: mailbox listing, fetching messages or headers, retrieving annotations, quota information, ACLs, etc. And that’s where the job based API gets interesting, we need to extend it? Just add a job, binary compatibility will be kept and so on, it makes it much easier to manage it over time.

Oh! And of course, since we still care about performances, each job is equipped to be able to pipeline several IMAP commands to the server, which dramatically reduces latency. So you guessed it, jobs don’t map 1:1 to IMAP commands, this way we can provide some convenience to developers because they get pre-shipped micro behavior (for instance, listing mailboxes and taking care about the namespace extension results always in the same list of commands, so we wired it in jobs which pipeline commands).

So we indeed solved our initial problem, we made an asynchronous IMAP library which is fast, efficient and easy to extend. It also tries to be clever when that actually makes it more convenient for developers (see the pipelining example above), but not too much, allowing to have a fine control on the higher level logic and strategies you’d need to implement in your application. And thanks to this strong basement, we could tackle the task of building the Akonadi IMAP resource on top of it.

As a post scriptum: is our library tied to a MIME implementation? Well, like others we’re tied to one: libkmime, available in kdepimlibs as well. You can’t really get around that in the end. Indeed, for fetch operations if you try to be independent of any MIME implementation, you end up implementing your own subset of MIME. That said we tried hard, and managed to keep that coupling as minimal as possible, and in fact only the FetchJob really depends on libkmime, everything else is independent of it. So, it wouldn’t be a big cost to implement your own FetchJob variant using another MIME implementation.

And finally, as a post post scriptum, if people out there wants to grab it and play with it, it is in our kdepimlibs module on trunk. You can browse it online here: http://websvn.kde.org/trunk/KDE/kdepimlibs/kimap/.

Categories
KDE
Tags
Akonadi, IMAP, KDE
Comments rss
Comments rss
Trackback
Trackback

« KMail as components, Akonadi Mail Reader The rise of the KDE/Maemo effort »

3 Responses to “libkimap – the genesis”

  1. Jan Kundrát says:
    October 22, 2009 at 10:53

    Hi, how do you measure performance of the parser and what results have you achieved? I just tried to benchmark my parser [1] and according to QTest, I’m getting 0.33ms on average per loop iteration (it parsers three replies per one iteration, yielding roughly 110 microseconds per response); this is on a few years old Thinkpad T60 (2007-FRG).

    [1] http://gitorious.org/trojita/trojita/commit/e6afc47d3ec51051adaa748c73528bbd90ccdc2a

  2. ervin says:
    October 22, 2009 at 11:03

    That’s probably a point unclear from the post indeed. I didn’t scientifically benchmark it yet (still need to find time to do it). That said, with a dumb UI slapped on top of the Akonadi resource which uses it, the difference of performance is stellar compared to KMail. Just to give an example, where it took several seconds for listing folders and getting metadata it’s now instance.

    I know it’s probably not a satisfying answer for you, it’s definitely not a satisfying one for me, but such benchmarks take time to be done well.

    Since there’s apparently interest in such benchmarking though I’ll keep a mental note on publishing them here once I get around having them.

  3. Jan Kundrát says:
    October 22, 2009 at 11:16

    Right, getting the UI part right is far from trivial (and my quick hack doesn’t do that, it’s just a low-level microbenchmark). It will be challenging to measure how long does it take for all items to pop up in the view, at least in my app.

Leave a Reply

Click here to cancel reply.

Tags

Akademy Akonadi Conference Idealism IDLE IMAP KDE Keynote LaTeX Maemo Mail Multi-Agent Ontologies Party PhD Pictures Plasma Qt Solid Tokamak Tokamak4

Categories

  • Akademy (55)
  • Conferences & Summits (1)
  • Conferences & Summits (20)
  • General (19)
  • KDE (27)

Related

  • IPSquad
  • KDAB
  • KDE
  • Planet KDE
  • Toulibre

Archives

  • March 2010
  • February 2010
  • January 2010
  • October 2009
  • August 2009
  • July 2009
  • February 2009
  • August 2008
  • June 2008
  • April 2008
  • March 2008
  • January 2008
  • December 2007
  • October 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • February 2006
  • January 2006
  • October 2005
  • September 2005
  • August 2005
  • August 2004
  • July 2004
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress