Skip to content

Commit d8402c2

Browse files
committed
assert to increase performance
1 parent 790a483 commit d8402c2

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

build/ucd_generator/generators.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ pub fn generate_enum_table(code_dir : &std::path::Path, name : &str, enum_values
9393

9494
write!(fd, " let column_offset = (index * {}_CHUNK_SIZE + code_point_lo) * {}_COLUMN_BITS;\n", upper_name, upper_name)?;
9595
write!(fd, " let column_byte_offset = column_offset / 8;\n")?;
96-
write!(fd, " let column_bit_offset = column_offset % 8;\n")?;
96+
write!(fd, " let column_bit_offset = column_offset % 8;\n\n")?;
97+
write!(fd, " // Explicitly assert to replace a double bound check with single bound check.\n")?;
98+
write!(fd, " assert!(column_byte_offset + 1 < {}_COLUMN.len());\n", upper_name)?;
9799
write!(fd, " let mut value: usize = 0;\n")?;
98100
let column_bytes_to_read = (column_bits + 7) / 8 + 1;
99101
for i in 0..column_bytes_to_read {

src/east_asian_width.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ pub const fn get_east_asian_width(code_point: char) -> EastAsianWidth
307307
let column_offset = (index * EAST_ASIAN_WIDTH_CHUNK_SIZE + code_point_lo) * EAST_ASIAN_WIDTH_COLUMN_BITS;
308308
let column_byte_offset = column_offset / 8;
309309
let column_bit_offset = column_offset % 8;
310+
311+
// Explicitly assert to replace a double bound check with single bound check.
312+
assert!(column_byte_offset + 1 < EAST_ASIAN_WIDTH_COLUMN.len());
310313
let mut value: usize = 0;
311314
value |= (EAST_ASIAN_WIDTH_COLUMN[column_byte_offset + 0] as usize) << 0;
312315
value |= (EAST_ASIAN_WIDTH_COLUMN[column_byte_offset + 1] as usize) << 8;

0 commit comments

Comments
 (0)