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

feat: optimize class equality check #140

Merged
merged 1 commit into from
Nov 20, 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
5 changes: 5 additions & 0 deletions ristretto_classloader/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ impl Class {

impl PartialEq for Class {
fn eq(&self, other: &Self) -> bool {
// Optimization for the case where the two classes are the same instance.
if std::ptr::eq(self, other) {
return true;
}

self.name == other.name
&& self.class_file == other.class_file
&& *self.parent.read().expect("parent") == *other.parent.read().expect("parent")
Expand Down
13 changes: 12 additions & 1 deletion ristretto_classloader/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::Arc;
const JAVA_8: Version = Version::Java8 { minor: 0 };

/// Represents an object in the Ristretto VM.
#[derive(Clone, PartialEq)]
#[derive(Clone)]
pub struct Object {
class: Arc<Class>,
fields: HashMap<String, Field>,
Expand Down Expand Up @@ -186,6 +186,17 @@ impl Display for Object {
}
}

impl PartialEq for Object {
fn eq(&self, other: &Self) -> bool {
// Compare the references by pointer to determine if they are the same object in
// order to avoid infinite recursion
if std::ptr::eq(self, other) {
return true;
}
self.class == other.class && self.fields == other.fields
}
}

impl TryInto<bool> for Object {
type Error = crate::Error;

Expand Down
29 changes: 1 addition & 28 deletions ristretto_classloader/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt::Display;
use std::sync::Arc;

/// Represents a reference to an object in the Ristretto VM.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum Reference {
ByteArray(ConcurrentVec<i8>),
CharArray(ConcurrentVec<u16>),
Expand Down Expand Up @@ -180,33 +180,6 @@ impl Display for Reference {
}
}

impl PartialEq for Reference {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Reference::ByteArray(a), Reference::ByteArray(b)) => a == b,
(Reference::CharArray(a), Reference::CharArray(b)) => a == b,
(Reference::ShortArray(a), Reference::ShortArray(b)) => a == b,
(Reference::IntArray(a), Reference::IntArray(b)) => a == b,
(Reference::LongArray(a), Reference::LongArray(b)) => a == b,
(Reference::FloatArray(a), Reference::FloatArray(b)) => a == b,
(Reference::DoubleArray(a), Reference::DoubleArray(b)) => a == b,
(Reference::Array(a_class, a_array), Reference::Array(b_class, b_array)) => {
a_class.name() == b_class.name() && a_array == b_array
}
(Reference::Object(a), Reference::Object(b)) => {
// Compare the references by pointer to determine if they are the same object in
// order to avoid infinite recursion
if std::ptr::eq(a, b) {
true
} else {
a == b
}
}
_ => false,
}
}
}

impl From<Vec<bool>> for Reference {
fn from(value: Vec<bool>) -> Self {
let value: Vec<i8> = value.into_iter().map(i8::from).collect();
Expand Down