From 12c608d701cccd07be10055d15886a01c2d3e798 Mon Sep 17 00:00:00 2001 From: recursion-ninja Date: Tue, 9 Oct 2018 13:34:01 -0400 Subject: [PATCH] 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. --- Data/Vector.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Data/Vector.hs b/Data/Vector.hs index ad0e8bd3..1c586490 100644 --- a/Data/Vector.hs +++ b/Data/Vector.hs @@ -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