Skip to content

Commit

Permalink
Improving efficiency & compatibility of traverse implementation for b…
Browse files Browse the repository at this point in the history
…oxed vectors. (gh pr #221, fixes bug #220)

previously traverse used unknown size fromList rather fromListN for constructing Boxed vectors.
In the presence of compact regions the implementation strategy for fromList results in program crashes. Now traverse on Boxed vectors uses the input vector size for constructing the result vector.
  • Loading branch information
recursion-ninja authored and cartazio committed Oct 9, 2018
1 parent 2de44a4 commit 12c608d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Data/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,12 @@ instance Foldable.Foldable Vector where

instance Traversable.Traversable Vector where
{-# INLINE traverse #-}
traverse f xs = Data.Vector.fromList Applicative.<$> Traversable.traverse f (toList xs)
traverse f xs =
-- Get the length of the vector in /O(1)/ time
let !n = G.length xs
-- Use fromListN to be more efficient in construction of resulting vector
-- Also behaves better with compact regions, preventing runtime exceptions
in Data.Vector.fromListN n Applicative.<$> Traversable.traverse f (toList xs)

{-# INLINE mapM #-}
mapM = mapM
Expand Down

0 comments on commit 12c608d

Please sign in to comment.