21 Jul 2003 MichaelCrawford   » (Master)

Please Comment on My Upcoming Kuro5hin Article

I need your help in improving an article that I plan to submit for publication to Kuro5hin in a few days. I want the article to get front page, and the K5 moderators are notoriously picky, so it's important I do the best job I can. Tell me how I can improve:

Send comments to crawford@goingware.com or (if you're a K5 member) post them in my Kuro5hin diary.

The article is more than it seems from the title. While it certainly does tell you where to get more legal music downloads than you could ever possibly hope to listen to, it also aims to raise the consciousness of the average p2p user by exploring some of the legal issues behind copyright, and outlining steps the reader can take to effect political change.

Remember what I said yesterday: Life just wouldn't be the same without some injustice I could rail against.

Updated: Caching

mathieu, I'm sorry that I can't give you the code, as I don't own it, but I once wrote a very effective caching library that was a C++ template.

It was based on a circular buffer, that could store any kind of data, being a template. A wrapper around that presented the appearance of an array, like the STL vector.

If a desired bit of data was already in the circular buffer, it would be immediately returned, but if it wasn't it would be fetched from a database (hence the need for a cache), copied into the buffer and returned.

The circular buffer always contained a contiguous range of database elements, because the access pattern of our application was to move rapidly around in small windows, but occasionally jump somewhere far away. If an element was inserted into the circular buffer that was farther away from the existing elements than the size of the buffer, then the circular buffer pointers would be adjusted to empty it. But if an element was inserted just a little ways away from cached data, then I'd pre-fetch all the elements in between.

It would not be hard to use a method like this to allow different policies that would be suitable for different access patterns, like Least Recently Used. You could still use the two-layer approach like I did where the storage plugs into the access mechanism, and you have a choice of algorithms to suit different access patterns.

If you parameterize your template based on the type of the array index, you can use it like a dictionary:

std::string name;
job_title = job_titles[ name ];

And yes, it took quite a lot of work to get the thing working right. You will want to use a profiler. I did this on Windows with Visual C++, so I used Intel VTune, which made a tremendous difference.

It is an interesting exercise to try to use a profiler to optimize a C++ template across different datatypes, for example the use of both strings and integers as array indices. That's one reason C++ has template specializations.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!