Skip to content

Commit f02530b

Browse files
committed
Refactor size
$ cabal run fine-grained -- -p size --stdev 1 All HashMap.Strict size Int 0: OK 2.04 ns ± 22 ps 1: OK 3.01 ns ± 40 ps 5: OK 20.0 ns ± 128 ps 10: OK 45.3 ns ± 568 ps 50: OK 186 ns ± 1.1 ns 100: OK 396 ns ± 4.1 ns 500: OK 1.88 μs ± 5.8 ns 1000: OK 4.59 μs ± 84 ns 5000: OK 28.3 μs ± 483 ns 10000: OK 56.2 μs ± 885 ns 50000: OK 387 μs ± 4.4 μs 100000: OK 735 μs ± 4.9 μs 500000: OK 5.36 ms ± 51 μs
1 parent 48fe903 commit f02530b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Data/HashMap/Internal.hs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,19 @@ null _ = False
615615

616616
-- | \(O(n)\) Return the number of key-value mappings in this map.
617617
size :: HashMap k v -> Int
618-
size t = go t 0
619-
where
620-
go (Leaf _ _) !n = n + 1
621-
go (BitmapIndexed _ ary) n = A.foldl' (flip go) n ary
622-
go (Full ary) n = A.foldl' (flip go) n ary
623-
go (Collision _ ary) n = n + A.length ary
618+
size = \case
619+
BitmapIndexed b ary
620+
| b == 0 -> 0
621+
| otherwise -> A.foldl' size_ 0 ary
622+
m -> size_ 0 m
623+
{-# INLINE size #-}
624+
625+
size_ :: Int -> HashMap k v -> Int
626+
size_ !n = \case
627+
Leaf _ _ -> n + 1
628+
BitmapIndexed _ ary -> A.foldl' size_ n ary
629+
Full ary -> A.foldl' size_ n ary
630+
Collision _ ary -> n + A.length ary
624631

625632
-- | \(O(\log n)\) Return 'True' if the specified key is present in the
626633
-- map, 'False' otherwise.

0 commit comments

Comments
 (0)