Skip to content

Commit c07029d

Browse files
committed
Introduce 'hashOfLeafOrCollision'
Core size reduction with GHC 9.12.2: * Lazy.unionWithKey: 2256 terms -> 1286 * Strict.unionWithKey: 2101 terms -> 1167 * union @int in fine-grained: 1245 terms -> 1134
1 parent 478bb60 commit c07029d

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

Data/HashMap/Internal.hs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ module Data.HashMap.Internal
146146
, deleteKeyExists
147147
, insertModifying
148148
, ptrEq
149+
, hashOfLeafOrCollision
149150
, adjust#
150151
) where
151152

@@ -1625,7 +1626,7 @@ unionWithKey f = go 0
16251626
go (nextShift s) st1 t2
16261627
in BitmapIndexed b1 ary'
16271628
where
1628-
h2 = leafHashCode t2
1629+
h2 = hashOfLeafOrCollision t2
16291630
m2 = mask h2 s
16301631
i = sparseIndex b1 m2
16311632
go s t1 (BitmapIndexed b2 ary2)
@@ -1636,24 +1637,20 @@ unionWithKey f = go 0
16361637
go (nextShift s) t1 st2
16371638
in BitmapIndexed b2 ary'
16381639
where
1639-
h1 = leafHashCode t1
1640+
h1 = hashOfLeafOrCollision t1
16401641
m1 = mask h1 s
16411642
i = sparseIndex b2 m1
16421643
go s (Full ary1) t2 =
1643-
let h2 = leafHashCode t2
1644+
let h2 = hashOfLeafOrCollision t2
16441645
i = index h2 s
16451646
ary' = updateFullArrayWith' ary1 i $ \st1 -> go (nextShift s) st1 t2
16461647
in Full ary'
16471648
go s t1 (Full ary2) =
1648-
let h1 = leafHashCode t1
1649+
let h1 = hashOfLeafOrCollision t1
16491650
i = index h1 s
16501651
ary' = updateFullArrayWith' ary2 i $ \st2 -> go (nextShift s) t1 st2
16511652
in Full ary'
16521653

1653-
leafHashCode (Leaf h _) = h
1654-
leafHashCode (Collision h _) = h
1655-
leafHashCode _ = error "leafHashCode"
1656-
16571654
goDifferentHash s h1 h2 t1 t2
16581655
| m1 == m2 = BitmapIndexed m1 (A.singleton $! goDifferentHash (nextShift s) h1 h2 t1 t2)
16591656
| m1 < m2 = BitmapIndexed (m1 .|. m2) (A.pair t1 t2)
@@ -2841,6 +2838,19 @@ otherOfOneOrZero :: Int -> Int
28412838
otherOfOneOrZero i = 1 - i
28422839
{-# INLINE otherOfOneOrZero #-}
28432840

2841+
------------------------------------------------------------------------
2842+
-- Tools for reducing duplication in code handling 'Leaf' and 'Collision' nodes
2843+
2844+
-- | The 'Hash' of a 'Leaf' or 'Collision' node.
2845+
--
2846+
-- This function is marked @NOINLINE@ to prevent GHC from generating separate
2847+
-- alternatives for 'Leaf' and 'Collision' nodes.
2848+
hashOfLeafOrCollision :: HashMap k v -> Hash
2849+
hashOfLeafOrCollision (Leaf h _) = h
2850+
hashOfLeafOrCollision (Collision h _) = h
2851+
hashOfLeafOrCollision _ = error "hashOfLeafOrCollision"
2852+
{-# NOINLINE hashOfLeafOrCollision #-}
2853+
28442854
------------------------------------------------------------------------
28452855
-- IsList instance
28462856
instance (Eq k, Hashable k) => Exts.IsList (HashMap k v) where

Data/HashMap/Internal/Strict.hs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ unionWithKey f = go 0
505505
go (nextShift s) st1 t2
506506
in BitmapIndexed b1 ary'
507507
where
508-
h2 = leafHashCode t2
508+
h2 = HM.hashOfLeafOrCollision t2
509509
m2 = mask h2 s
510510
i = sparseIndex b1 m2
511511
go s t1 (BitmapIndexed b2 ary2)
@@ -516,24 +516,20 @@ unionWithKey f = go 0
516516
go (nextShift s) t1 st2
517517
in BitmapIndexed b2 ary'
518518
where
519-
h1 = leafHashCode t1
519+
h1 = HM.hashOfLeafOrCollision t1
520520
m1 = mask h1 s
521521
i = sparseIndex b2 m1
522522
go s (Full ary1) t2 =
523-
let h2 = leafHashCode t2
523+
let h2 = HM.hashOfLeafOrCollision t2
524524
i = index h2 s
525525
ary' = HM.updateFullArrayWith' ary1 i $ \st1 -> go (nextShift s) st1 t2
526526
in Full ary'
527527
go s t1 (Full ary2) =
528-
let h1 = leafHashCode t1
528+
let h1 = HM.hashOfLeafOrCollision t1
529529
i = index h1 s
530530
ary' = HM.updateFullArrayWith' ary2 i $ \st2 -> go (nextShift s) t1 st2
531531
in Full ary'
532532

533-
leafHashCode (Leaf h _) = h
534-
leafHashCode (Collision h _) = h
535-
leafHashCode _ = error "leafHashCode"
536-
537533
goDifferentHash s h1 h2 t1 t2
538534
| m1 == m2 = BitmapIndexed m1 (A.singleton $! goDifferentHash (nextShift s) h1 h2 t1 t2)
539535
| m1 < m2 = BitmapIndexed (m1 .|. m2) (A.pair t1 t2)

0 commit comments

Comments
 (0)