Skip to content

ICU-23644 Use exponential backoff in BreakCache::populatePreceding - #4398

Open
FrankYFTang wants to merge 1 commit into
unicode-org:mainfrom
FrankYFTang:fix-icu-23644
Open

FrankYFTang wants to merge 1 commit into
unicode-org:mainfrom
FrankYFTang:fix-icu-23644

Conversation

@FrankYFTang

Copy link
Copy Markdown
Contributor

Replace the fixed - 30 step in RuleBasedBreakIterator::BreakCache::populatePreceding() (in both ICU4C and ICU4J) with exponential backoff starting at 30 and doubling on each iteration (with an overflow guard).

Summary

  • ICU4C (icu4c/source/common/rbbi_cache.cpp): Use backupStep = 30, doubling each iteration (up to INT32_MAX / 2) in BreakCache::populatePreceding().
  • ICU4J (icu4j/main/core/src/main/java/com/ibm/icu/text/RuleBasedBreakIterator.java): Matching exponential backoff (up to Integer.MAX_VALUE / 2) in BreakCache.populatePreceding().
  • Tests: Added TestICU23644 in icu4c/source/test/intltest/rbbitst.{h,cpp} and icu4j/main/core/src/test/java/com/ibm/icu/dev/test/rbbi/RBBITest.java covering word, sentence, and line break iterators on long unbroken runs.

For normal short runs, iteration 1 still backs up by 30 with no change in behavior; for long unbroken segments where handleSafePrevious() stops quickly inside the segment, backing up completes in O(log N) iterations / O(N) total scanned characters instead of O(N^2).

Checklist

  • Required: Issue filed: https://unicode-org.atlassian.net/browse/ICU-23644
  • Required: The PR title must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Required: Each commit message must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Issue accepted (done by Technical Committee after discussion)
  • Tests included, if applicable
  • API docs and/or User Guide docs changed or added, if applicable
  • Approver: Feel free to merge on my behalf

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 eggrobin left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
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