Yet another developer blog.

Evas Cserve2 - Cache Server

Posted on 06-07-2012 in

Evas, the canvas library behind EFL, is already fast and lightweight. But a new feature was added to it recently, making it even faster: a cache server, able to cache images and font glyphs between different applications.

The idea is simple: whenever an application needs to show an image on the screen, a quite common use case for any graphical application, it must first load this image from the filesystem, decode it on memory, and just then it has the pixels available for making any other transformation and showing them. Now, instead of loading these pixels from the filesystem directly, applications can make a request to CServe2, that will then load the image from the disk and return the loaded pixels in a shared memory.

One of the advantages of having such server is obvious: if the same image is used by more than one application, it will be loaded only once, and will use that memory only once. And loading the same image is a common use case when you have several applications using the same widget toolkit, with the same decorations and so on.

What happens then is that the overall system memory footprint will be reduced (less copies of the image in memory). Additionally, if an application has all its images cached already before starting, because any other application has lodead them already or because its images are still cached since the last run, it can start faster. No image loading time will be necessary.

A similar idea is applied to fonts and glyphs. Glyphs are, roughly speaking, visual representations of text characters. In one of the last stages of rendering text on the screen, the glyphs, which usually still have a vectorial representation, are rendered and become images. At this stage we are caching these images on the server too, keeping them ready for client applications when they need them.

The image cache server was already done before, called CServe, and that’s the reason why this one is called CServe2. Besides caching images between applications, it also caches font glyph bitmaps (only that for now), and has some other nice features: it exports an asynchronous API, which can be used by clients to request the loading of images as soon as they know that these images may be used. The server will then start preloading these images speculatively, and when the application really needs to show them on the screen, they will be already loaded.

The asynchronous API is actually part of a bigger plan, in which the Evas rendering pipeline will also be converted to a more asynchronous one, and then make better use of the cache server, but the current gains are already interesting. For applications that do not require big fonts or images being rendered, it doesn’t make a difference, but for an image viewer, like Ephoto, it already has some nice gains on its start time if loading a big image.

Here is a simple test done using Elementary’s photocam widget, which loads a picture and, once it is loaded, show it on the screen. The time from the widget being created until the image being loaded for the first time was measured, and the results are shown below. The tests were done with no CServe2 being used, then when it was used but had no previous cache of the said image, and finally when the image was already cached on the server:

Evas CServe2 Comparison

The results speak for themselves. While there’s a little extra overhead added by the server when loading the picture for the first time, due to many factors which can also be minimized later, it proves that the resulting benefits of using the server outweigh by far this extra overhead.

All this stuff is already available at E’s SVN and enabled by default, although the server needs to be run explicitely. If you want to do that, just start the installed binary (should be under /usr/local/libexec/evas_cserve2 on a default build from sources), and export an environment variable:

export EVAS_CSERVE2=1

With that setup, just run any efl application and it should use the cache server (if running the sofware x11 backend) by default.

Try it out. Any comments are appreciated.