[improve][ml] Reduce object allocation when iterate on LongPairRangeSet - #18357
Conversation
|
origin jmh test here the result json can achieve here just upload to https://jmh.morethan.io/ get the picture. |
| } | ||
|
|
||
| @Override | ||
| public <O> void forEachWithRangeBoundMapper(LongPairConsumer<O> rawRangeBoundMapper, |
There was a problem hiding this comment.
There is a lot of duplication of code with forEach, can it be optimized?
There was a problem hiding this comment.
thanks for review. the generic for range require for <T extands Comparable<T>> which is used in Range<T> but for iteration ops we may transform to non Comparable object. i try to reuse but we need the MLDataFormats.MessageRange implements Comparable
There was a problem hiding this comment.
Ichange the signature and optimize the code. please take a look~
| return rangeList.size() <= config.getMaxUnackedRangesToPersist(); | ||
| }); | ||
|
|
||
| individualDeletedMessages.forEachWithRangeBoundMapper( |
There was a problem hiding this comment.
Can we just have a callback like
individualDeletedMessages.forEach((l1, l2, u1, u2) -> {
})There was a problem hiding this comment.
Nice suggestion ! I have done coding. please take a look again.
d257efa to
94bb275
Compare
06935e6 to
265dec6
Compare
|
/pulsarbot run-failure-checks |
Codecov Report
@@ Coverage Diff @@
## master #18357 +/- ##
============================================
- Coverage 47.23% 45.17% -2.06%
- Complexity 10430 10660 +230
============================================
Files 692 758 +66
Lines 67766 72750 +4984
Branches 7260 7805 +545
============================================
+ Hits 32009 32865 +856
- Misses 32192 36200 +4008
- Partials 3565 3685 +120
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
All CI passed in my local repository. see lifepuzzlefun#2 |
| @Override | ||
| public void forEachRawRange(RawRangeProcessor action) { | ||
| for (Range<T> range : asRanges()) { | ||
| LongPair lowerEndpoint = this.rangeEndPointConsumer.apply(range.lowerEndpoint()); |
There was a problem hiding this comment.
When a range does not set the lower boundary, whether there will be NPE here?
There was a problem hiding this comment.
thanks for review, after check the code, all range is insert by LongPairRangeSet.addOpenClosed(long,long,long,long) i think there is no chance to get a Range which boundary is not set. maybe i missed somewhere, correct me if im wrong
|
cc @codelipenghui @Technoboy- @Jason918 shall we merge this patch now? |
The forEachRawRange callback used to append each range and then return rangeList.size() <= maxRanges, which kept iteration alive until the list reached maxRanges + 1 entries. One extra range was therefore serialized on every persist, and a cursor whose exact ack-hole count equaled maxRanges + 1 raised a spurious overflow warning even though nothing was actually truncated. Switch to the check-before-add pattern (symmetric with the existing buildBatchEntryDeletionIndexInfoList logic) and derive the overflow signal from a MutableBoolean set when the callback refuses an extra range. MutableBoolean is used rather than AtomicBoolean because this method runs under the cursor write lock and does not need atomic semantics; the existing MutableBoolean usage in readEntries (around line 2219) follows the same convention. The regression lived since apache#3819 (2019), when the original stream().limit(N) was translated to a forEach callback without keeping the strict cap. PR apache#18357 later migrated to forEachRawRange and carried the bug forward. Extend testPersistOverflowRangesCounter to reopen the cursor after ledger.close() in the overflow scenario and assert that exactly maxRanges entries are recovered, guarding against a reintroduction. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Fixes #18356
Motivation
support direct perform logic on (long,long) pair without
RangeandPostionImplobject creationModifications
LongPairRangeSet.forEachWithRangeBoundMapperManagedCursorImpl.buildIndividualDeletedMessageRangesandLongPairRangeSet.sizeto use this functionforEachandforEachWithRangeBoundMapperreturn the same result.Verifying this change
This change is a trivial rework / code cleanup without any test coverage.
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
docdoc-requireddoc-not-neededdoc-complete