Skip to content

Commit

Permalink
StringValue::eq without virtual call
Browse files Browse the repository at this point in the history
Summary: This does not have much effect here, but probably helps with D63615329 (this generates less code, so that diff is allowed to inline more code).

Reviewed By: JakobDegen

Differential Revision: D63602970

fbshipit-source-id: e5d0ba8d23e7e3301ec62531650a8054d609244b
  • Loading branch information
stepancheg authored and facebook-github-bot committed Sep 29, 2024
1 parent 93c72a1 commit 9714af1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions starlark/src/values/layout/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,30 @@ impl<'v, T: StarlarkValue<'v>> Serialize for FrozenValueTyped<'v, T> {
}
}

// Have to implement these manually to avoid the `T: PartialEq` bound
impl<'v, T: StarlarkValue<'v>> PartialEq for ValueTyped<'v, T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
// Poor man specialization.
if T::static_type_id() == StarlarkStr::static_type_id() {
// SAFETY: just checked type ids.
let (this, other) = unsafe {
(
StringValue::new_unchecked(self.0),
StringValue::new_unchecked(other.0),
)
};
this.0.ptr_eq(other.0) || StarlarkStr::eq(this.as_ref(), other.as_ref())
} else {
// Slow comparison with virtual call.
self.0 == other.0
}
}
}

impl<'v, T: StarlarkValue<'v>> Eq for ValueTyped<'v, T> {}

impl<'v, T: StarlarkValue<'v>> PartialEq for FrozenValueTyped<'v, T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
self.to_value_typed() == other.to_value_typed()
}
}

Expand Down

0 comments on commit 9714af1

Please sign in to comment.