Skip to content

Avoid intermediate Map allocations in groupFlatMap#79

Open
cheeseng wants to merge 1 commit into
dotty-staging:stdlib/group-flat-mapfrom
artimahub:stdlib/group-flat-map-artima-cs
Open

Avoid intermediate Map allocations in groupFlatMap#79
cheeseng wants to merge 1 commit into
dotty-staging:stdlib/group-flat-mapfrom
artimahub:stdlib/group-flat-map-artima-cs

Conversation

@cheeseng

Copy link
Copy Markdown

The Result/AbstractFunction1 pattern finalized builders via repeated calls to immutable.Map#updated, which rebuilds parts of the HashMap trie on every call and discards all but the final version as garbage.

Replace it with view.mapValues(_.result()).toMap, which stages results in a single Map builder and freezes it once. Same O(n + k) complexity (n = elements, k = groups), fewer allocations.

Enhanced the test in group-flat-map.scala to cover more cases.

The Result/AbstractFunction1 pattern finalized builders via repeated
calls to immutable.Map#updated, which rebuilds parts of the HashMap
trie on every call and discards all but the final version as garbage.

Replace it with view.mapValues(_.result()).toMap, which stages results
in a single Map builder and freezes it once. Same O(n + k) complexity
(n = elements, k = groups), fewer allocations.

Enhanced the test in group-flat-map.scala to cover more cases.
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