Speed up Unicode string offset conversion

Merged
JA
12d ago
+804-87

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.


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.