Image Concatenation and best fit algorithms EQUALS web server performance
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
I was reading about Image Concatenation in CSS today. The concept allows for all the individual images on a website to be displayed from a single larger image. The extremely useful benefit of this is less image HTTP requests. This technique is only applicable to sites that already have some form of static image delivery but need further performance.
The performance gains are due to the fact that every HTTP request has an overhead, even if you are using a lightweight HTTP server rather than Apache (Apache is not the fastest for this kind of task.) then there is still the cost of serving each incoming connection. These include socket connection costs, file finding (to retrieve the image, from cache or disk.) and the not-insignificant TCP/IP overhead. Although most browsers limit the number of requests they perform simultaneously it still puts an extra load on a web server.
There is one problem with all this, you need to get all your individual images (gif,jpg,png) onto one single larger image. When I read the original article it occurred to me that I already had a solution from a previous project I had written. This surprisingly comes from the realms of games. It is a common concept within games development to take a large number of smaller images and putting them into square shaped memory areas (sometimes on memory boundaries.) To this end I wrote a system to read a range of images and perform a ‘best-fit’ into a x by y area.
Below I have included an example of taking a set of fonts and applying them into a defined space. The space has been reduced from left to right (300×300, 250×250 and lastly 220×220).
|
|
The images above show the workings of the system.
|
|
And these are the results.
So what I am now thinking is I need a way to make this happen all automatically, to extract all images from a particular page of a site and then munge them all together and to then alter the CSS to take account of it. (to be continued….)
apache cache concatenation performance gains Programming
Comment by Asd on 27 April 2007:
I am sure there are lots of best fit texture packing algorithms out there in game development land.
Comment by Jonathan O'Connor, Ireland on 27 April 2007:
Nick,
25 years ago when windows were all made of glass, I had to write a solution for a glass cutting company (actually it was a clever exam question). They had a large piece of glass, and they needed various smaller pieces of glass for customer’s windows. I guess the computer science name for it is a 2-dimensional knapsack problem.
One doesn’t often get such nice problems.
Comment by Ricky on 27 April 2007:
You wouldn’t actually want all of your images in one file, just all of your like-sized images. Wasted extra blank space is just introducing extra overhead (and reducing overhead is the whole point). If you made few different images to group images of similar size, you’ll probably yield the best results. It also has the upside of being cleaner to maintain and keep track of.
Comment by Nick on 27 April 2007:
I agree that there is no need to have all the images, most importantly you would not have any images that are dynamically selected.
But as for wasted space, I think my demo in the article shows that very little space is wasted by putting all the images onto one area. And the wasted space does not affect the size as runs of colours (i.e. blank spaces) compress down to near nothing. And grouping of similar sized images would then put restrictions on the system and require more human input the thus making the whole thing too much hassle to deal with.
Comment by Sriram C on 31 March 2008:
Is this algorithm available ?