← Back to Pull requests

Speed up Unicode string offset conversion #1315

Closed
unknown closed this pull request without merging on Nov 23, 2026 10:15 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 18:00 UTC
Author

Speed up Unicode string indexing, slicing, and bounded searches by scanning from the nearer end of the string.
Short scans step through UTF-8 characters, while longer scans use SIMD or bulk counting.
Long scans poll the execution deadline, including when a heap string's character count is first computed.

<!-- This is an auto-generated description by cubic. -->
---

Summary by cubic


Speeds up Unicode string indexing, slicing, and bounded searches by scanning from the nearer string endpoint instead of always from the start, with cached character counts and ASCII status on string values.

Scanning

  • Adds a stateless fast_offsets.rs scanner; the caller supplies the text's character count, an optional anchor, and a deadline callback.

  • Short skips walk code points, medium skips use 16-byte SIMD via wide, and longer runs count with bytecount; forward and backward traversal share a bulk threshold of 128, or 512 when the whole string averages at least 1.5 UTF-8 bytes per code point.

  • Small indices probe at most 128 characters without counting the string; search ranges anchor the end offset on the start boundary, and results count from the search start or the string end when closer.

  • Deadline polling covers initial character counts and ASCII classification on heap strings; interrupted scans stay uncached and are retried on the next lookup.

  • Benchmarks show roughly 2.5x speedup on random access and 96x on paragraph offsets for 128K-character strings; short-string operations stay within a few percent of baseline.
  • Layout and limits

  • Splits string code into HeapStr, InlineStr, and StringIterator modules; Str is renamed HeapStr, and PyStr unifies string operations.

  • Caches character lengths on heap and interned strings, stores ASCII status in inline-string tags, propagates both through string-producing operations, and caches search-offset classifications.

  • Caps source input at u32::MAX bytes since parser offsets are 32-bit, raising SyntaxError beyond that.

  • Adds wide as a dependency, enables bytecount's runtime-dispatch SIMD, and removes HeapBoxRead::as_box_value() from the heap-reader API.
  • <sup>Written for commit c8903aa82606d973d6645d19771651017fdb81b9. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/1315?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    Diff: 350 lines (+262 / -88) across 4 files Reviewer: rewitt94 Review duration: 15374m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 350 lines diff across 4 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.351
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #1315"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.4% (MEDIUM) · Higher = larger blast radius
    47.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.7% · Safe to merge
    18.7%
    Repository