Fwd: [Haskell-cafe] Optimization with Strings ?

Alberto G. Corona agocorona at gmail.com
Thu Dec 3 14:13:34 EST 2009

```Use makeStableName from System.Mem.StableName

StableName`s are just for checking pointer equality. Instead of checking for
equality of the strings, check for pointer equality of their stableNames

a dirty way:
pointerEq x y= unsafePerformIO \$ do
px <- makeStableName x
py <- makeStableName  y
return x == y

pEq  x  y |  pointerEq x y == True = True
| otherwise = x == y

2009/12/3 David Virebayre
>

On Thu, Dec 3, 2009 at 1:03 PM, Emmanuel CHANTREAU
>
> > In my futur program, it use a lot of binary trees with strings (words)
> > as leaf. There is just arround 1000 words and they will appear a lot of
> > times. The program will possibly consume a lot of process and memory
> > (it is a mathematics proover).
>
> > I began this program in C++ but haskell has a prety good speed and
> > memory footprint and is easier. But I don't know if it worth to do this
> > optimization: having a dictionary to translate string words in Int.
>
> > The answer depends on the automatic optimizations in GHC, because GHC
> > could compare quickely two strings if it is the same object, so it
> > depends if program generated by GHC have a dictionary (tree) of strings
> > internaly. Someone knows this ?
>
> It doesn't work this way : Strings are just lists of Chars. Comparison
> is made recursively, Char by Char. You can have a look at the source
> to make sure :
>
> instance (Eq a) => Eq [a] where
>    []     == []     = True
>    (x:xs) == (y:ys) = x == y && xs == ys
>    _xs    == _ys    = False
>
> So you will have to code your own optimisation.
>
> David.
>
> P.S. In French if you didn't understand:
>
> Ca ne marche pas comme ça.
> Les chaines de caractères ne sont que des listes de caractères.
> La comparaison sur les listes est faite récursivement, caractère par
> caractère, il suffit pour s'en assurer de regarder au source :
> Donc il vaut mieux que tu implémente ton propre dictionnaire.
> _______________________________________________