Skip to content

Truly independent web browser

LibJS: Compare a shape and its dictionary generation as one value

Shannon Booth5811610e6955
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 6ec7a917372..aa13747b83f 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -53,6 +53,7 @@ use crate::runtime::big_int::{BigInt, SignedBigInteger};
use crate::runtime::environment_shape::{EnvironmentShape, EnvironmentShapeCache};
use crate::runtime::primitive_string::PrimitiveString;
use crate::runtime::property_key::PropertyKey;
+use crate::runtime::shape::ShapeKey;
use crate::runtime::shared_function_instance_data::SharedFunctionInstanceData;
use crate::runtime::value::number_to_string;
use crate::source_code::SourceCode;
@@ -66,10 +67,9 @@ use libjs_runtime_macros::Trace;
pub struct PropertyLookupCacheEntryData {
pub entry_type: PropertyLookupCacheEntryType,
pub property_offset: u32,
- pub shape_dictionary_generation: u32,
pub writes_data_property: bool,
pub new_shape: Option<Gc<Shape>>,
- pub shape: Option<Gc<Shape>>,
+ pub shape: Option<ShapeKey>,
pub prototype: Option<Gc<Object>>,
pub prototype_chain_validity: Option<Gc<WatchpointSet>>,
/// See PropertyLookupCacheEntry::key.
@@ -82,7 +82,6 @@ impl Default for PropertyLookupCacheEntryData {
Self {
entry_type: PropertyLookupCacheEntryType::Empty,
property_offset: 0,
- shape_dictionary_generation: 0,
writes_data_property: false,
new_shape: None,
shape: None,
@@ -105,17 +104,17 @@ impl PropertyLookupCacheEntryData {
return false;
}
+ // NB: An entry for an earlier dictionary generation of the shape is replaced too.
+ let same_shape = self.shape.map(|key| key.shape) == other.shape.map(|key| key.shape);
match self.entry_type {
- PropertyLookupCacheEntryType::AddOwnProperty => {
- self.shape == other.shape && self.new_shape == other.new_shape
- }
+ PropertyLookupCacheEntryType::AddOwnProperty => same_shape && self.new_shape == other.new_shape,
PropertyLookupCacheEntryType::ChangeOwnProperty
| PropertyLookupCacheEntryType::GetOwnProperty
| PropertyLookupCacheEntryType::GetMissingProperty
- | PropertyLookupCacheEntryType::MissingOwnProperty => self.shape == other.shape,
+ | PropertyLookupCacheEntryType::MissingOwnProperty => same_shape,
PropertyLookupCacheEntryType::ChangePropertyInPrototypeChain
| PropertyLookupCacheEntryType::GetPropertyInPrototypeChain => {
- self.shape == other.shape && self.prototype == other.prototype
+ same_shape && self.prototype == other.prototype
}
PropertyLookupCacheEntryType::Empty => unreachable!("empty entries have no cache key"),
}
@@ -124,7 +123,7 @@ impl PropertyLookupCacheEntryData {
fn has_dead_cell(&self, sweep: &Sweep<'_>) -> bool {
(self.key != 0 && sweep.is_dead(Value::from_bits(self.key).as_cell()))
|| self.new_shape.is_some_and(|shape| sweep.is_dead(shape))
- || self.shape.is_some_and(|shape| sweep.is_dead(shape))
+ || self.shape.is_some_and(|key| sweep.is_dead(key.shape))
|| self.prototype.is_some_and(|prototype| sweep.is_dead(prototype))
|| self
.prototype_chain_validity
@@ -145,10 +144,10 @@ impl PropertyLookupCacheEntry {
Self {
entry_type: Cell::new(data.entry_type),
property_offset: Cell::new(data.property_offset),
- shape_dictionary_generation: Cell::new(data.shape_dictionary_generation),
+ shape_dictionary_generation: Cell::new(data.shape.map_or(0, |key| key.dictionary_generation)),
writes_data_property: Cell::new(data.writes_data_property),
new_shape: Cell::new(data.new_shape),
- shape: Cell::new(data.shape),
+ shape: Cell::new(data.shape.map(|key| key.shape)),
prototype: Cell::new(data.prototype),
prototype_chain_validity: Cell::new(data.prototype_chain_validity),
key: Cell::new(data.key),
@@ -160,10 +159,9 @@ impl PropertyLookupCacheEntry {
PropertyLookupCacheEntryData {
entry_type: self.entry_type.get(),
property_offset: self.property_offset.get(),
- shape_dictionary_generation: self.shape_dictionary_generation.get(),
writes_data_property: self.writes_data_property.get(),
new_shape: self.new_shape.get(),
- shape: self.shape.get(),
+ shape: self.shape_key(),
prototype: self.prototype.get(),
prototype_chain_validity: self.prototype_chain_validity.get(),
key: self.key.get(),
@@ -174,16 +172,24 @@ impl PropertyLookupCacheEntry {
pub fn set(&self, data: PropertyLookupCacheEntryData) {
self.entry_type.set(data.entry_type);
self.property_offset.set(data.property_offset);
- self.shape_dictionary_generation.set(data.shape_dictionary_generation);
+ self.shape_dictionary_generation
+ .set(data.shape.map_or(0, |key| key.dictionary_generation));
self.writes_data_property.set(data.writes_data_property);
self.new_shape.set(data.new_shape);
- self.shape.set(data.shape);
+ self.shape.set(data.shape.map(|key| key.shape));
self.prototype.set(data.prototype);
self.prototype_chain_validity.set(data.prototype_chain_validity);
self.key.set(data.key);
self.getter.set(data.getter);
}
+ pub fn shape_key(&self) -> Option<ShapeKey> {
+ self.shape.get().map(|shape| ShapeKey {
+ shape,
+ dictionary_generation: self.shape_dictionary_generation.get(),
+ })
+ }
+
/// Forgets the whole entry if one of its cells died in this collection, as clear_cache_entry_if_dead does.
fn clear_if_it_has_a_dead_cell(&self, sweep: &Sweep<'_>) {
if self.get().has_dead_cell(sweep) {
@@ -279,7 +285,7 @@ fn megamorphic_secondary_index(shape: Gc<Shape>, key: u64) -> usize {
fn megamorphic_slot_for_shape(data: &MegamorphicData, shape: Gc<Shape>, key: u64) -> Option<&PropertyLookupCacheEntry> {
let is_for_shape = |slot: &&PropertyLookupCacheEntry| {
let entry = slot.get();
- entry.shape == Some(shape) && entry.key == key
+ entry.shape.is_some_and(|entry_shape| entry_shape.shape == shape) && entry.key == key
};
Some(&data.primary_entries[megamorphic_primary_index(shape, key)])
.filter(is_for_shape)
@@ -287,11 +293,11 @@ fn megamorphic_slot_for_shape(data: &MegamorphicData, shape: Gc<Shape>, key: u64
}
fn insert_megamorphic_entry(data: &MegamorphicData, entry: &PropertyLookupCacheEntryData) {
- let lookup_shape = entry.shape.expect("a megamorphic entry has a shape");
+ let lookup_shape = entry.shape.expect("a megamorphic entry has a shape").shape;
let primary_entry = &data.primary_entries[megamorphic_primary_index(lookup_shape, entry.key)];
let primary = primary_entry.get();
- if let Some(displaced_shape) = primary.shape
+ if let Some(displaced_shape) = primary.shape.map(|key| key.shape)
&& (displaced_shape != lookup_shape || primary.key != entry.key)
{
data.secondary_entries[megamorphic_secondary_index(displaced_shape, primary.key)].set(primary);
@@ -462,10 +468,10 @@ impl PropertyLookupCache {
// NB: A keyed access that keeps missing cycles through more keys than the cache can hold, and filling it
// would cost more than it saves.
if new_entry.key != 0 {
- let lookup_shape = new_entry.shape.expect("a megamorphic entry has a shape");
+ let lookup_shape = new_entry.shape.expect("a megamorphic entry has a shape").shape;
let is_known = |entry: &PropertyLookupCacheEntry| {
let entry = entry.get();
- entry.shape == Some(lookup_shape) && entry.key == new_entry.key
+ entry.shape.is_some_and(|key| key.shape == lookup_shape) && entry.key == new_entry.key
};
let known = is_known(&data.primary_entries[megamorphic_primary_index(lookup_shape, new_entry.key)])
|| is_known(&data.secondary_entries[megamorphic_secondary_index(lookup_shape, new_entry.key)]);
@@ -856,6 +862,23 @@ pub struct KeyedPropertyLookup {
pub new_shape: Option<Gc<Shape>>,
}
+impl KeyedPropertyLookup {
+ pub fn for_shape(shape: ShapeKey) -> Self {
+ Self {
+ shape_dictionary_generation: shape.dictionary_generation,
+ shape: Some(shape.shape),
+ ..Default::default()
+ }
+ }
+
+ pub fn shape_key(&self) -> Option<ShapeKey> {
+ self.shape.map(|shape| ShapeKey {
+ shape,
+ dictionary_generation: self.shape_dictionary_generation,
+ })
+ }
+}
+
impl Default for KeyedPropertyLookup {
fn default() -> Self {
Self {
@@ -1097,18 +1120,13 @@ impl ObjectPropertyIteratorCacheData {
shape: Gc<Shape>,
prototype_chain_validity: Option<Gc<WatchpointSet>>,
) -> Gc<ObjectPropertyIteratorCacheData> {
- let (shape_is_dictionary, shape_dictionary_generation) = if shape.is_dictionary() {
- (true, shape.dictionary_generation())
- } else {
- (false, 0)
- };
let cache_data = vm.heap().allocate(|_| Self {
header: CellHeader::for_class(Self::CLASS),
- shape_is_dictionary: Cell::new(shape_is_dictionary),
+ shape_is_dictionary: Cell::new(shape.is_dictionary()),
fast_path: Cell::new(fast_path),
receiver_has_magical_length: Cell::new(receiver_has_magical_length_property),
indexed_property_count: Cell::new(indexed_property_count),
- shape_dictionary_generation: Cell::new(shape_dictionary_generation),
+ shape_dictionary_generation: Cell::new(shape.dictionary_generation()),
shape: Cell::new(Some(shape)),
prototype_chain_validity: Cell::new(prototype_chain_validity),
property_values: InterpreterBuffer::new(),
@@ -1175,18 +1193,17 @@ impl ObjectPropertyIteratorCacheData {
self.receiver_has_magical_length.get()
}
- pub fn shape(&self) -> Option<Gc<Shape>> {
- self.shape.get()
+ pub fn shape_key(&self) -> Option<ShapeKey> {
+ self.shape.get().map(|shape| ShapeKey {
+ shape,
+ dictionary_generation: self.shape_dictionary_generation.get(),
+ })
}
pub fn prototype_chain_validity(&self) -> Option<Gc<WatchpointSet>> {
self.prototype_chain_validity.get()
}
- pub fn shape_dictionary_generation(&self) -> u32 {
- self.shape_dictionary_generation.get()
- }
-
/// The key values the interpreter's fast path hands out, one per indexed and named key.
pub fn property_value(&self, index: usize) -> Value {
self.property_values.get(index)
diff --git a/Libraries/LibJS/Rust/src/bytecode/property_access.rs b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
index 05c8c303e61..e29b8c7e5bd 100644
--- a/Libraries/LibJS/Rust/src/bytecode/property_access.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
@@ -27,6 +27,7 @@ use crate::runtime::object::{
};
use crate::runtime::property_attributes::{Attribute, PropertyAttributes};
use crate::runtime::property_key::PropertyKey;
+use crate::runtime::shape::ShapeKey;
use crate::utf16::Utf16Display;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -73,8 +74,7 @@ fn remember_keyed_lookup(site_cache: &PropertyLookupCache, site_cache_key: u64,
site_cache.update(entry.entry_type, |site_entry| {
site_entry.key = site_cache_key;
site_entry.property_offset = entry.property_offset;
- site_entry.shape_dictionary_generation = entry.shape_dictionary_generation;
- site_entry.shape = entry.shape;
+ site_entry.shape = entry.shape_key();
site_entry.prototype = entry.prototype;
site_entry.prototype_chain_validity = entry.prototype_chain_validity;
});
@@ -104,7 +104,7 @@ pub fn get_by_value_with_keyed_cache(
}
};
if let Some(entry) = keyed_property_lookup_cache.lookup(entry_index, shape, property_name)
- && (!shape.is_dictionary() || shape.dictionary_generation() == entry.shape_dictionary_generation)
+ && entry.shape_key() == Some(ShapeKey::of(shape))
{
let prototype_chain_validity_is_valid = entry
.prototype_chain_validity
@@ -136,7 +136,7 @@ pub fn get_by_value_with_keyed_cache(
.prototype()
.and_then(|prototype| prototype.shape().prototype_chain_validity());
- let dictionary_generation = shape.dictionary_generation();
+ let shape_key = ShapeKey::of(shape);
let mut cacheable_metadata = CacheableGetPropertyMetadata {
property_absence_is_cacheable: base_object.is_cacheable_for_absence_of(vm, property_key),
..Default::default()
@@ -151,18 +151,13 @@ pub fn get_by_value_with_keyed_cache(
// A getter may have changed the object's shape or the property storage of a dictionary shape, which
// leaves the metadata describing a lookup that no longer applies.
- if shape != base_object.shape()
- || shape.dictionary_generation() != dictionary_generation
+ if ShapeKey::of(base_object.shape()) != shape_key
|| cacheable_metadata.r#type == CacheableGetPropertyMetadataType::NotCacheable
{
return Ok(value);
}
- let mut entry = KeyedPropertyLookup {
- shape: Some(shape),
- ..Default::default()
- };
- entry.shape_dictionary_generation = shape.dictionary_generation();
+ let mut entry = KeyedPropertyLookup::for_shape(shape_key);
match cacheable_metadata.r#type {
CacheableGetPropertyMetadataType::GetOwnProperty => {
entry.entry_type = PropertyLookupCacheEntryType::GetOwnProperty;
@@ -200,8 +195,7 @@ pub fn get_own_property_without_side_effects(
if let Some(cache_entry) = cache.first_entry()
&& (cache_entry.entry_type == PropertyLookupCacheEntryType::GetOwnProperty
|| cache_entry.entry_type == PropertyLookupCacheEntryType::GetMissingProperty)
- && Some(shape) == cache_entry.shape
- && (!shape.is_dictionary() || shape.dictionary_generation() == cache_entry.shape_dictionary_generation)
+ && cache_entry.shape == Some(ShapeKey::of(shape))
{
if cache_entry.entry_type == PropertyLookupCacheEntryType::GetMissingProperty {
return Value::EMPTY;
@@ -216,11 +210,10 @@ pub fn get_own_property_without_side_effects(
PropertyLookupCacheEntryType::GetMissingProperty
};
cache.update(cache_type, |entry| {
- entry.shape = Some(shape);
+ entry.shape = Some(ShapeKey::of(shape));
if let Some(metadata) = metadata {
entry.property_offset = metadata.offset;
}
- entry.shape_dictionary_generation = shape.dictionary_generation();
});
let Some(metadata) = metadata else {
@@ -343,9 +336,7 @@ pub fn cached_property_slot(object: Gc<Object>, cache: &PropertyLookupCache, cac
if cache_key != 0 && entry.key.get() != cache_key {
continue;
}
- if Some(shape) != entry.shape.get()
- || (shape.is_dictionary() && shape.dictionary_generation() != entry.shape_dictionary_generation.get())
- {
+ if entry.shape_key() != Some(ShapeKey::of(shape)) {
continue;
}
let prototype_chain_is_valid = || {
@@ -398,7 +389,7 @@ pub fn get_with_property_lookup_cache(
.prototype()
.and_then(|prototype| prototype.shape().prototype_chain_validity());
- let dictionary_generation = shape.dictionary_generation();
+ let shape_key = ShapeKey::of(shape);
let mut cacheable_metadata = CacheableGetPropertyMetadata {
property_absence_is_cacheable: base_obj.is_cacheable_for_absence_of(vm, property_name),
..Default::default()
@@ -415,39 +406,33 @@ pub fn get_with_property_lookup_cache(
// that collected metadata is valid, e.g. if getter in prototype chain added
// property with the same name into the object itself. The same applies when
// a getter changed the property storage of a dictionary shape.
- if shape == base_obj.shape() && shape.dictionary_generation() == dictionary_generation {
+ if ShapeKey::of(base_obj.shape()) == shape_key {
match cacheable_metadata.r#type {
CacheableGetPropertyMetadataType::GetOwnProperty => {
cache.update(PropertyLookupCacheEntryType::GetOwnProperty, |entry| {
entry.key = cache_key;
- entry.shape = Some(shape);
+ entry.shape = Some(shape_key);
entry.property_offset = cacheable_metadata
.property_offset
.expect("cacheable metadata has an offset");
-
- entry.shape_dictionary_generation = shape.dictionary_generation();
});
}
CacheableGetPropertyMetadataType::GetPropertyInPrototypeChain => {
cache.update(PropertyLookupCacheEntryType::GetPropertyInPrototypeChain, |entry| {
entry.key = cache_key;
- entry.shape = Some(base_obj.shape());
+ entry.shape = Some(shape_key);
entry.property_offset = cacheable_metadata
.property_offset
.expect("cacheable metadata has an offset");
entry.prototype = cacheable_metadata.prototype;
entry.prototype_chain_validity = prototype_chain_validity;
-
- entry.shape_dictionary_generation = shape.dictionary_generation();
});
}
CacheableGetPropertyMetadataType::GetMissingProperty => {
cache.update(PropertyLookupCacheEntryType::GetMissingProperty, |entry| {
entry.key = cache_key;
- entry.shape = Some(shape);
+ entry.shape = Some(shape_key);
entry.prototype_chain_validity = prototype_chain_validity;
-
- entry.shape_dictionary_generation = shape.dictionary_generation();
});
}
_ => {}
@@ -539,7 +524,7 @@ pub fn put_by_property_key(
} else {
(None, object.shape())
};
- let from_shape_dictionary_generation = from_shape.dictionary_generation();
+ let from_shape = ShapeKey::of(from_shape);
if let Some(caches) = caches {
for cache in caches.entries_for_shape(object.shape(), cache_key).as_slice() {
// NB: Only the caches of keyed accesses have keys, and named accesses pass a constant 0.
@@ -552,13 +537,8 @@ pub fn put_by_property_key(
let Some(cached_prototype) = cache.prototype else {
continue;
};
- let Some(cached_shape) = cache.shape else {
- continue;
- };
// OPTIMIZATION: If the prototype chain hasn't been mutated in a way that would invalidate the cache, we can use it.
- let can_use_cache = object.shape() == cached_shape
- && (!cached_shape.is_dictionary()
- || object.shape().dictionary_generation() == cache.shape_dictionary_generation)
+ let can_use_cache = cache.shape == Some(ShapeKey::of(object.shape()))
&& cache
.prototype_chain_validity
.is_some_and(|validity| validity.is_valid());
@@ -574,16 +554,7 @@ pub fn put_by_property_key(
}
}
PropertyLookupCacheEntryType::ChangeOwnProperty => {
- let Some(cached_shape) = cache.shape else {
- continue;
- };
- if cached_shape != object.shape() {
- continue;
- }
-
- if cached_shape.is_dictionary()
- && cached_shape.dictionary_generation() != cache.shape_dictionary_generation
- {
+ if cache.shape != Some(ShapeKey::of(object.shape())) {
continue;
}
@@ -604,7 +575,7 @@ pub fn put_by_property_key(
PropertyLookupCacheEntryType::AddOwnProperty => {
// OPTIMIZATION: If the object's shape is the same as the one cached before adding the new property, we can
// reuse the resulting shape from the cache.
- if cache.shape != Some(object.shape()) {
+ if cache.shape != Some(ShapeKey::of(object.shape())) {
continue;
}
if !property_addition_is_cacheable(vm, &object, name) {
@@ -676,8 +647,7 @@ pub fn put_by_property_key(
// a setter changed the property storage of a dictionary shape.
if let Some(caches) = caches
&& succeeded
- && from_shape == object.shape()
- && from_shape.dictionary_generation() == from_shape_dictionary_generation
+ && ShapeKey::of(object.shape()) == from_shape
{
match cacheable_metadata.r#type {
CacheableSetPropertyMetadataType::AddOwnProperty => {
@@ -687,19 +657,17 @@ pub fn put_by_property_key(
CacheableSetPropertyMetadataType::ChangeOwnProperty => {
caches.update(PropertyLookupCacheEntryType::ChangeOwnProperty, |cache| {
cache.key = cache_key;
- cache.shape = Some(object.shape());
+ cache.shape = Some(from_shape);
cache.property_offset = cacheable_metadata
.property_offset
.expect("cacheable metadata has an offset");
cache.writes_data_property = cacheable_metadata.writes_data_property;
-
- cache.shape_dictionary_generation = object.shape().dictionary_generation();
});
}
CacheableSetPropertyMetadataType::ChangePropertyInPrototypeChain => {
caches.update(PropertyLookupCacheEntryType::ChangePropertyInPrototypeChain, |cache| {
cache.key = cache_key;
- cache.shape = Some(object.shape());
+ cache.shape = Some(from_shape);
cache.property_offset = cacheable_metadata
.property_offset
.expect("cacheable metadata has an offset");
@@ -708,8 +676,6 @@ pub fn put_by_property_key(
.expect("a property in the prototype chain has a holder");
cache.prototype = Some(prototype);
cache.prototype_chain_validity = prototype_chain_validity;
-
- cache.shape_dictionary_generation = object.shape().dictionary_generation();
});
}
CacheableSetPropertyMetadataType::NotCacheable => {}
@@ -743,7 +709,7 @@ pub fn put_by_property_key(
// objects, but keep this aligned with the normal PutById
// AddOwnProperty cache hit so a future bytecode path cannot
// bypass subclass hooks for objects that require them.
- if cache.shape != Some(object.shape()) {
+ if cache.shape != Some(ShapeKey::of(object.shape())) {
continue;
}
if !property_addition_is_cacheable(vm, &object, name) {
@@ -773,7 +739,7 @@ pub fn put_by_property_key(
{
caches.update(PropertyLookupCacheEntryType::AddOwnProperty, |cache| {
cache.key = cache_key;
- cache.shape = Some(from_shape);
+ cache.shape = Some(ShapeKey::of(from_shape));
cache.new_shape = Some(object.shape());
cache.property_offset = object.shape().lookup(name).expect("the property was just added").offset;
});
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
index 81325ea8aca..53c1e118703 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
@@ -41,6 +41,7 @@ use crate::runtime::object::{
use crate::runtime::primitive_string::PrimitiveString;
use crate::runtime::property_key::PropertyKey;
use crate::runtime::reference::{BaseType, Reference};
+use crate::runtime::shape::ShapeKey;
use crate::runtime::source_text_module::SourceTextModule;
use crate::utf16::{Utf16Display, Utf16View, utf16_formatted};
@@ -901,8 +902,7 @@ fn global_variable_value(
let shape = binding_object.shape();
if cache.environment_serial_number.get() == declarative_record.environment_serial_number() {
if let Some(entry) = cache.first_entry()
- && entry.shape == Some(shape)
- && (!shape.is_dictionary() || shape.dictionary_generation() == entry.shape_dictionary_generation)
+ && entry.shape == Some(ShapeKey::of(shape))
{
let value = binding_object.get_direct(entry.property_offset);
return get_cached_property_value(vm, value, Value::from_object(binding_object)).map(Some);
@@ -934,7 +934,7 @@ fn global_variable_value(
let identifier_key = PropertyKey::from(identifier);
if binding_object.has_property(vm, &identifier_key)? {
- let dictionary_generation = shape.dictionary_generation();
+ let shape_key = ShapeKey::of(shape);
let mut cacheable_metadata = CacheableGetPropertyMetadata::default();
let value = binding_object.internal_get(
vm,
@@ -944,18 +944,13 @@ fn global_variable_value(
PropertyLookupPhase::OwnProperty,
)?;
if cacheable_metadata.r#type == CacheableGetPropertyMetadataType::GetOwnProperty
- && shape == binding_object.shape()
- && shape.dictionary_generation() == dictionary_generation
+ && ShapeKey::of(binding_object.shape()) == shape_key
{
cache.update(PropertyLookupCacheEntryType::GetOwnProperty, |entry| {
- entry.shape = Some(shape);
+ entry.shape = Some(shape_key);
entry.property_offset = cacheable_metadata
.property_offset
.expect("an own property get reports its offset");
-
- if shape.is_dictionary() {
- entry.shape_dictionary_generation = shape.dictionary_generation();
- }
});
}
return Ok(Some(value));
@@ -975,8 +970,7 @@ pub fn set_global(vm: &Vm, pc: u32, instruction: &op::SetGlobal, values: &mut op
if cache().environment_serial_number.get() == declarative_record.environment_serial_number() {
if let Some(entry) = cache().first_entry()
- && entry.shape == Some(shape)
- && (!shape.is_dictionary() || shape.dictionary_generation() == entry.shape_dictionary_generation)
+ && entry.shape == Some(ShapeKey::of(shape))
{
let value = binding_object.get_direct(entry.property_offset);
if value.is_accessor() {
@@ -1029,7 +1023,7 @@ pub fn set_global(vm: &Vm, pc: u32, instruction: &op::SetGlobal, values: &mut op
let identifier_key = PropertyKey::from(identifier.clone());
if asm_try!(vm, pc, binding_object.has_property(vm, &identifier_key)) {
- let dictionary_generation = shape.dictionary_generation();
+ let shape_key = ShapeKey::of(shape);
let mut cacheable_metadata = CacheableSetPropertyMetadata::default();
let success = asm_try!(
vm,
@@ -1060,19 +1054,14 @@ pub fn set_global(vm: &Vm, pc: u32, instruction: &op::SetGlobal, values: &mut op
return throw_error(vm, pc, ErrorKind::TypeError, ErrorType::ObjectSetReturnedFalse, &[]);
}
if cacheable_metadata.r#type == CacheableSetPropertyMetadataType::ChangeOwnProperty
- && shape == binding_object.shape()
- && shape.dictionary_generation() == dictionary_generation
+ && ShapeKey::of(binding_object.shape()) == shape_key
{
cache().update(PropertyLookupCacheEntryType::ChangeOwnProperty, |entry| {
- entry.shape = Some(shape);
+ entry.shape = Some(shape_key);
entry.property_offset = cacheable_metadata
.property_offset
.expect("an own property change reports its offset");
entry.writes_data_property = cacheable_metadata.writes_data_property;
-
- if shape.is_dictionary() {
- entry.shape_dictionary_generation = shape.dictionary_generation();
- }
});
}
return SlowPathControl::continue_at(pc + op::SetGlobal::LENGTH);
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 4aff9ade62b..2185e4c6da6 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
@@ -45,6 +45,7 @@ use crate::runtime::private_environment::{PrivateEnvironment, PrivateName};
use crate::runtime::property_attributes::{Attribute, DEFAULT_ATTRIBUTES, PropertyAttributes};
use crate::runtime::property_key::{NonSymbolPropertyKey, PropertyKey};
use crate::runtime::reference::Reference;
+use crate::runtime::shape::ShapeKey;
use crate::runtime::typed_array::{Kind, is_valid_integer_index, typed_array_of_object};
fn strict_of(header_strict: bool) -> Strict {
@@ -173,7 +174,7 @@ pub fn get_by_id_cached_accessor(
&& let Some(completed_entry) = cache.first_entry_slot()
{
let completed = completed_entry.get();
- if completed.shape == Some(object.shape()) {
+ if completed.shape == Some(ShapeKey::of(object.shape())) {
let completed_holder = completed.prototype.unwrap_or(object);
let completed_value = completed_holder.get_direct(completed.property_offset);
if completed_value.is_accessor() && completed_value.as_accessor().getter() == Some(getter) {
@@ -546,12 +547,13 @@ fn put_by_value_with_keyed_store_cache(
cache_key,
)
};
- if let Some(entry) = store_cache.lookup(index, shape, name) {
+ let shape_key = ShapeKey::of(shape);
+ if let Some(entry) = store_cache.lookup(index, shape, name)
+ && entry.shape_key() == Some(shape_key)
+ {
match entry.entry_type {
PropertyLookupCacheEntryType::ChangeOwnProperty => {
- if (!shape.is_dictionary() || shape.dictionary_generation() == entry.shape_dictionary_generation)
- && !object.get_direct(entry.property_offset).is_accessor()
- {
+ if !object.get_direct(entry.property_offset).is_accessor() {
object.put_direct_from_cache(entry.property_offset, value);
return Ok(());
}
@@ -570,32 +572,25 @@ fn put_by_value_with_keyed_store_cache(
return Ok(());
}
}
- _ => {
- if !shape.is_dictionary() || shape.dictionary_generation() == entry.shape_dictionary_generation {
- return generic_put(None, 0);
- }
- }
+ _ => return generic_put(None, 0),
}
}
- let shape_dictionary_generation = shape.dictionary_generation();
let scratch_cache = vm.keyed_property_store_scratch_cache();
scratch_cache.forget_entries();
generic_put(Some(scratch_cache), key.0)?;
let entry = scratch_cache.first_entry().unwrap_or_default();
scratch_cache.forget_entries();
// NB: Puts the cache does not make are remembered with another type, for the shape they found.
- let mut lookup = KeyedPropertyLookup {
- shape_dictionary_generation,
- shape: Some(shape),
- ..Default::default()
- };
+ let mut lookup = KeyedPropertyLookup::for_shape(shape_key);
match entry.entry_type {
- PropertyLookupCacheEntryType::ChangeOwnProperty if entry.writes_data_property && entry.shape == Some(shape) => {
+ PropertyLookupCacheEntryType::ChangeOwnProperty
+ if entry.writes_data_property && entry.shape == Some(shape_key) =>
+ {
lookup.entry_type = PropertyLookupCacheEntryType::ChangeOwnProperty;
lookup.property_offset = entry.property_offset;
}
- PropertyLookupCacheEntryType::AddOwnProperty if entry.shape == Some(shape) && entry.new_shape.is_some() => {
+ PropertyLookupCacheEntryType::AddOwnProperty if entry.shape == Some(shape_key) && entry.new_shape.is_some() => {
lookup.entry_type = PropertyLookupCacheEntryType::AddOwnProperty;
lookup.property_offset = entry.property_offset;
lookup.prototype_chain_validity = entry.prototype_chain_validity;
@@ -949,11 +944,7 @@ fn object_property_iterator_cache_matches(object: Gc<Object>, cache: &ObjectProp
}
let shape = object.shape();
- if Some(shape) != cache.shape() {
- return false;
- }
-
- if shape.is_dictionary() && shape.dictionary_generation() != cache.shape_dictionary_generation() {
+ if cache.shape_key() != Some(ShapeKey::of(shape)) {
return false;
}
@@ -1535,15 +1526,7 @@ fn try_put_by_property_lookup_cache(
}
match entry.entry_type.get() {
PropertyLookupCacheEntryType::ChangeOwnProperty => {
- let Some(cached_shape) = entry.shape.get() else {
- continue;
- };
- if cached_shape != object.shape() {
- continue;
- }
- if cached_shape.is_dictionary()
- && cached_shape.dictionary_generation() != entry.shape_dictionary_generation.get()
- {
+ if entry.shape_key() != Some(ShapeKey::of(object.shape())) {
continue;
}
let current = object.get_direct(entry.property_offset.get());
@@ -1554,7 +1537,7 @@ fn try_put_by_property_lookup_cache(
return true;
}
PropertyLookupCacheEntryType::AddOwnProperty => {
- if entry.shape.get() != Some(object.shape()) {
+ if entry.shape_key() != Some(ShapeKey::of(object.shape())) {
continue;
}
if !object_can_cache_property_additions(&object) {
diff --git a/Libraries/LibJS/Rust/src/jit/intrinsics.rs b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
index cfb81664742..7fa819b46aa 100644
--- a/Libraries/LibJS/Rust/src/jit/intrinsics.rs
+++ b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
@@ -22,6 +22,7 @@ use crate::runtime::module_namespace_object::ModuleNamespaceObject;
use crate::runtime::property_key::{PropertyKey, StringMayBeNumber};
use crate::runtime::proxy_object::ProxyObject;
use crate::runtime::realm_watchpoints;
+use crate::runtime::shape::ShapeKey;
use crate::runtime::string_object::StringObject;
use crate::runtime::typed_array::{ContentType, TypedArrayBase};
use crate::utf16::{has_fly_string_storage, to_utf16_fly_string};
@@ -151,7 +152,7 @@ fn has_own_string_property(vm: &Vm, object: Gc<Object>, name: &Utf16FlyString) -
let cache = vm.keyed_property_lookup_cache();
let index = KeyedPropertyLookupCache::entry_index_for(shape, name);
if let Some(entry) = cache.lookup(index, shape, name)
- && (!shape.is_dictionary() || shape.dictionary_generation() == entry.shape_dictionary_generation)
+ && entry.shape_key() == Some(ShapeKey::of(shape))
{
// NB: Only names that are no array indices have entries, since the shape does not hold indexed properties.
match entry.entry_type {
@@ -177,11 +178,7 @@ fn has_own_string_property(vm: &Vm, object: Gc<Object>, name: &Utf16FlyString) -
PropertyLookupCacheEntryType::MissingOwnProperty
},
property_offset: metadata.map_or(0, |metadata| metadata.offset),
- shape_dictionary_generation: shape.dictionary_generation(),
- shape: Some(shape),
- prototype: None,
- prototype_chain_validity: None,
- new_shape: None,
+ ..KeyedPropertyLookup::for_shape(ShapeKey::of(shape))
},
name,
);
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index d3f607a35d9..7d1f21be47a 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -59,7 +59,7 @@ use crate::runtime::object_prototype;
use crate::runtime::property_key::PropertyKey;
use crate::runtime::realm::Realm;
use crate::runtime::realm_watchpoints;
-use crate::runtime::shape::Shape;
+use crate::runtime::shape::{Shape, ShapeKey};
use crate::runtime::shared_function_instance_data::ClosureCount;
use crate::runtime::shared_function_instance_data::ConstructorKind;
use crate::runtime::shared_function_instance_data::FunctionKind;
@@ -267,7 +267,7 @@ fn property_holds_data(entry: &PropertyLookupCacheEntryData) -> bool {
) {
return false;
}
- let Some(shape) = entry.shape else {
+ let Some(ShapeKey { shape, .. }) = entry.shape else {
return false;
};
let mut writable = false;
@@ -430,10 +430,8 @@ impl SnapshotBuilder<'_> {
};
for slot in slots {
let entry = slot.get();
- if entry
- .shape
- .is_some_and(|shape| primitive_prototype_shapes.contains(&shape))
- {
+ let shape = entry.shape.map(|key| key.shape);
+ if shape.is_some_and(|shape| primitive_prototype_shapes.contains(&shape)) {
sees_primitives = true;
continue;
}
@@ -455,7 +453,7 @@ impl SnapshotBuilder<'_> {
let cached_value_offset = prototype_property_value(&entry)
.filter(|value| value.is_accessor() && value.as_accessor().cache_on_receiver())
.and_then(|value| value.as_accessor().cached_value_key())
- .and_then(|key| entry.shape?.lookup(&PropertyKey::from_symbol(key)))
+ .and_then(|key| shape?.lookup(&PropertyKey::from_symbol(key)))
.map(|metadata| metadata.offset);
let accessor_function = entry_accessor_function(&entry).map(|function| {
let cell = self.cell(function);
@@ -473,12 +471,12 @@ impl SnapshotBuilder<'_> {
entries.push(PropertyCacheEntrySnapshot {
entry_type,
property_offset: entry.property_offset,
- shape_dictionary_generation: entry.shape_dictionary_generation,
- shape_is_dictionary: entry.shape.is_some_and(|shape| shape.is_dictionary()),
- shape_is_stable: entry.shape.is_some_and(|shape| shape.is_stable()),
+ shape_dictionary_generation: entry.shape.map_or(0, |key| key.dictionary_generation),
+ shape_is_dictionary: shape.is_some_and(|shape| shape.is_dictionary()),
+ shape_is_stable: shape.is_some_and(|shape| shape.is_stable()),
writes_data_property: entry.writes_data_property,
new_shape: self.optional_cell(entry.new_shape),
- shape: self.optional_cell(entry.shape),
+ shape: self.optional_cell(shape),
prototype: self.optional_cell(entry.prototype),
prototype_chain_validity: self.optional_cell(entry.prototype_chain_validity),
prototype_chain_valid: entry
@@ -552,8 +550,7 @@ impl SnapshotBuilder<'_> {
return None;
}
if let Some(entry) = cache.first_entry()
- && entry.shape == Some(shape)
- && (!shape.is_dictionary() || shape.dictionary_generation() == entry.shape_dictionary_generation)
+ && entry.shape == Some(ShapeKey::of(shape))
{
let value = object.get_direct(entry.property_offset);
if value.is_accessor() {
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index 23a5340be73..71fa990f373 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -54,7 +54,7 @@ use crate::runtime::property_attributes::{DEFAULT_ATTRIBUTES, PropertyAttributes
use crate::runtime::property_descriptor::{PropertyDescriptor, to_property_descriptor};
use crate::runtime::property_key::{NonSymbolPropertyKey, PropertyKey};
use crate::runtime::realm::Realm;
-use crate::runtime::shape::{AssignCopy, Shape};
+use crate::runtime::shape::{AssignCopy, Shape, ShapeKey};
use crate::runtime::special_property_cache::{self, SpecialPropertyKey};
use crate::runtime::symbol::Symbol;
use crate::runtime::value::{PreferredType, is_strictly_equal, same_value};
@@ -1306,8 +1306,7 @@ impl Object {
// 2. Let properties be a new empty List.
let properties = MarkedVec::with_capacity(vm, self.own_properties_count());
- let pre_iteration_shape = self.shape();
- let pre_iteration_dictionary_generation = pre_iteration_shape.dictionary_generation();
+ let pre_iteration_shape = ShapeKey::of(self.shape());
self.for_each_own_property_with_enumerability(vm, |property_key, enumerable| {
// a. If Type(key) is String, then
// i. Let desc be ? O.[[GetOwnProperty]](key).
@@ -1315,9 +1314,7 @@ impl Object {
// NOTE: If the object's shape has been mutated during iteration through own properties
// by executing a getter, we can no longer assume that subsequent properties
// are still present and enumerable.
- if self.shape() == pre_iteration_shape
- && self.shape().dictionary_generation() == pre_iteration_dictionary_generation
- {
+ if ShapeKey::of(self.shape()) == pre_iteration_shape {
if !enumerable {
return Ok(());
}
diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rs
index 4cfb61ff229..fd20d751719 100644
--- a/Libraries/LibJS/Rust/src/runtime/shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs
@@ -114,6 +114,23 @@ pub struct AssignCopy {
pub copy_shape: Gc<Shape>,
}
+/// A shape and its dictionary generation, which changes when a dictionary shape changes its properties in place. Shapes
+/// that are not dictionaries keep generation 0.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct ShapeKey {
+ pub shape: Gc<Shape>,
+ pub dictionary_generation: u32,
+}
+
+impl ShapeKey {
+ pub fn of(shape: Gc<Shape>) -> Self {
+ Self {
+ shape,
+ dictionary_generation: shape.dictionary_generation(),
+ }
+ }
+}
+
/// The string keys of the properties of a shape, as with_own_string_keys() caches them.
struct OwnStringKeys {
/// The dictionary generation of the shape when the keys were collected. Changing the properties of a shape in