Skip to content

[Unicode] Shrink the property range pool by 34.9% - #2342

Open
andre-rosa wants to merge 3 commits into
meta:static_hfrom
andre-rosa:unicode-property-range-pool
Open

andre-rosa wants to merge 3 commits into
meta:static_hfrom
andre-rosa:unicode-property-range-pool

Conversation

@andre-rosa

@andre-rosa andre-rosa commented Nov 30, 2026 •

Copy link
Copy Markdown

Summary

Building on the upstream ULEB128 encoding, this series shrinks the Unicode property range pool from 39,660 to 25,819 bytes and saves 14,952 bytes in the stripped ARMv7 Thumb-2 CLI.

Before ULEB128, the pool occupied 145,632 bytes. The two changes together save 119,813 bytes (82.3%) relative to that original representation. Upstream ULEB128 change

The series has three independently reviewable commits.

  1. Pack short property ranges into one byte. Encode small gap and length pairs together, with a tagged fallback for larger values. Check that decoding each generated block reproduces its original endpoints.
  2. Encode alternating singleton property ranges as runs. Use a reserved token for runs of singleton ranges separated by one code point. Extend the generator checks to cover run lengths and add a C++ regression for a reference that ends partway through a run.
  3. Reserve range capacity before decoding properties. Use the generated range counts when decoding into an empty set and the exact complement count when inserting inverted ranges into an empty receiver.

genUnicodeTable.py emits the encoded bytes and reference offsets. C++ decodes the table while constructing regex character sets. The encoding preserves range endpoints, aliases, shared references, and block resets.

Size and compile time

Encoding Unicode range pool Stripped ARMv7 Thumb-2 CLI savings Stripped x86-64 CLI savings
Upstream ULEB128 39,660 bytes Baseline Baseline
Pairs plus run token 25,819 bytes 15,016 bytes 12,288 bytes
Full series with range reservation 25,819 bytes 14,952 bytes 12,288 bytes

Capacity reservation adds 64 bytes to the stripped ARMv7 CLI relative to the first two commits. Reserving before insertion avoids intermediate range-buffer growth and the copies that occur when growth moves the buffer. In separate x86-64 instrumentation trials, upstream and the first two commits each made 404,844 process-wide realloc calls. The full series made 184,984 calls, a reduction of 54.3%. The instrumentation counted calls to realloc.

The table shows the full series versus upstream. Values are median paired changes, with interquartile intervals in brackets. Negative values mean less time.

Workload ARMv7 Thumb-2 x86-64
Typical Unicode −1.9% [−2.3% to −1.8%] −10.1% [−10.4% to −10.0%]
Property-heavy Unicode −0.3% [−0.7% to −0.1%] −3.8% [−4.0% to −3.7%]

The ARMv7 build used -Os, LTO, and Thumb-2. All variants ran the same benchmark bytecode in balanced execution orders, with six samples per variant on ARMv7 and twelve on x86-64. The timed loop measured new RegExp construction. Matching ran outside the timed region.

Test plan

  • Verified all 13 Unicode 17.0.0 input hashes and reproduced the encoded pool and reference tables.
  • Passed six offline generator tests in normal and optimized Python. They cover packed and fallback boundaries, run thresholds, multibyte run lengths, and rejection of malformed or incorrect encodings.
  • Independently decoded all 356 references and verified that all 18,797 referenced ranges matched upstream.
  • Verified that the C++ regression emits only the requested prefix when a reference ends partway through a run. Removing the clamp caused the test to fail on the extra ranges.
  • Confirmed byte-for-byte identical stdout and Hermes bytecode for both Unicode regex fixtures with the full series.
  • Passed 71 PlatformUnicode tests, 16 Regex and RegExp tests, and all four fixture invocations under Clang 20 with AddressSanitizer and leak detection.

It follows the generation-time packing approach discussed in the earlier PR. The commit split shows the effects of one-byte pairs, the run token, and reservation separately. PR #2300

Summary:
Keep the range deltas and block resets introduced in d41288d. Encode
short gap and length pairs as one byte and use a tagged fallback for
larger values. Generate the bytes and offsets in genUnicodeTable.py
and decode them when constructing regex character sets.

The generated Unicode 17.0 range pool shrinks from 39,660 to 29,810
bytes. The decoder preserves aliases, shared references and range
boundaries without allocating a decoded cache.

Test Plan:
Regenerate the pool and references from the 13 pinned Unicode inputs
and independently compare the decoded endpoints of all 356 references
with upstream. Check output and bytecode equality for both Unicode
regex fixtures.
Summary:
Reserve one short-pair tag for runs of singletons separated by one code
point. Emit it only when it reduces the encoded size. Limit expansion
to the active reference length so a reference can consume a prefix of
a shared block. The generated range pool falls from 29,810 to 25,819
bytes.

Test Plan:
Regenerate the pool and references, compare decoded endpoints with
upstream, and verify fixture output and bytecode equality. Compare
linked size and Unicode regex construction across ULEB128, pairs, and
pairs plus the run token on x86-64 and ARMv7 Thumb-2.
Use the generated range counts to reserve room before building an empty set. Reserve the exact complement count when inserting into an empty receiver. Existing nonempty sets can contain overlapping ranges, so leave their allocation policy unchanged.
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 30, 2026
@andre-rosa

Copy link
Copy Markdown
Author

@micleo2 @javache @avp, could you take a look at this follow-up to the ULEB128 change? The series shrinks the generated range pool by another 34.9%. I’d especially appreciate feedback on the one-byte encoding and the run token.

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

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant