Repository navigation
ICU-23644 Use exponential backoff in BreakCache::populatePreceding - #4398
Open
FrankYFTang wants to merge 1 commit into
Open
FrankYFTang wants to merge 1 commit into
FrankYFTang wants to merge 1 commit into
Conversation
Use exponential backoff (starting at 30 and doubling on each iteration, with an overflow guard) instead of a fixed step of 30 when searching backwards in BreakCache::populatePreceding() in both ICU4C and ICU4J. This avoids O(N^2) rescanning when backing up across long unbroken segments where handleSafePrevious() stops quickly inside the segment.
eggrobin
approved these changes
Nov 25, 2026
Member
There was a problem hiding this comment.
This seems like reasonable way to fix this.
Ideally I would like to figure out how to automatically construct a backward state machine (and then we look forward for backward safe pairs), but while reversing a regular language is a textbook problem, constructing a DFA that accepts the same strings in reverse as the forward DFA with maximal munch is tricky (at the very least it is clear that lookahead is required; while that is a feature we have, it means we are no longer just recognizing regular languages, so things get messier theoretically).
This branch has not been deployed
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.
Replace the fixed
- 30step inRuleBasedBreakIterator::BreakCache::populatePreceding()(in both ICU4C and ICU4J) with exponential backoff starting at30and doubling on each iteration (with an overflow guard).Summary
icu4c/source/common/rbbi_cache.cpp): UsebackupStep = 30, doubling each iteration (up toINT32_MAX / 2) inBreakCache::populatePreceding().icu4j/main/core/src/main/java/com/ibm/icu/text/RuleBasedBreakIterator.java): Matching exponential backoff (up toInteger.MAX_VALUE / 2) inBreakCache.populatePreceding().TestICU23644inicu4c/source/test/intltest/rbbitst.{h,cpp}andicu4j/main/core/src/test/java/com/ibm/icu/dev/test/rbbi/RBBITest.javacoveringword,sentence, andlinebreak iterators on long unbroken runs.For normal short runs, iteration 1 still backs up by
30with no change in behavior; for long unbroken segments wherehandleSafePrevious()stops quickly inside the segment, backing up completes inO(log N)iterations /O(N)total scanned characters instead ofO(N^2).Checklist