Skip to content

Commit

Permalink
perf(span): compare Spans as single u64s (#8300)
Browse files Browse the repository at this point in the history
#8298 made `Span` aligned on 8 on 64-bit platforms. Utilize this property to compare `Span`s as a single `u64` instead of 2 x `u32`s. This removes some really weird assembly which compiler otherwise produces, using expensive SIMD operations for a simple comparison: https://godbolt.org/z/sEf9MGvsr

Note: This only affects comparing `&Span`s. Makes no difference when comparing owned `Span`s.
  • Loading branch information
overlookmotel committed Jan 18, 2025
1 parent a43560c commit 63eb298
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 32 additions & 1 deletion crates/oxc_span/src/span/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ impl Span {
/// assert!(!Span::new(0, 5).is_unspanned());
/// assert!(!Span::new(5, 5).is_unspanned());
/// ```
#[inline]
pub const fn is_unspanned(self) -> bool {
self.start == SPAN.start && self.end == SPAN.end
self.const_eq(SPAN)
}

/// Check if this [`Span`] contains another [`Span`].
Expand Down Expand Up @@ -360,6 +361,21 @@ impl Span {
((self.start as u64) << 32) | (self.end as u64)
}
}

/// Compare two [`Span`]s.
///
/// Same as `PartialEq::eq`, but a const function, and takes owned `Span`s.
//
// `#[inline(always)]` because want to make sure this is inlined into `PartialEq::eq`.
#[expect(clippy::inline_always)]
#[inline(always)]
const fn const_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
}
}
}

impl Index<Span> for str {
Expand Down Expand Up @@ -397,6 +413,15 @@ 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/sEf9MGvsr
impl PartialEq for Span {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.const_eq(*other)
}
}

// Skip hashing `_align` field.
// On 64-bit platforms, hash `Span` as a single `u64`, which is faster with `FxHash`.
// https://godbolt.org/z/4fbvcsTxM
Expand Down Expand Up @@ -511,7 +536,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 63eb298

Please sign in to comment.