Accidentally-Quadratic HashMaps

Posted on October 16, 2020 by Jack Kelly
Tags: coding, haskell, performance

TL;DR: Unless you’re careful, using foldMap to build HashMaps (from unordered-containers) can take O(n2) time. This can be avoided by:

  1. Building a Map (from containers) instead;

  2. Using foldr or foldl with insert; or

  3. Using foldMap to build an intermediate list [(k, v)] and then building the HashMap with fromList.

Shout-out to Luke Worth who found this out while we were chasing down performance problems.

Read more...
All Posts | RSS | Atom
Copyright © 2024 Jack Kelly
Site generated by Hakyll (source)