Skip to content

Commit ad775c2

Browse files
committed
Align transition table and fit it in a single page
1. To reduce the cache footprint. 2. To avoid additional cost when access across pages.
1 parent 62ae19b commit ad775c2

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

library/core/src/str/validations.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ const ST_ACCEPT: u32 = OFFSETS[1];
166166
// See the end of `./solve_dfa.py`.
167167
const OFFSETS: [u32; STATE_CNT] = [0, 6, 16, 19, 1, 25, 11, 18, 24];
168168

169-
static TRANS_TABLE: [u32; 256] = {
169+
// Keep the whole table in a single page.
170+
#[repr(align(1024))]
171+
struct TransitionTable([u32; 256]);
172+
173+
static TRANS_TABLE: TransitionTable = {
170174
let mut table = [0u32; 256];
171175
let mut b = 0;
172176
while b < 256 {
@@ -187,12 +191,12 @@ static TRANS_TABLE: [u32; 256] = {
187191
};
188192
b += 1;
189193
}
190-
table
194+
TransitionTable(table)
191195
};
192196

193197
#[inline(always)]
194198
const fn next_state(st: u32, byte: u8) -> u32 {
195-
TRANS_TABLE[byte as usize].wrapping_shr(st)
199+
TRANS_TABLE.0[byte as usize].wrapping_shr(st)
196200
}
197201

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

0 commit comments

Comments
 (0)