[Haskell-cafe] looking for optimization advice

Carl Witty cwitty at newtonlabs.com
Thu Mar 25 11:39:26 EST 2004

On Thu, 2004-03-25 at 10:09, David Roundy wrote:
> The function is a simple packed string compare, and I basically just call
> the C standard library function memcmp for this.  Without further ado:

I don't know how to make your particular function faster, but I do have
some ideas for different approaches you may not have thought of.

If you compare strings more often than you create strings, and the
comparisons usually fail, you could add a hash of the string to your
PackedString datatype; then psniceq could compare hash values first and
only confirm equality with the memcmp if the hash values match.

You could use hash consing/interning; keep a global hash table which
maps between strings and some unique identifier (your unique identifier
might be an Int, or an IORef).  Then you can compare the strings with a
single comparison.  (This table could be a memory leak in a long-running
application; you could probably avoid that with weak pointers.)

Carl Witty

More information about the Haskell-Cafe mailing list