Skip to content

Truly independent web browser

LibJS: Don't cache own property additions that make a dictionary

Shannon Booth082b244acbc1
diff --git a/Libraries/LibJS/Rust/src/bytecode/property_access.rs b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
index 28136eac905..23a14037c69 100644
--- a/Libraries/LibJS/Rust/src/bytecode/property_access.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
@@ -634,12 +634,6 @@ pub fn put_by_property_key(
continue;
}
- if cached_shape.is_dictionary()
- && object.shape().dictionary_generation() != cache.shape_dictionary_generation
- {
- continue;
- }
-
// The cache is invalid if the prototype chain has been mutated, since such a mutation could have added a setter for the property.
if cache
.prototype_chain_validity
@@ -688,9 +682,6 @@ pub fn put_by_property_key(
if let Some(prototype) = cacheable_metadata.prototype {
cache.prototype_chain_validity = prototype.shape().prototype_chain_validity();
}
- if object.shape().is_dictionary() {
- cache.shape_dictionary_generation = object.shape().dictionary_generation();
- }
});
}
@@ -776,11 +767,6 @@ pub fn put_by_property_key(
let Some(cached_shape) = cache.shape else {
continue;
};
- if cached_shape.is_dictionary()
- && object.shape().dictionary_generation() != cache.shape_dictionary_generation
- {
- continue;
- }
object.unsafe_set_shape(cached_shape);
object.initialize_direct(cache.property_offset, value);
return Ok(());
@@ -798,15 +784,13 @@ pub fn put_by_property_key(
if let Some(caches) = caches
&& from_shape != object.shape()
+ && !object.shape().is_dictionary()
{
caches.update(PropertyLookupCacheEntryType::AddOwnProperty, |cache| {
cache.key = cache_key;
cache.from_shape = Some(from_shape);
cache.shape = Some(object.shape());
cache.property_offset = object.shape().lookup(name).expect("the property was just added").offset;
- if object.shape().is_dictionary() {
- cache.shape_dictionary_generation = object.shape().dictionary_generation();
- }
});
}
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
index a15c7ee2c34..7eaf5a03a49 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
@@ -561,8 +561,6 @@ fn put_by_value_with_keyed_store_cache(
let new_shape = entry.new_shape.expect("additions have a new shape");
if property_addition_is_cacheable(vm, &object, property_key)
&& object.internal_is_extensible(vm)?
- && (!new_shape.is_dictionary()
- || shape.dictionary_generation() == entry.shape_dictionary_generation)
&& entry
.prototype_chain_validity
.is_none_or(|validity| validity.is_valid())
@@ -600,7 +598,6 @@ fn put_by_value_with_keyed_store_cache(
PropertyLookupCacheEntryType::AddOwnProperty if entry.from_shape == Some(shape) && entry.shape.is_some() => {
lookup.entry_type = PropertyLookupCacheEntryType::AddOwnProperty;
lookup.property_offset = entry.property_offset;
- lookup.shape_dictionary_generation = entry.shape_dictionary_generation;
lookup.prototype_chain_validity = entry.prototype_chain_validity;
lookup.new_shape = entry.shape;
}
@@ -1497,11 +1494,6 @@ pub fn try_put_by_id_cache(vm: &Vm, instruction: &op::PutById, values: &op::PutB
if !object.extensible() {
continue;
}
- if cached_shape.is_dictionary()
- && object.shape().dictionary_generation() != entry.shape_dictionary_generation.get()
- {
- continue;
- }
if entry
.prototype_chain_validity
.get()
@@ -1623,11 +1615,6 @@ pub fn try_put_by_value_cache(vm: &Vm, instruction: &op::PutByValue, values: &op
if !object.extensible() {
continue;
}
- if cached_shape.is_dictionary()
- && object.shape().dictionary_generation() != entry.shape_dictionary_generation.get()
- {
- continue;
- }
if entry
.prototype_chain_validity
.get()
diff --git a/Tests/LibJS/Runtime/object-literal-caching.js b/Tests/LibJS/Runtime/object-literal-caching.js
index 7d71aedabd4..319230f10ef 100644
--- a/Tests/LibJS/Runtime/object-literal-caching.js
+++ b/Tests/LibJS/Runtime/object-literal-caching.js
@@ -491,3 +491,18 @@ describe("cache invalidation scenarios", () => {
expect(obj[sym2]).toBe(3);
});
});
+
+describe("object literals that become dictionaries", () => {
+ test("do not get the dictionary shape of an earlier object", () => {
+ // NB: The spread makes the literal add its named properties with PutById.
+ const properties = Array.from({ length: 70 }, (_, i) => `p${i}: ${i}`);
+ const make = new Function(`return { ...null, ${properties.join(", ")} };`);
+ const first = make();
+ const second = make();
+ expect(haveSameShape(first, second)).toBeFalse();
+
+ first.extra = "extra";
+ expect(second.extra).toBeUndefined();
+ expect(Object.keys(second)).toHaveLength(70);
+ });
+});