Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster Ord when Eq #328

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,18 @@ impl Drop for Identifier {
}
}

impl Identifier {
pub(crate) fn ptr_eq(&self, rhs: &Self) -> bool {
self.head == rhs.head && self.tail == rhs.tail
}
}

impl PartialEq for Identifier {
fn eq(&self, rhs: &Self) -> bool {
if self.is_empty_or_inline() {
if self.ptr_eq(rhs) {
// Fast path (most common)
self.head == rhs.head && self.tail == rhs.tail
} else if rhs.is_empty_or_inline() {
true
} else if self.is_empty_or_inline() || rhs.is_empty_or_inline() {
false
} else {
// SAFETY: both reprs are in the heap allocated representation.
Expand Down
7 changes: 6 additions & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ impl PartialOrd for BuildMetadata {

impl Ord for Prerelease {
fn cmp(&self, rhs: &Self) -> Ordering {
if self.identifier.ptr_eq(&rhs.identifier) {
return Ordering::Equal;
}
match self.is_empty() {
true if rhs.is_empty() => return Ordering::Equal,
// A real release compares greater than prerelease.
true => return Ordering::Greater,
// Prerelease compares less than the real release.
Expand Down Expand Up @@ -105,6 +107,9 @@ impl Ord for Prerelease {

impl Ord for BuildMetadata {
fn cmp(&self, rhs: &Self) -> Ordering {
if self.identifier.ptr_eq(&rhs.identifier) {
return Ordering::Equal;
}
let lhs = self.as_str().split('.');
let mut rhs = rhs.as_str().split('.');

Expand Down
Loading