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

Remove some clones and branches in hot PropertyDescriptor functions #4104

Merged
merged 1 commit into from
Dec 30, 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
2 changes: 1 addition & 1 deletion core/engine/src/object/internal_methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ pub(crate) fn validate_and_apply_property_descriptor(
if let Some((obj, key)) = obj_and_key {
// a. For each field of Desc that is present, set the corresponding attribute of the
// property named P of object O to the value of the field.
current.fill_with(&desc);
current.fill_with(desc);
obj.borrow_mut()
.properties
.insert_with_slot(key, current, slot);
Expand Down
72 changes: 48 additions & 24 deletions core/engine/src/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,25 +280,49 @@ impl PropertyDescriptor {
#[inline]
#[must_use]
pub fn into_accessor_defaulted(mut self) -> Self {
self.kind = DescriptorKind::Accessor {
get: self.get().cloned(),
set: self.set().cloned(),
};
PropertyDescriptorBuilder { inner: self }
.complete_with_defaults()
.build()
match &mut self.kind {
DescriptorKind::Accessor { set, get } => {
if set.is_none() {
*set = Some(JsValue::undefined());
}
if get.is_none() {
*get = Some(JsValue::undefined());
}
}
_ => {
self.kind = DescriptorKind::Accessor {
get: Some(JsValue::undefined()),
set: Some(JsValue::undefined()),
};
}
}
self.configurable = self.configurable.or(Some(false));
self.enumerable = self.enumerable.or(Some(false));
self
}

/// Creates a data property descriptor with default values.
#[must_use]
pub fn into_data_defaulted(mut self) -> Self {
self.kind = DescriptorKind::Data {
value: self.value().cloned(),
writable: self.writable(),
};
PropertyDescriptorBuilder { inner: self }
.complete_with_defaults()
.build()
match &mut self.kind {
DescriptorKind::Data { value, writable } => {
if value.is_none() {
*value = Some(JsValue::undefined());
}
if writable.is_none() {
*writable = Some(false);
}
}
_ => {
self.kind = DescriptorKind::Data {
value: Some(JsValue::undefined()),
writable: Some(false),
};
}
}
self.configurable = self.configurable.or(Some(false));
self.enumerable = self.enumerable.or(Some(false));
self
}

/// Creates an generic property descriptor with default values.
Expand All @@ -317,20 +341,20 @@ impl PropertyDescriptor {
///
/// Panics if the given `PropertyDescriptor` is not compatible with this one.
#[inline]
pub fn fill_with(&mut self, desc: &Self) {
match (&mut self.kind, &desc.kind) {
pub fn fill_with(&mut self, mut desc: Self) {
match (&mut self.kind, &mut desc.kind) {
(
DescriptorKind::Data { value, writable },
DescriptorKind::Data {
value: desc_value,
writable: desc_writable,
},
) => {
if let Some(desc_value) = desc_value {
*value = Some(desc_value.clone());
if desc_value.is_some() {
std::mem::swap(value, desc_value);
}
if let Some(desc_writable) = desc_writable {
*writable = Some(*desc_writable);
if desc_writable.is_some() {
std::mem::swap(writable, desc_writable);
}
}
(
Expand All @@ -340,11 +364,11 @@ impl PropertyDescriptor {
set: desc_set,
},
) => {
if let Some(desc_get) = desc_get {
*get = Some(desc_get.clone());
if desc_get.is_some() {
std::mem::swap(get, desc_get);
}
if let Some(desc_set) = desc_set {
*set = Some(desc_set.clone());
if desc_set.is_some() {
std::mem::swap(set, desc_set);
}
}
(_, DescriptorKind::Generic) => {}
Expand Down
Loading