Skip to content

Instantly share code, notes, and snippets.

@mads-hartmann
Created May 6, 2012 18:37
Show Gist options
  • Save mads-hartmann/2623720 to your computer and use it in GitHub Desktop.
Save mads-hartmann/2623720 to your computer and use it in GitHub Desktop.
Performance wtf
concurrentMap3 :: Int -> (a -> b) -> [a] -> IO [b]
concurrentMap3 k f xs = do
mvars <- sequence $ replicate k newEmptyMVar
_ <- sequence $ map spark (ys `zip` mvars)
results <- sequence $ map takeMVar mvars
return $ concat $ results
where len = fromIntegral $ length $ xs -- Hope you're happy now GHC.
q = ceiling $ (len / (fromIntegral k))
ys = every q xs
spark = \tup -> forkIO $ do
val <- sequence $ map (\x -> evaluate $ f x) (fst tup) -- Forcing strict evaluation!
putMVar (snd tup) val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment