[Haskell-cafe] Space questions about intern and sets

Gracjan Polak gracjan at acchsh.com
Wed Jun 8 10:40:41 EDT 2005


Bjorn Bringert wrote:
>> memory. Here is something I wrote, but it doesn't work :(
>

I must have been doing something really wrong that day, because today it 
works smoothly... :)

> 
> 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
>

Very interesting, thanks!

> It seems very similiar to your code, except that it uses HashTable 
> instead of Map.

Question is: which one is better? My tupicall file contains 160000 
tokens, where 95% is taken by about 20 tokens that are used very frequently.

> 
> /Björn

-- 
Gracjan


More information about the Haskell-Cafe mailing list