[Haskell-cafe] Space questions about intern and sets
Gracjan Polak
gracjan at acchsh.com
Tue May 31 08:14:32 EDT 2005
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 :(
intern :: Ord a => a -> a
intern x = unsafePerformIO $ internIO x
iorefset :: Ord a => IORef(Map.Map a a)
iorefset = unsafePerformIO $ do
newIORef $ Map.empty
{-# NOINLINE iorefset #-}
internIO :: Ord a => a -> IO a
internIO x = do
myset <- readIORef iorefset
case Map.lookup x myset of
Just y -> do
return y
Nothing -> do
let newset = Map.insert x x myset
writeIORef iorefset newset
return $ fromJust $ Map.lookup x newset
--return x
iorefset is re-executed, but it shouldn't.
2. Second question: imagine a Data.Set.Set and Data.Set.map with some
function f, that is mostly identity, only some, rare elements are
changed. As I understand it, map essentially copies whole structure of
set and this leads to out-of-memory conditions for me.
So, basically I have space problems. Is there any FAQ out there "how to
make my program fit in 256MB of heap?"
--
Gracjan
More information about the Haskell-Cafe
mailing list