diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-26 20:50:17 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-26 20:50:17 +0000 |
commit | f2f54ac1d76fc391c0e231b4309ae5c4245cd0f9 (patch) | |
tree | 43ffdbe03d68384d77cfb3a7637d8912c1317d9b /lib/Data/Packed | |
parent | 611ff4782c261a6c6e52fe7ed0c122a0eac22691 (diff) |
improve monadic maps performance (go functions)
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index a47c376..7360f93 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -390,64 +390,64 @@ foldVectorG f s0 v = foldLoop g s0 (dim v) | |||
390 | mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b) | 390 | mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b) |
391 | mapVectorM f v = do | 391 | mapVectorM f v = do |
392 | w <- return $! unsafePerformIO $! createVector (dim v) | 392 | w <- return $! unsafePerformIO $! createVector (dim v) |
393 | mapVectorM' f v w 0 (dim v -1) | 393 | mapVectorM' w 0 (dim v -1) |
394 | return w | 394 | return w |
395 | where mapVectorM' f' v' w' !k !t | 395 | where mapVectorM' w' !k !t |
396 | | k == t = do | 396 | | k == t = do |
397 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 397 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
398 | y <- f' x | 398 | y <- f x |
399 | return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y | 399 | return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y |
400 | | otherwise = do | 400 | | otherwise = do |
401 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 401 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
402 | y <- f' x | 402 | y <- f x |
403 | _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y | 403 | _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y |
404 | mapVectorM' f' v' w' (k+1) t | 404 | mapVectorM' w' (k+1) t |
405 | {-# INLINE mapVectorM #-} | 405 | {-# INLINE mapVectorM #-} |
406 | 406 | ||
407 | -- | monadic map over Vectors | 407 | -- | monadic map over Vectors |
408 | mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m () | 408 | mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m () |
409 | mapVectorM_ f v = do | 409 | mapVectorM_ f v = do |
410 | mapVectorM' f v 0 (dim v -1) | 410 | mapVectorM' 0 (dim v -1) |
411 | where mapVectorM' f' v' !k !t | 411 | where mapVectorM' !k !t |
412 | | k == t = do | 412 | | k == t = do |
413 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 413 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
414 | f' x | 414 | f x |
415 | | otherwise = do | 415 | | otherwise = do |
416 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 416 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
417 | _ <- f' x | 417 | _ <- f x |
418 | mapVectorM' f' v' (k+1) t | 418 | mapVectorM' (k+1) t |
419 | {-# INLINE mapVectorM_ #-} | 419 | {-# INLINE mapVectorM_ #-} |
420 | 420 | ||
421 | -- | monadic map over Vectors with the zero-indexed index passed to the mapping function | 421 | -- | monadic map over Vectors with the zero-indexed index passed to the mapping function |
422 | mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b) | 422 | mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b) |
423 | mapVectorWithIndexM f v = do | 423 | mapVectorWithIndexM f v = do |
424 | w <- return $! unsafePerformIO $! createVector (dim v) | 424 | w <- return $! unsafePerformIO $! createVector (dim v) |
425 | mapVectorM' f v w 0 (dim v -1) | 425 | mapVectorM' w 0 (dim v -1) |
426 | return w | 426 | return w |
427 | where mapVectorM' f' v' w' !k !t | 427 | where mapVectorM' w' !k !t |
428 | | k == t = do | 428 | | k == t = do |
429 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 429 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
430 | y <- f' k x | 430 | y <- f k x |
431 | return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y | 431 | return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y |
432 | | otherwise = do | 432 | | otherwise = do |
433 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 433 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
434 | y <- f' k x | 434 | y <- f k x |
435 | _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y | 435 | _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y |
436 | mapVectorM' f' v' w' (k+1) t | 436 | mapVectorM' w' (k+1) t |
437 | {-# INLINE mapVectorWithIndexM #-} | 437 | {-# INLINE mapVectorWithIndexM #-} |
438 | 438 | ||
439 | -- | monadic map over Vectors with the zero-indexed index passed to the mapping function | 439 | -- | monadic map over Vectors with the zero-indexed index passed to the mapping function |
440 | mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m () | 440 | mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m () |
441 | mapVectorWithIndexM_ f v = do | 441 | mapVectorWithIndexM_ f v = do |
442 | mapVectorM' f v 0 (dim v -1) | 442 | mapVectorM' 0 (dim v -1) |
443 | where mapVectorM' f' v' !k !t | 443 | where mapVectorM' !k !t |
444 | | k == t = do | 444 | | k == t = do |
445 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 445 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
446 | f' k x | 446 | f k x |
447 | | otherwise = do | 447 | | otherwise = do |
448 | x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k | 448 | x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k |
449 | _ <- f' k x | 449 | _ <- f k x |
450 | mapVectorM' f' v' (k+1) t | 450 | mapVectorM' (k+1) t |
451 | {-# INLINE mapVectorWithIndexM_ #-} | 451 | {-# INLINE mapVectorWithIndexM_ #-} |
452 | 452 | ||
453 | ------------------------------------------------------------------- | 453 | ------------------------------------------------------------------- |