Skip to content

Truly independent web browser

LibJS: Avoid slow paths added by the smaller classes

Andreas Kling60be3196ce57
diff --git a/Libraries/LibJS/Rust/src/gc/gc_ref_cell.rs b/Libraries/LibJS/Rust/src/gc/gc_ref_cell.rs
index 3f9583bc016..b011ea234ed 100644
--- a/Libraries/LibJS/Rust/src/gc/gc_ref_cell.rs
+++ b/Libraries/LibJS/Rust/src/gc/gc_ref_cell.rs
@@ -25,6 +25,10 @@ impl<T> GcRefCell<T> {
self.0.borrow()
}
+ pub fn as_ptr(&self) -> *mut T {
+ self.0.as_ptr()
+ }
+
pub fn borrow_mut<'a>(&'a self, vm: &'a Vm) -> GcRefMut<'a, T> {
let defer_gc = DeferGc::new(vm);
GcRefMut {
diff --git a/Libraries/LibJS/Rust/src/runtime/descriptor_array.rs b/Libraries/LibJS/Rust/src/runtime/descriptor_array.rs
index 3f864589727..18e19ac4ac3 100644
--- a/Libraries/LibJS/Rust/src/runtime/descriptor_array.rs
+++ b/Libraries/LibJS/Rust/src/runtime/descriptor_array.rs
@@ -180,14 +180,21 @@ impl DescriptorArray {
}
/// Whether the property with the given enum index has the key and the attributes.
+ ///
+ /// OPTIMIZATION: Adding a property that some object of the shape already got comes through here, so it takes no
+ /// borrows of the cells.
+ #[inline(always)]
pub fn has_property_at_enum_index(
&self,
enum_index: u32,
property_key: &PropertyKey,
attributes: PropertyAttributes,
) -> bool {
- let entries = self.entries.borrow();
- let entry = &entries[self.entry_indices_by_enum_index.borrow()[enum_index as usize] as usize];
+ // SAFETY: The borrows that change the entries and the indices end within the methods of this array, which run
+ // no other code that could get here.
+ let (entries, entry_indices) =
+ unsafe { (&*self.entries.as_ptr(), &*self.entry_indices_by_enum_index.as_ptr()) };
+ let entry = &entries[entry_indices[enum_index as usize] as usize];
entry.property_key == *property_key && entry.metadata().attributes == attributes
}
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index bc204a3a1e9..a7fa92a8d77 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -3728,9 +3728,14 @@ impl Object {
}
}
+ /// NB: Inline storage starts at one of two offsets, within the object. Other storage is outside of it.
+ #[inline(always)]
pub(crate) fn named_storage_is_inline(&self) -> bool {
- core::ptr::eq(self.named_properties.get(), self.inline_named_storage_pointer())
- || self.named_storage_overlaps_indexed_elements()
+ self.named_properties
+ .get()
+ .addr()
+ .wrapping_sub(core::ptr::from_ref(self).addr())
+ <= core::mem::offset_of!(Object, inline_named_storage)
}
/// Whether the inline named storage of this plain object starts at the indexed elements word.
@@ -3796,6 +3801,7 @@ impl Object {
}
}
+ #[inline(always)]
fn ensure_named_storage_capacity(&self, needed: u32) {
let old_capacity = self.named_storage_capacity();
if needed <= old_capacity {
@@ -3806,6 +3812,7 @@ impl Object {
/// NB: The capacity of the storage never shrinks, which JIT code that added properties to an object it allocated
/// relies on.
+ #[inline(never)]
fn move_named_storage_out_of_line(&self, old_capacity: u32, new_capacity: u32) {
let overlapped_indexed_elements = self.named_storage_overlaps_indexed_elements();
diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rs
index de9574c475b..b7c960c8dd0 100644
--- a/Libraries/LibJS/Rust/src/runtime/shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs
@@ -699,6 +699,17 @@ impl Shape {
attributes: PropertyAttributes,
) -> Gc<Shape> {
self.mark_unstable(vm);
+ // OPTIMIZATION: Most shapes have one transition, which most additions of a property take, and which needs no
+ // key to look at.
+ if !self.is_prototype_shape()
+ && let ShapeStorageState::PutTransition(Some(target)) = self.storage.state()
+ && target
+ .descriptors()
+ .expect("the target of a transition has descriptors")
+ .has_property_at_enum_index(self.property_count(), property_key, attributes)
+ {
+ return target;
+ }
let key = TransitionKey {
property_key: property_key.clone(),
attributes,
diff --git a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
index 42a2845c9ba..25a7f72f495 100644
--- a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
@@ -922,9 +922,11 @@ impl SharedFunctionInstanceData {
/// them through the environment. Otherwise a declarative environment does for the function's bindings, just as no
/// environment at all does for a function without any.
pub fn function_environment_has_this_binding(&self) -> bool {
- self.contains_direct_call_to_eval()
- || self.is_module_wrapper()
- || (!self.is_arrow_function() && self.this_value_needs_environment_resolution())
+ let metadata = self.asm_call_metadata.get();
+ metadata & (asm_call_metadata::CONTAINS_DIRECT_CALL_TO_EVAL | asm_call_metadata::IS_MODULE_WRAPPER) != 0
+ || metadata
+ & (asm_call_metadata::IS_ARROW_FUNCTION | asm_call_metadata::THIS_VALUE_NEEDS_ENVIRONMENT_RESOLUTION)
+ == asm_call_metadata::THIS_VALUE_NEEDS_ENVIRONMENT_RESOLUTION
}
pub fn function_environment_needed(&self) -> bool {