Skip to content

Commit

Permalink
perf(span): compare Spans as single u64s
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jan 6, 2025
1 parent 4680154 commit 9235279
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions crates/oxc_span/src/span/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,19 @@ impl From<Span> for LabeledSpan {
}
}

// On 64-bit platforms, compare `Span`s as single `u64`s, which is faster when used with `&Span` refs.
// https://godbolt.org/z/P4Wvf7nxT
impl PartialEq for Span {
#[inline]
fn eq(&self, other: &Self) -> bool {
if cfg!(target_pointer_width = "64") {
self.as_u64() == other.as_u64()
} else {
self.start == other.start && self.end == other.end
}
}
}

// Skip hashing `_align` field.
// On 64-bit platforms, hash `Span` as a single `u64`, which is faster with `FxHash`.
// https://godbolt.org/z/4q36xrWG8
Expand Down Expand Up @@ -511,7 +524,13 @@ mod test {
fn test_eq() {
assert_eq!(Span::new(0, 0), Span::new(0, 0));
assert_eq!(Span::new(0, 1), Span::new(0, 1));
assert_eq!(Span::new(1, 5), Span::new(1, 5));

assert_ne!(Span::new(0, 0), Span::new(0, 1));
assert_ne!(Span::new(1, 5), Span::new(0, 5));
assert_ne!(Span::new(1, 5), Span::new(2, 5));
assert_ne!(Span::new(1, 5), Span::new(1, 4));
assert_ne!(Span::new(1, 5), Span::new(1, 6));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_span/src/span/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use super::PointerAlign;
/// [`expand`]: Span::expand
/// [`shrink`]: Span::shrink
#[ast(visit)]
#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Default, Clone, Copy, Eq, PartialOrd, Ord)]
#[generate_derive(ESTree)]
#[estree(no_type, always_flatten)]
pub struct Span {
Expand Down

0 comments on commit 9235279

Please sign in to comment.