Skip to content

Commit

Permalink
Align transition table and fit it in a single page
Browse files Browse the repository at this point in the history
1. To reduce the cache footprint.
2. To avoid additional cost when access across pages.
  • Loading branch information
oxalica committed Feb 9, 2025
1 parent 16d3192 commit 9cc648b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions library/core/src/str/validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ const ST_ACCEPT: u32 = OFFSETS[1];
// See the end of `./solve_dfa.py`.
const OFFSETS: [u32; STATE_CNT] = [0, 6, 16, 19, 1, 25, 11, 18, 24];

static TRANS_TABLE: [u32; 256] = {
// Keep the whole table in a single page.
#[repr(align(1024))]
struct TransitionTable([u32; 256]);

static TRANS_TABLE: TransitionTable = {
let mut table = [0u32; 256];
let mut b = 0;
while b < 256 {
Expand All @@ -187,12 +191,12 @@ static TRANS_TABLE: [u32; 256] = {
};
b += 1;
}
table
TransitionTable(table)
};

#[inline(always)]
const fn next_state(st: u32, byte: u8) -> u32 {
TRANS_TABLE[byte as usize].wrapping_shr(st)
TRANS_TABLE.0[byte as usize].wrapping_shr(st)
}

/// Check if `byte` is a valid UTF-8 first byte, assuming it must be a valid first or
Expand Down

0 comments on commit 9cc648b

Please sign in to comment.