Significantly improve basicSet (based on a patch by Louis Wasserman)
authorRoman Leshchinskiy <rl@cse.unsw.edu.au>
Fri, 19 Aug 2011 22:36:44 +0000 (22:36 +0000)
committerRoman Leshchinskiy <rl@cse.unsw.edu.au>
Fri, 19 Aug 2011 22:36:44 +0000 (22:36 +0000)
Data/Vector/Generic/Mutable.hs

index 57eb2cb..fd15071 100644 (file)
@@ -141,14 +141,19 @@ class MVector v a where
   basicClear _ = return ()
 
   {-# INLINE basicSet #-}
-  basicSet !v x = do_set 0
+  basicSet !v x
+    | n == 0    = return ()
+    | otherwise = do
+                    basicUnsafeWrite v 0 x
+                    do_set 1
     where
       !n = basicLength v
 
-      do_set i | i < n = do
-                           basicUnsafeWrite v i x
-                           do_set (i+1)
-                | otherwise = return ()
+      do_set i | 2*i < n = do basicUnsafeCopy (basicUnsafeSlice i i v)
+                                              (basicUnsafeSlice 0 i v)
+                              do_set (2*i)
+               | otherwise = basicUnsafeCopy (basicUnsafeSlice i (n-i) v)
+                                             (basicUnsafeSlice 0 (n-i) v)
 
   {-# INLINE basicUnsafeCopy #-}
   basicUnsafeCopy !dst !src = do_copy 0