@@ -232,15 +232,20 @@ infixl 9 !
232
232
-- | O(1) Indexing.
233
233
(!) :: (HasCallStack , Vector v a ) => v a -> Int -> a
234
234
{-# INLINE_FUSED (!) #-}
235
- (!) v i = checkIndex Bounds i (length v) $ unBox (basicUnsafeIndexM v i)
235
+ (!) v ! i = checkIndex Bounds i (length v) $ unBox (basicUnsafeIndexM v i)
236
+ -- Why do we need ! before i?
237
+ -- The reason is that 'basicUnsafeIndexM' is a class member and, unless (!) was
238
+ -- already specialised to a specific v, GHC has no clue that i is most certainly
239
+ -- to be used eagerly. Bang before i hints this vital for optimizer information.
236
240
237
241
infixl 9 !?
238
242
-- | O(1) Safe indexing.
239
243
(!?) :: Vector v a => v a -> Int -> Maybe a
240
244
{-# INLINE_FUSED (!?) #-}
241
245
-- Use basicUnsafeIndexM @Box to perform the indexing eagerly.
242
- v !? i | i `inRange` length v = case basicUnsafeIndexM v i of Box a -> Just a
243
- | otherwise = Nothing
246
+ v !? (! i)
247
+ | i `inRange` length v = case basicUnsafeIndexM v i of Box a -> Just a
248
+ | otherwise = Nothing
244
249
245
250
246
251
-- | /O(1)/ First element.
@@ -256,7 +261,7 @@ last v = v ! (length v - 1)
256
261
-- | /O(1)/ Unsafe indexing without bounds checking.
257
262
unsafeIndex :: Vector v a => v a -> Int -> a
258
263
{-# INLINE_FUSED unsafeIndex #-}
259
- unsafeIndex v i = checkIndex Unsafe i (length v) $ unBox (basicUnsafeIndexM v i)
264
+ unsafeIndex v ! i = checkIndex Unsafe i (length v) $ unBox (basicUnsafeIndexM v i)
260
265
261
266
-- | /O(1)/ First element, without checking if the vector is empty.
262
267
unsafeHead :: Vector v a => v a -> a
@@ -316,7 +321,7 @@ unsafeLast v = unsafeIndex v (length v - 1)
316
321
-- element) is evaluated eagerly.
317
322
indexM :: (HasCallStack , Vector v a , Monad m ) => v a -> Int -> m a
318
323
{-# INLINE_FUSED indexM #-}
319
- indexM v i = checkIndex Bounds i (length v) $ liftBox $ basicUnsafeIndexM v i
324
+ indexM v ! i = checkIndex Bounds i (length v) $ liftBox $ basicUnsafeIndexM v i
320
325
321
326
-- | /O(1)/ First element of a vector in a monad. See 'indexM' for an
322
327
-- explanation of why this is useful.
@@ -334,7 +339,7 @@ lastM v = indexM v (length v - 1)
334
339
-- explanation of why this is useful.
335
340
unsafeIndexM :: (Vector v a , Monad m ) => v a -> Int -> m a
336
341
{-# INLINE_FUSED unsafeIndexM #-}
337
- unsafeIndexM v i = checkIndex Unsafe i (length v)
342
+ unsafeIndexM v ! i = checkIndex Unsafe i (length v)
338
343
$ liftBox
339
344
$ basicUnsafeIndexM v i
340
345
@@ -993,7 +998,7 @@ backpermute v is = seq v
993
998
-- NOTE: we do it this way to avoid triggering LiberateCase on n in
994
999
-- polymorphic code
995
1000
index :: HasCallStack => Int -> Box a
996
- index i = checkIndex Bounds i n $ basicUnsafeIndexM v i
1001
+ index ! i = checkIndex Bounds i n $ basicUnsafeIndexM v i
997
1002
998
1003
-- | Same as 'backpermute', but without bounds checking.
999
1004
unsafeBackpermute :: (Vector v a , Vector v Int ) => v a -> v Int -> v a
@@ -1010,7 +1015,7 @@ unsafeBackpermute v is = seq v
1010
1015
{-# INLINE index #-}
1011
1016
-- NOTE: we do it this way to avoid triggering LiberateCase on n in
1012
1017
-- polymorphic code
1013
- index i = checkIndex Unsafe i n $ basicUnsafeIndexM v i
1018
+ index ! i = checkIndex Unsafe i n $ basicUnsafeIndexM v i
1014
1019
1015
1020
-- Safe destructive updates
1016
1021
-- ------------------------
@@ -2534,7 +2539,7 @@ streamR v = v `seq` n `seq` (Bundle.unfoldr get n `Bundle.sized` Exact n)
2534
2539
2535
2540
{-# INLINE get #-}
2536
2541
get 0 = Nothing
2537
- get i = let i' = i- 1
2542
+ get i = let ! i' = i- 1
2538
2543
in
2539
2544
case basicUnsafeIndexM v i' of Box x -> Just (x, i')
2540
2545
0 commit comments