perf: eliminate hot-path allocations and fix double-lock race in sync structures#34
Draft
perf: eliminate hot-path allocations and fix double-lock race in sync structures#34
Conversation
… hot paths Co-authored-by: twisti-dev <76837088+twisti-dev@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Identify and improve slow or inefficient code
perf: eliminate hot-path allocations and fix double-lock race in sync structures
Feb 23, 2026
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.
Several hot-path code paths had unnecessary heap allocations and a correctness issue in
SyncMapImpl.removeIf. This PR addresses the most impactful ones.Changes
AtomicBooleanremoved from stream message handler (both caches)processStreamMessageruns on every Redis stream event. It was allocating a freshAtomicBooleansolely to smuggle a flag out of thegetAndUpdatelambda. Replaced with arithmetic on the returnedprevVersion:SyncMapImpl.removeIf— two write locks collapsed to oneThe original code held the write lock to collect matching keys, released it, then re-acquired it to remove them. This left a mutation window between the two critical sections and contained a bug:
removeManyRemotewas called with all matched keys, not just the ones actually removed in the second pass. Now uses a singlefastIterator().remove()pass with parallelObjectArrayLists (noPairboxing):lock.write { val it = map.object2ObjectEntrySet().fastIterator() while (it.hasNext()) { val e = it.next() if (predicate(e.key, e.value)) { removedKeys.add(e.key); removedVals.add(e.value); it.remove() } } } removeManyRemote(removedKeys) // only actually-removed keysoverrideFromRemoteintermediate collections eliminatedSyncMapImpl:map { }.toMap()(producesList<Pair>+LinkedHashMap) → single loop into a pre-sizedObject2ObjectOpenHashMap.SyncSetImpl/SyncListImpl:map(::decodeValue)+addAll→mapTodirectly into the target collection, skipping the intermediate list.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.