perf: hot-path quick wins — hoist getSimpleName, batch scratch buffer + BitSet, drop KeyOrdered wrapper#203
Merged
Merged
Conversation
… + BitSet, drop KeyOrdered wrapper
Three small, surgical allocation cuts on the per-record hot path. No
behavior change; existing tests pass unmodified.
1. TypedPipelineBuilder.deserialize: hoist `format.getClass().getSimpleName()`
from per-record to build-time. The value is only used inside the catch
arm (diagnostic message), but it was being recomputed on every successful
deserialize.
2. BatchPipelineWrapper.flushLocked:
- Replace `List.copyOf(buffer)` + separate values `ArrayList` with one
pre-sized `ArrayList<>(buffer)` snapshot and one pre-sized values list.
The buffer is now reused (`ArrayList.clear()` keeps the backing array)
so steady-state flushes don't reallocate it.
- Replace `HashSet<Integer>(size*2)` with `BitSet(size)` for tracking
succeeded indexes — no boxing on the index walks.
- Replace `ArrayList<Integer> missing` with a count-first pass; only
allocate the list on the (rare) coverage-violation path. Common case
is allocation-free.
3. KeyOrderedDispatcher.dispatch: replace the per-dispatch wrapper
`Runnable` (capturing `processTask`, `onComplete`, `pending`, and `key`)
with a small `QueuedTask` record holding only `(processTask, onComplete)`.
`drain()` now handles `pending.decrement` + `onComplete` directly in its
own try/finally, preserving the same throw-safety + drain-loop-survives-
any-Throwable semantics as the old wrapper.
Bigger wins surfaced by the audit (OffsetManager TopicPartition caching,
MessageFormat `deserialize(byte[], int, int)` SPI extension to skip the
`System.arraycopy` on the skipBytes path) are deferred for JMH-baselined PRs.
Tests: kpipe-core and kpipe-consumer unit suites green on both runs. The
two failing tests (`ExternalOffsetIntegrationTest`, `KPipeProducerIntegrationTest`)
are Testcontainers-based and fail on main too due to Docker daemon being
unavailable in this environment — unrelated to these changes.
Owner
Author
|
@copilot please review |
Copilot stopped work on behalf of
eschizoid due to an error
June 19, 2026 23:34
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #203 +/- ##
============================================
- Coverage 77.82% 77.80% -0.02%
+ Complexity 732 724 -8
============================================
Files 68 66 -2
Lines 2800 2798 -2
Branches 346 351 +5
============================================
- Hits 2179 2177 -2
+ Misses 462 461 -1
- Partials 159 160 +1 ☔ View full report in Codecov by Harness. |
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
Three small, surgical allocation cuts on the per-record hot path. No behavior change; existing tests pass unmodified.
1.
TypedPipelineBuilder.deserialize— hoistgetSimpleName()format.getClass().getSimpleName()was recomputed on everydeserialize()call, but the value is only used inside the catch arm (diagnostic message wrap). Moved it to afinalcapture inbuild()so it's computed exactly once per built pipeline.Allocation eliminated: one
Stringplus a class-hierarchy walk per record on the success path.2.
BatchPipelineWrapper.flushLocked— scratch buffer reuse +BitSetList.copyOf(buffer)+ separateArrayList<T>for values with one pre-sizedArrayList<>(buffer)snapshot and one pre-sized values list. Thebufferfield is now reused (ArrayList.clear()keeps the backing array), so steady-state flushes don't reallocate the buffer.HashSet<Integer>(size * 2)forsucceededwithBitSet(size)— constant-timeset/geton primitiveintindexes, zeroIntegerboxing on the walks.ArrayList<Integer> missingwith a count-first pass; only allocate the list on the (rare) coverage-violation path. Common case is allocation-free for the missing-index tracking.Allocations eliminated per flush: the immutable
List.copyOfarray, theHashSetplus its boxedIntegerentries, the always-allocatedmissinglist, and the buffer's backing array on every steady-state flush.3.
KeyOrderedDispatcher.dispatch— drop the wrapperRunnableThe dispatcher used to wrap
(processTask, onComplete)in a per-dispatch lambda that also capturedpendingandkey. Replaced with a smallQueuedTaskrecord holding only(processTask, onComplete).drain()now handlespending.decrement+onCompletedirectly in its own try/finally, preserving the same throw-safety + drain-loop-survives-any-Throwablesemantics as the old wrapper.The dispatcher's public
dispatch(record, processTask, onComplete)signature is unchanged — all existing dispatcher tests pass without modification.Allocation eliminated: the wrapper
Runnable(captures 4 things) becomes a 2-field record. Lower object header + capture overhead per dispatch.Deferred for JMH-baselined PRs
Bigger wins surfaced by the same audit are deliberately not in this PR — they need before/after benchmark numbers, not just "looks faster":
OffsetManager: cacheTopicPartitioninstances per(topic, partition)instead of allocating one per record'smarkOffsetProcessed/trackOffset.MessageFormatSPI: adddeserialize(byte[], int, int)so theskipBytes(n)path can skip theSystem.arraycopyon every record.Test plan
./gradlew :lib:kpipe-core:test— green (change 1 covered)./gradlew :lib:kpipe-consumer:test— green twice, 263 unit tests pass on both runs. Two@Testcontainersintegration tests (ExternalOffsetIntegrationTest,KPipeProducerIntegrationTest) fail withDockerClientProviderStrategyerrors — pre-existing failure onmainin any environment without a running Docker daemon, unrelated to these changes.KeyOrderedDispatcherTestcases pass without modification (signature preserved).BatchPipelineWrapper*Testcases pass — coverage contract, concurrency, and out-of-range index handling all green.