Optimizing Hamlet (a minor correction)

June 7, 2010

GravatarBy Michael Snoyman

Note: this is in relation to my previous blog post, which showed a steep speed advantage for Hamlet versus Blaze on the BigTable benchmark.

Jasper pointed out that the benchmark I was using introduced unnecessary overhead by converting the final lazy bytestring to a String, when simply taking the length would have been enough strictness. I re-ran the benchmark that way, which gave the advantage back to Blaze over Hamlet 0.3.

I decided to run one more benchmark: the CGI benchmark. In this one, Blaze also has an advantage over Hamlet, this time of about 10%. I don't have time at the moment to do an in-depth investigation of this, but here is my theory on this strange behavior:

Hamlet currently performs no bytestring concatenation. As a result, it produces a lot of small bytestrings. These are very ammenable to being converted to Strings and being concatenated together. However, it will take longer to sum up the lengths of all the bytestrings, since length of a strict bytestring is O(1). Blaze, on the other hand, does concatenate bytestrings together, therefore giving it the exact opposite characteristics.

In practice, clearly the behavior of Blaze will be preferable. Given that Jasper is doing such hard work on optimizing Blaze, it's unlikely that Hamlet will ever get those specific attributes of its backend to be as fast as Blaze is. On the other hand, Hamlet still seems to have an edge over Blaze by being able to perform compile-time character encoding, and at least in theory concatenating large blocks of static HTML into a single string. The BigTable benchmark, however, utilized neither of these features.

Jasper had mentioned to me the possibility of using Blaze as a backend for Hamlet before, but I thought it didn't make much sense, given the heavy compile-time nature of Hamlet. However, the benchmarks are beginning to prove me wrong. Most likely, the 0.3 release of Hamlet will not be based on Blaze (especially since it's not yet released), but if the numbers indicate gains by using Blaze as a backend, most likely a later release of Hamlet will do so.

Thanks to Jasper for the great work!


comments powered by Disqus