Skip to content

perf: eliminate hot-path allocations and fix double-lock race in sync structures#34

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/improve-code-efficiency
Draft

perf: eliminate hot-path allocations and fix double-lock race in sync structures#34
Copilot wants to merge 2 commits intomasterfrom
copilot/improve-code-efficiency

Conversation

Copy link
Contributor

Copilot AI commented Feb 23, 2026

Several hot-path code paths had unnecessary heap allocations and a correctness issue in SyncMapImpl.removeIf. This PR addresses the most impactful ones.

Changes

AtomicBoolean removed from stream message handler (both caches)

processStreamMessage runs on every Redis stream event. It was allocating a fresh AtomicBoolean solely to smuggle a flag out of the getAndUpdate lambda. Replaced with arithmetic on the returned prevVersion:

// Before
val shouldInvalidate = AtomicBoolean(false)
val oldVersion = lastVersion.getAndUpdate { cur ->
    if (...) { shouldInvalidate.set(true); version } else cur
}
if (shouldInvalidate.get()) { … }

// After
val prevVersion = lastVersion.getAndUpdate { cur -> … }
if (prevVersion != 0L && version > prevVersion + 1) { … }

SyncMapImpl.removeIf — two write locks collapsed to one

The 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: removeManyRemote was called with all matched keys, not just the ones actually removed in the second pass. Now uses a single fastIterator().remove() pass with parallel ObjectArrayLists (no Pair boxing):

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 keys

overrideFromRemote intermediate collections eliminated

  • SyncMapImpl: map { }.toMap() (produces List<Pair> + LinkedHashMap) → single loop into a pre-sized Object2ObjectOpenHashMap.
  • SyncSetImpl / SyncListImpl: map(::decodeValue) + addAllmapTo directly 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.

… 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
Copilot AI requested a review from twisti-dev February 23, 2026 11:51
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