Skip to content

Commit ccf2260

Browse files
recursion-ninjacartazio
authored andcommitted
Improving efficiency & compatibility of traverse implementation for boxed 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.
1 parent 1d208ee commit ccf2260

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Data/Vector.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,12 @@ instance Foldable.Foldable Vector where
425425

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

430435
{-# INLINE mapM #-}
431436
mapM = mapM

0 commit comments

Comments
 (0)