Gracjan Polak wrote:
Hello all,
I've got two questions, both are space related so I'll put them in one
e-mail.
1. I'd like to have function intern, that behaves essentially like id,
but with the following constraint:
if x==y then makeStableName(intern x)==makeStableName(intern y)
Reasoning behind this hack: objects equal (as in Eq) should occupy the
same place in memory. I've got to parse quite large file, most tokens
are the same. Haskell speed is very good, but I constantly run out of
memory. Here is something I wrote, but it doesn't work :(
The code below seems to work for strings, and should be generalizable to
any type for which you have a hash function:
import Data.HashTable as H
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE stringPool #-}
stringPool :: HashTable String String
stringPool = unsafePerformIO $ new (==) hashString
{-# NOINLINE shareString #-}
shareString :: String -> String
shareString s = unsafePerformIO $ do
mv <- H.lookup stringPool s
case mv of
Just s' -> return s'
Nothing -> do
H.insert stringPool s s
return s
It seems very similiar to your code, except that it uses HashTable
instead of Map.
/Björn
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe