Skip to content

Commit ded91f1

Browse files
committed
Optimize iteration over collision arrays
1 parent 1029038 commit ded91f1

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

Data/HashMap/Internal.hs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,17 +1056,17 @@ insertModifying x f k0 m0 = go h0 k0 0 m0
10561056
-- | Like insertModifying for arrays; used to implement insertModifying
10571057
insertModifyingArr :: Eq k => v -> (v -> (# v #)) -> k -> A.Array (Leaf k v)
10581058
-> A.Array (Leaf k v)
1059-
insertModifyingArr x f k0 ary0 = go k0 ary0 0 (A.length ary0)
1059+
insertModifyingArr x f k0 ary0 = go k0 ary0 (A.length ary0 - 1)
10601060
where
1061-
go !k !ary !i !n
1061+
go !k !ary !i
10621062
-- Not found, append to the end.
1063-
| i >= n = A.snoc ary $ L k x
1063+
| i < 0 = A.snoc ary $ L k x
10641064
| otherwise = case A.index ary i of
10651065
(L kx y) | k == kx -> case f y of
10661066
(# y' #) -> if ptrEq y y'
10671067
then ary
10681068
else A.update ary i (L k y')
1069-
| otherwise -> go k ary (i+1) n
1069+
| otherwise -> go k ary (i-1)
10701070
{-# INLINE insertModifyingArr #-}
10711071

10721072
-- | In-place update version of insertWith
@@ -2309,41 +2309,41 @@ fromListWithKey f = List.foldl' (\ m (k, v) -> unsafeInsertWithKey (\k' a b -> (
23092309
lookupInArrayCont ::
23102310
forall rep (r :: TYPE rep) k v.
23112311
Eq k => ((# #) -> r) -> (v -> Int -> r) -> k -> A.Array (Leaf k v) -> r
2312-
lookupInArrayCont absent present k0 ary0 = go k0 ary0 0 (A.length ary0)
2312+
lookupInArrayCont absent present k0 ary0 = go k0 ary0 (A.length ary0 - 1)
23132313
where
2314-
go :: Eq k => k -> A.Array (Leaf k v) -> Int -> Int -> r
2315-
go !k !ary !i !n
2316-
| i >= n = absent (# #)
2314+
go :: Eq k => k -> A.Array (Leaf k v) -> Int -> r
2315+
go !k !ary !i
2316+
| i < 0 = absent (# #)
23172317
| otherwise = case A.index ary i of
23182318
(L kx v)
23192319
| k == kx -> present v i
2320-
| otherwise -> go k ary (i+1) n
2320+
| otherwise -> go k ary (i-1)
23212321
{-# INLINE lookupInArrayCont #-}
23222322

23232323
-- | \(O(n)\) Lookup the value associated with the given key in this
23242324
-- array. Returns 'Nothing' if the key wasn't found.
23252325
indexOf :: Eq k => k -> A.Array (Leaf k v) -> Maybe Int
2326-
indexOf k0 ary0 = go k0 ary0 0 (A.length ary0)
2326+
indexOf k0 ary0 = go k0 ary0 (A.length ary0 - 1)
23272327
where
2328-
go !k !ary !i !n
2329-
| i >= n = Nothing
2328+
go !k !ary !i
2329+
| i < 0 = Nothing
23302330
| otherwise = case A.index ary i of
23312331
(L kx _)
23322332
| k == kx -> Just i
2333-
| otherwise -> go k ary (i+1) n
2333+
| otherwise -> go k ary (i-1)
23342334
{-# INLINABLE indexOf #-}
23352335

23362336
updateWith# :: Eq k => (v -> (# v #)) -> k -> A.Array (Leaf k v) -> A.Array (Leaf k v)
2337-
updateWith# f k0 ary0 = go k0 ary0 0 (A.length ary0)
2337+
updateWith# f k0 ary0 = go k0 ary0 (A.length ary0 - 1)
23382338
where
2339-
go !k !ary !i !n
2340-
| i >= n = ary
2339+
go !k !ary !i
2340+
| i < 0 = ary
23412341
| otherwise = case A.index ary i of
23422342
(L kx y) | k == kx -> case f y of
23432343
(# y' #)
23442344
| ptrEq y y' -> ary
23452345
| otherwise -> A.update ary i (L k y')
2346-
| otherwise -> go k ary (i+1) n
2346+
| otherwise -> go k ary (i-1)
23472347
{-# INLINABLE updateWith# #-}
23482348

23492349
updateOrSnocWith :: Eq k => (v -> v -> (# v #)) -> k -> v -> A.Array (Leaf k v)
@@ -2353,17 +2353,17 @@ updateOrSnocWith f = updateOrSnocWithKey (const f)
23532353

23542354
updateOrSnocWithKey :: Eq k => (k -> v -> v -> (# v #)) -> k -> v -> A.Array (Leaf k v)
23552355
-> A.Array (Leaf k v)
2356-
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 0 (A.length ary0)
2356+
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 (A.length ary0 - 1)
23572357
where
2358-
go !k v !ary !i !n
2358+
go !k v !ary !i
23592359
-- Not found, append to the end.
2360-
| i >= n = A.snoc ary $ L k v
2360+
| i < 0 = A.snoc ary $ L k v
23612361
| L kx y <- A.index ary i
23622362
, k == kx
23632363
, (# v2 #) <- f k v y
23642364
= A.update ary i (L k v2)
23652365
| otherwise
2366-
= go k v ary (i+1) n
2366+
= go k v ary (i-1)
23672367
{-# INLINABLE updateOrSnocWithKey #-}
23682368

23692369
updateOrConcatWithKey :: Eq k => (k -> v -> v -> (# v #)) -> A.Array (Leaf k v) -> A.Array (Leaf k v) -> A.Array (Leaf k v)

Data/HashMap/Internal/Strict.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -713,13 +713,13 @@ fromListWithKey f = List.foldl' (\ m (k, v) -> unsafeInsertWithKey f k v m) HM.e
713713
-- Array operations
714714

715715
updateWith :: Eq k => (v -> v) -> k -> A.Array (Leaf k v) -> A.Array (Leaf k v)
716-
updateWith f k0 ary0 = go k0 ary0 0 (A.length ary0)
716+
updateWith f k0 ary0 = go k0 ary0 (A.length ary0 - 1)
717717
where
718-
go !k !ary !i !n
719-
| i >= n = ary
718+
go !k !ary !i
719+
| i < 0 = ary
720720
| otherwise = case A.index ary i of
721721
(L kx y) | k == kx -> let !v' = f y in A.update ary i (L k v')
722-
| otherwise -> go k ary (i+1) n
722+
| otherwise -> go k ary (i-1)
723723
{-# INLINABLE updateWith #-}
724724

725725
-- | Append the given key and value to the array. If the key is
@@ -739,14 +739,14 @@ updateOrSnocWith f = updateOrSnocWithKey (const f)
739739
-- array.
740740
updateOrSnocWithKey :: Eq k => (k -> v -> v -> v) -> k -> v -> A.Array (Leaf k v)
741741
-> A.Array (Leaf k v)
742-
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 0 (A.length ary0)
742+
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 (A.length ary0 - 1)
743743
where
744-
go !k v !ary !i !n
744+
go !k v !ary !i
745745
-- Not found, append to the end.
746-
| i >= n = A.snoc ary $! L k $! v
746+
| i < 0 = A.snoc ary $! L k $! v
747747
| otherwise = case A.index ary i of
748748
(L kx y) | k == kx -> let !v' = f k v y in A.update ary i (L k v')
749-
| otherwise -> go k v ary (i+1) n
749+
| otherwise -> go k v ary (i-1)
750750
{-# INLINABLE updateOrSnocWithKey #-}
751751

752752
------------------------------------------------------------------------

0 commit comments

Comments
 (0)