index folding#124
Open
aneubeck wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR ports the “index folding” operation into the casefold crate, reusing the existing paged-bitmap/run-table approach and the same fast ASCII pre-pass to provide a compact 1-byte-per-character fold suitable for indexing.
Changes:
- Adds
index_fold(s: String) -> Vec<u8>that simple-folds and then collapses each character to a single byte (lossy, non-UTF-8) without allocating a second buffer. - Refactors
simple_foldinto its own module and shares atest_support::reference()parser across fold implementations. - Extends the generated table with a parallel
INDEX_DELTAside table and updates docs/benchmarks accordingly.
Show a summary per file
| File | Description |
|---|---|
| crates/casefold/src/simple_fold.rs | New module containing the optimized simple UTF-8 case-fold implementation and its tests. |
| crates/casefold/src/index_fold.rs | New index-fold implementation (1 byte per character) plus correctness tests against the reference map. |
| crates/casefold/src/lib.rs | Wires in the new modules/exports, adjusts bitmap popcount helper signature, and adds shared test support. |
| crates/casefold/build.rs | Emits the new INDEX_DELTA side table and updates size accounting. |
| crates/casefold/README.md | Documents index_fold and updates the table layout/size discussion. |
| crates/casefold/benchmarks/conversion.rs | Adds a Criterion benchmark variant for index_fold and updates benchmark descriptions. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 6/6 changed files
- Comments generated: 4
Comment on lines
+30
to
+33
| /// Like [`simple_fold`](crate::simple_fold), characters are never fully decoded | ||
| /// and the fold needs no UTF-8 reconstruction: the page coordinates come from | ||
| /// the lead/continuation bytes, and on a fold hit the folded low 7 bits are | ||
| /// `(cp & 0x7F)` plus the run's 7-bit `INDEX_DELTA`, masked back to 7 bits. |
Comment on lines
+197
to
+201
| #[test] | ||
| fn index_fold_matches_reference_map_exhaustive() { | ||
| // Drive every assigned code point through the byte-oriented index path | ||
| // and cross-check against the reference fold map. | ||
| let r = reference(); |
Comment on lines
+311
to
+315
| // collapses each code point to `cp & 0x7F`; by modular arithmetic the folded | ||
| // low-7-bit value is `((cp & 0x7F) + (delta & 0x7F)) & 0x7F`, so storing the | ||
| // code-point delta reduced mod 128 lets `index_fold` derive the folded index | ||
| // byte with one `wrapping_add` + mask — no UTF-8 reconstruction. The high | ||
| // bit is added unconditionally at write time, so only 7 bits are stored. |
Comment on lines
+160
to
+164
| // only folds that grow are U+023A/U+023E (2→3 bytes), so every 2 | ||
| // input bytes yield ≤3 output bytes; `+ 4` covers the 4-byte | ||
| // over-store of the final character. The non-zero capacity makes | ||
| // `out.as_mut_ptr()` non-null, so `dst` is non-null from here on. | ||
| out = Vec::with_capacity(bytes.len() + bytes.len() / 2 + 4); |
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.
Port our index fold operation as well using the same speedup tricks.