Repository navigation
Iterate treaps with an explicit stack instead of coroutines - #30
Merged
Merged
Conversation
Set and map iterators (and keys, values, entries, asTreapSequence) were
`sequence {}` builders, nested two deep for hashed sets and with a new
coroutine per node for map entries. A node iterator with an explicit stack
replaces them; iteration order is unchanged.
Best-of-15 ns per element, before -> after, at 50 / 2,000 elements:
hashed set 24.7 -> 6.6 / 33.4 -> 6.0, sorted set 12.8 -> 4.3 / 17.2 -> 6.3,
hashed map entries 40.8 -> 5.8 / 44.4 -> 6.2, keys 50.7 -> 5.3 / 48.5 -> 4.7.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ericeil
approved these changes
Nov 3, 2026
ericeil
left a comment
Collaborator
There was a problem hiding this comment.
Looks great! I've been meaning to try this for a long time. :)
Collaborator
|
Released as version 1.7.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Set and map iteration no longer uses coroutines.
HashTreapSet.iterator()was asequence {}wrapped around a second one (asTreapSequence), and mapentries/keys/valuesstarted a new coroutine for every node.TreapNodeIterator(inTreap.kt): in-order node walk with an explicit array stack.HashTreapSet/SortedTreapSet.iterator(), mapentries/keys/values(through a new internalentryIterator(transform)),entrySequence()andasTreapSequence()all go through it.zipstill usessequence {}; it didn't show up in profiles.Why
A JFR profile of a large EVM rule in the Certora Prover (about 1M TAC commands, 359 JVM CPU-s) spent 12.5% of JVM CPU inside coroutine/sequence iteration machinery, spread over about 100 call sites (
forloops,any,filter,singleOrNull,groupBy, ... on treap sets and maps).Results
Micro-benchmark, best of 15 rounds, ns per element, before -> after:
At these sizes iterating now costs about the same as
forEachElement/forEachEntry. At 100,000 elements memory access dominates and the gain is about 2x.Prover profile of the same rule with the first commit of this PR patched in: iteration machinery went from 12.5% to 6.0% of JVM CPU, same verdict. 1.5 points of the remaining 6.0% were the
ArrayListstack growing, which the second commit removes; the rest is mostly Kotlin sequences in the prover's own code.Testing
./gradlew :collect:test).iteratorMatchesForEachElement,iteratorsMatchForEachEntry, run for hashed and sorted sets and maps): the iterators return the same elements in the same order asforEachElement/forEachEntry, with many hash collisions, andnext()past the end throwsNoSuchElementException.🤖 Generated with Claude Code