Skip to content

Use unboxed Maybe for map lookups on GHC - #1238

Draft
meooow25 wants to merge 2 commits into
haskell:masterfrom
meooow25:lookup-unbox
Draft

Use unboxed Maybe for map lookups on GHC#1238
meooow25 wants to merge 2 commits into
haskell:masterfrom
meooow25:lookup-unbox

Conversation

@meooow25

Copy link
Copy Markdown
Contributor

Trying this out for #801.

@meooow25

Copy link
Copy Markdown
Contributor Author

The benchmarks results are interesting.

Map:

Name              Time - - - - - - - -    Allocated - - - - -
                       A       B     %         A       B     %
lookup absent     474 μs  502 μs   +5%    145 B   317 B   +118%
lookup present    469 μs  459 μs   -2%    128 KB  329 B   -99%

Great reduction in allocations, but no effect on time saved. I expect GHC is just very fast with short-lived allocations.

IntMap:

Name             Time - - - - - - - -    Allocated - - - - -
                      A       B     %         A       B     %
lookup_half      279 μs  296 μs   +6%    148 B   147 B    +0%
lookup_hits      479 μs  522 μs   +8%    1.0 KB  335 B   -67%
lookup_misses    472 μs  499 μs   +5%    309 B   305 B    -1%
lookup_mixed     246 μs  268 μs   +9%    148 B   161 B    +8%
lookup_most      438 μs  474 μs   +8%    308 B   293 B    -4%

Surprisingly, there are no allocations even without this change. I checked the Core and found that lookup is small enough that GHC inlines it and simplifies, removing the allocations. With this change, GHC no longer inlines lookup, which makes it slightly slower. I would argue it is still a good change because it has a positive impact on code size and compile time if lookup doesn't inline at every call-site.

@meooow25

Copy link
Copy Markdown
Contributor Author

Point of reference: unordered-containers does the same thing, see https://github.com/haskell-unordered-containers/unordered-containers/blob/594a3055/Data/HashMap/Internal.hs#L670-L677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant