Skip to content

index folding#124

Open
aneubeck wants to merge 3 commits into
mainfrom
aneubeck/casefold
Open

index folding#124
aneubeck wants to merge 3 commits into
mainfrom
aneubeck/casefold

Conversation

@aneubeck

Copy link
Copy Markdown
Collaborator

Port our index fold operation as well using the same speedup tricks.

@aneubeck aneubeck requested a review from a team as a code owner June 10, 2026 10:54
Copilot AI review requested due to automatic review settings June 10, 2026 10:54
Comment thread crates/casefold/src/simple_fold.rs Dismissed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_fold into its own module and shares a test_support::reference() parser across fold implementations.
  • Extends the generated table with a parallel INDEX_DELTA side 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 thread crates/casefold/build.rs
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);
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.

3 participants