Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreapMap union and intersection #19

Merged
merged 3 commits into from
Dec 19, 2024
Merged

TreapMap union and intersection #19

merged 3 commits into from
Dec 19, 2024

Conversation

ericeil
Copy link
Collaborator

@ericeil ericeil commented Dec 16, 2024

TreapMap.merge is a very flexible way to merge two TreapMaps, but it has a couple of drawbacks:

  1. It always visits every entry in both maps
  2. It doesn't allow null values to be added to the result map (because merge treats null as meaning "remove this entry")

The first issue can be a major performance problem if you're merging two large maps with a relatively small key intersection, and you only need to apply custom merge logic to the keys in the intersection. This turns out to be very common.

To address this, we add new methods to TreapMap:

    public fun union(
        m: Map<K, V>,
        merger: (K, V, V) -> V
    ): TreapMap<K, V>

    public fun intersect(
        m: Map<K, V>,
        merger: (K, V, V) -> V
    ): TreapMap<K, V>

These apply the merger function only to the intersection of the two maps' keys, and don't permit merger to discard entries. union preserves all non-intersecting entries from both maps, while intersect discards them.

We also add parallel versions of both functions, following the example of merge/parallelMerge.

See https://github.com/Certora/EVMVerifier/pull/6819 for an example of where this can be used to get some significant performance benefits.

@ericeil ericeil requested a review from jtoman December 17, 2024 00:50
@ericeil ericeil marked this pull request as ready for review December 17, 2024 00:50
@ericeil ericeil changed the title TreapMap union and intersect TreapMap union and intersection Dec 17, 2024
Copy link

@jtoman jtoman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good modulo a spelling error

/**
Calls [action] for each entry in the map.

Traverses the treap without allocating temprarory storage, which may be more efficient than `entries.forEach`.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"temporary"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@ericeil ericeil requested a review from jtoman December 17, 2024 15:51
@ericeil ericeil merged commit 38a54f5 into Certora:main Dec 19, 2024
1 check passed
@ericeil ericeil deleted the mergeModes branch December 19, 2024 19:47
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.

2 participants