Skip to content

Commit e2ecedd

Browse files
authored
perf(textindex)!: use u32 where appropriate (#231)
1 parent 3bd1b9f commit e2ecedd

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

crates/lintspec-core/src/parser/slang.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,9 @@ impl From<SlangTextIndex> for TextIndex {
903903
fn from(value: SlangTextIndex) -> Self {
904904
Self {
905905
utf8: value.utf8,
906-
line: value.line,
907-
col_utf32: value.column,
906+
line: u32::try_from(value.line).or_panic("slang line number is too large for u32"),
907+
col_utf32: u32::try_from(value.column)
908+
.or_panic("slang column number is too large for u32"),
908909
// col_utf8 and col_utf16 are filled by the completion step
909910
col_utf8: 0,
910911
col_utf16: 0,
@@ -991,19 +992,20 @@ mod tests {
991992
};
992993
}
993994

995+
#[expect(clippy::cast_possible_truncation)]
994996
fn single_line_textrange(range: Range<usize>) -> TextRange {
995997
TextIndex {
996998
utf8: range.start,
997999
line: 0,
998-
col_utf8: range.start,
999-
col_utf16: range.start,
1000-
col_utf32: range.start,
1000+
col_utf8: range.start as u32,
1001+
col_utf16: range.start as u32,
1002+
col_utf32: range.start as u32,
10011003
}..TextIndex {
10021004
utf8: range.end,
10031005
line: 0,
1004-
col_utf8: range.end,
1005-
col_utf16: range.end,
1006-
col_utf32: range.end,
1006+
col_utf8: range.end as u32,
1007+
col_utf16: range.end as u32,
1008+
col_utf32: range.end as u32,
10071009
}
10081010
}
10091011

crates/lintspec-core/src/textindex.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ pub struct TextIndex {
2222
/// Byte offset from the start of the document
2323
pub utf8: usize,
2424
/// Line number (0-based)
25-
pub line: usize,
25+
pub line: u32,
2626
/// Column offset in bytes (0-based)
27-
pub col_utf8: usize,
27+
pub col_utf8: u32,
2828
/// Column offset in UTF-16 code units (0-based)
29-
pub col_utf16: usize,
29+
pub col_utf16: u32,
3030
/// Column offset in UTF-32 code points (0-based)
3131
///
3232
/// This is the same as Rust's [`char`] count.
33-
pub col_utf32: usize,
33+
pub col_utf32: u32,
3434
}
3535

3636
impl TextIndex {
@@ -81,9 +81,10 @@ impl TextIndex {
8181
self.col_utf16 = 0;
8282
self.col_utf32 = 0;
8383
}
84+
#[expect(clippy::cast_possible_truncation)]
8485
_ => {
85-
self.col_utf8 += bytes;
86-
self.col_utf16 += c.len_utf16();
86+
self.col_utf8 += bytes as u32;
87+
self.col_utf16 += c.len_utf16() as u32;
8788
self.col_utf32 += 1;
8889
}
8990
}
@@ -103,9 +104,10 @@ impl TextIndex {
103104
self.col_utf16 = 0;
104105
self.col_utf32 = 0;
105106
}
107+
#[expect(clippy::cast_possible_truncation)]
106108
_ => {
107-
self.col_utf8 += bytes;
108-
self.col_utf16 += c.len_utf16();
109+
self.col_utf8 += bytes as u32;
110+
self.col_utf16 += c.len_utf16() as u32;
109111
self.col_utf32 += 1;
110112
}
111113
}
@@ -114,7 +116,7 @@ impl TextIndex {
114116
/// Advance this index according to the `Advance` parameter.
115117
#[inline]
116118
fn advance_by(&mut self, advance: &Advance) {
117-
self.utf8 += advance.bytes;
119+
self.utf8 += advance.bytes as usize;
118120
self.line += advance.lines;
119121
// ASCII-only path: 1 byte = 1 UTF-16 code unit = 1 code point
120122
match advance.column {
@@ -153,15 +155,15 @@ impl Ord for TextIndex {
153155
/// The type of operation to perform on the `TextIndex`'s `column` field
154156
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
155157
enum Column {
156-
Increment(usize),
157-
Set(usize),
158+
Increment(u32),
159+
Set(u32),
158160
}
159161

160162
/// An update to perform on `TextIndex` after scanning a chunk of the input text
161163
#[derive(Debug, Clone, PartialEq, Eq)]
162164
struct Advance {
163-
bytes: usize,
164-
lines: usize,
165+
bytes: u32,
166+
lines: u32,
165167
column: Column,
166168
}
167169

@@ -233,18 +235,17 @@ impl From<[i8; SIMD_LANES]> for Advance {
233235
/// increment the column count) from the number of bytes on the last line which we calculated before. This number
234236
/// is the new value of the `column` field of `TextIndex`.
235237
#[inline]
238+
#[expect(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
236239
fn from(chunk: [i8; SIMD_LANES]) -> Self {
237240
let bytes = i8x32::new(chunk);
238241
let nonascii_mask = bytes.simd_lt(i8x32::ZERO).to_bitmask();
239-
#[expect(clippy::cast_possible_wrap)]
240242
let lf_bytes = i8x32::splat(b'\n' as i8);
241243
let mut lf_mask = bytes.simd_eq(lf_bytes).to_bitmask();
242-
#[expect(clippy::cast_possible_wrap)]
243244
let cr_bytes = i8x32::splat(b'\r' as i8);
244245
let mut cr_mask = bytes.simd_eq(cr_bytes).to_bitmask();
245246

246247
// ignore non-ASCII characters at the end
247-
let n_ascii = nonascii_mask.trailing_zeros() as usize;
248+
let n_ascii = nonascii_mask.trailing_zeros();
248249
if n_ascii == 0 {
249250
// there are not ASCII bytes at the start of the chunk
250251
return Advance {
@@ -253,15 +254,15 @@ impl From<[i8; SIMD_LANES]> for Advance {
253254
lines: 0,
254255
};
255256
}
256-
let shift = SIMD_LANES - n_ascii; // this is < SIMD_LANES
257+
let shift = SIMD_LANES as u32 - n_ascii; // this is < SIMD_LANES
257258
lf_mask <<= shift;
258259
cr_mask <<= shift;
259260

260261
let mut n_lines = 0;
261262
let column = if lf_mask > 0 {
262263
// the chunk contains multiple lines, we ignore everything but the last line
263-
n_lines = lf_mask.count_ones() as usize;
264-
let n_last_line = lf_mask.leading_zeros() as usize;
264+
n_lines = lf_mask.count_ones();
265+
let n_last_line = lf_mask.leading_zeros();
265266
if n_last_line == 0 {
266267
// edge case where the last byte is \n
267268
return Advance {
@@ -271,10 +272,10 @@ impl From<[i8; SIMD_LANES]> for Advance {
271272
};
272273
}
273274
// we ignore the \r in the last line for the columns count
274-
cr_mask >>= SIMD_LANES - n_last_line; // the shift amount is < SIMD_LANES
275-
Column::Set(n_last_line - cr_mask.count_ones() as usize)
275+
cr_mask >>= SIMD_LANES as u32 - n_last_line; // the shift amount is < SIMD_LANES
276+
Column::Set(n_last_line - cr_mask.count_ones())
276277
} else {
277-
Column::Increment(n_ascii - cr_mask.count_ones() as usize)
278+
Column::Increment(n_ascii - cr_mask.count_ones())
278279
};
279280
Advance {
280281
bytes: n_ascii,

0 commit comments

Comments
 (0)