Skip to content

Truly independent web browser

LibJS: Use megamorphic cache entries for the shapes of new objects

Andreas Kling459333a1ca0e
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs b/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
index da04b0c578f..ac54d043faf 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
@@ -19,6 +19,8 @@
//! same entries.
use super::GraphBuilder;
+use super::SlotState;
+use crate::bytecode::Operand;
use crate::ir::AccessorPart;
use crate::ir::Block;
use crate::ir::BranchCondition;
@@ -215,6 +217,26 @@ impl GraphBuilder<'_> {
cache.entries.iter().map(CacheEntry::of).collect()
}
+ /// The entry of megamorphic property lookup cache `cache` for the shape
+ /// the code knows the object in `object` has (see
+ /// `AbstractFrame::fresh_shapes`), if the snapshot captured one.
+ pub(super) fn megamorphic_entries_for_known_shape(&self, cache: u32, object: Operand) -> Option<Vec<CacheEntry>> {
+ let cache = self.function.executable.property_caches.get(cache as usize)?;
+ if cache.kind != PropertyCacheKind::Megamorphic {
+ return None;
+ }
+ let SlotState::Value { node, .. } = self.peek(object) else {
+ return None;
+ };
+ let shape = self.fresh_shape(node)?;
+ let entry = cache
+ .entries
+ .iter()
+ .filter_map(CacheEntry::of)
+ .find(|entry| entry.shape.shape == shape)?;
+ Some(vec![entry])
+ }
+
/// The shape guard of an entry. Records whether the shape was stable,
/// which check elimination may rely on.
pub(super) fn shape_guard(&mut self, entry: &CacheEntry) -> ShapeCheck {
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
index 599740a3acb..65e7c339855 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
@@ -217,7 +217,10 @@ impl GraphBuilder<'_> {
base: Operand,
cache: u32,
) -> Result<bool, CompileFailure> {
- let Some(entries) = self.cache_entries(cache) else {
+ let Some(entries) = self
+ .cache_entries(cache)
+ .or_else(|| self.megamorphic_entries_for_known_shape(cache, base))
+ else {
return Ok(false);
};
self.build_get(dst, base, entries, None)
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 7af1c4059f2..f0a4a5e8655 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -277,6 +277,17 @@ fn megamorphic_secondary_index(shape: Gc<Shape>, key: u64) -> usize {
& (MEGAMORPHIC_SECONDARY_CACHE_SIZE - 1)
}
+/// The slot of a megamorphic cache's hash tables that holds the entry for `shape` and `key`, if one does.
+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.lookup_shape() == Some(shape) && entry.key == key
+ };
+ Some(&data.primary_entries[megamorphic_primary_index(shape, key)])
+ .filter(is_for_shape)
+ .or_else(|| Some(&data.secondary_entries[megamorphic_secondary_index(shape, key)]).filter(is_for_shape))
+}
+
fn insert_megamorphic_entry(data: &MegamorphicData, entry: &PropertyLookupCacheEntryData) {
let lookup_shape = entry.lookup_shape().expect("a megamorphic entry has a lookup shape");
@@ -416,22 +427,20 @@ impl PropertyLookupCache {
let Some(data) = self.megamorphic_data() else {
return self.entries();
};
-
- let find_entry = |entries: &[PropertyLookupCacheEntry], index: usize| {
- let entry = entries[index].get();
- (entry.lookup_shape() == Some(shape) && entry.key == key).then_some(entry)
- };
-
- let entry = find_entry(&data.primary_entries, megamorphic_primary_index(shape, key))
- .or_else(|| find_entry(&data.secondary_entries, megamorphic_secondary_index(shape, key)));
- let Some(entry) = entry else {
+ let Some(slot) = megamorphic_slot_for_shape(data, shape, key) else {
return &[];
};
- data.entry.set(entry);
+ data.entry.set(slot.get());
core::slice::from_ref(&data.entry)
}
+ /// The entry a megamorphic cache has for `shape` and `key`, if it has one, without moving it to the front like
+ /// `entry_slots_for_shape()` does.
+ pub fn megamorphic_entry_for_shape(&self, shape: Gc<Shape>, key: u64) -> Option<&PropertyLookupCacheEntry> {
+ megamorphic_slot_for_shape(self.megamorphic_data()?, shape, key)
+ }
+
/// Records an entry of `entry_type`, filled in by `callback`, moving the cache to the next tier when it has no
/// room for it. Keyed sites also refresh entries after hits in the VM cache. Keep this out of line instead of
/// growing the frames of the property access paths that every access runs through.
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 579f4f70c51..96870d29856 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -103,6 +103,9 @@ struct SnapshotBuilder<'vm> {
lexical_environment_templates: Vec<Option<LexicalEnvironmentTemplateSnapshot>>,
/// The environment templates of the direct calls of the executable whose snapshot is being made.
environment_templates: Vec<EnvironmentTemplateSnapshot>,
+ /// For the constructors among the inlining candidates, by index: the shape of the `this` value their inlined
+ /// constructs create, whose entries their megamorphic caches describe.
+ construct_this_shapes: HashMap<u32, Gc<Shape>>,
}
/// The number of bytecode instructions of an executable, counted up to one past `limit`.
@@ -353,7 +356,14 @@ impl SnapshotBuilder<'_> {
}
}
- fn property_cache(&mut self, cache: &PropertyLookupCache, caller: &Entry) -> PropertyCacheSnapshot {
+ /// What property lookup cache `cache` of `caller` knows. Of a megamorphic cache, that is the entry for
+ /// `known_shape` if it has one: JIT code only uses megamorphic entries for objects whose shape it knows.
+ fn property_cache(
+ &mut self,
+ cache: &PropertyLookupCache,
+ caller: &Entry,
+ known_shape: Option<Gc<Shape>>,
+ ) -> PropertyCacheSnapshot {
let kind = match cache.tier() {
PropertyLookupCacheTier::Empty => PropertyCacheKind::Empty,
PropertyLookupCacheTier::Monomorphic => PropertyCacheKind::Monomorphic,
@@ -371,7 +381,12 @@ impl SnapshotBuilder<'_> {
];
let mut sees_primitives = false;
let mut entries = Vec::new();
- for slot in cache.entries() {
+ let known_shape_slot = known_shape.and_then(|shape| cache.megamorphic_entry_for_shape(shape, 0));
+ let slots = match known_shape_slot {
+ Some(slot) => core::slice::from_ref(slot),
+ None => cache.entries(),
+ };
+ for slot in slots {
let entry = slot.get();
if entry
.shape
@@ -751,6 +766,7 @@ impl SnapshotBuilder<'_> {
empty_shape.create_prototype_transition(self.vm, Some(prototype))
};
let index = self.inlined_function_index(function, caller)?;
+ self.construct_this_shapes.insert(index, this_shape);
Some(ConstructTarget {
executable: index,
function_shape: self.cell(function_shape),
@@ -868,10 +884,15 @@ impl SnapshotBuilder<'_> {
keyed,
};
+ let construct_this_shape = entry
+ .function
+ .and_then(|function| self.indices.get(&function))
+ .and_then(|index| self.construct_this_shapes.get(index))
+ .copied();
let property_caches = executable
.property_lookup_caches()
.iter()
- .map(|cache| self.property_cache(cache, entry))
+ .map(|cache| self.property_cache(cache, entry, construct_this_shape))
.collect();
let object_shape_caches = executable
.object_shape_caches()
@@ -1138,6 +1159,7 @@ pub fn capture_snapshot(
closure_templates: Vec::new(),
environment_templates: Vec::new(),
lexical_environment_templates: Vec::new(),
+ construct_this_shapes: HashMap::new(),
};
builder.closure_templates = closure_template_snapshots(closure_templates, |sample| builder.cell(sample));
builder.lexical_environment_templates =
diff --git a/Tests/LibJS/JIT/ir/megamorphic-constructor-methods.js b/Tests/LibJS/JIT/ir/megamorphic-constructor-methods.js
new file mode 100644
index 00000000000..39420edae66
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/megamorphic-constructor-methods.js
@@ -0,0 +1,33 @@
+// REQUIRES: inline-allocation
+// A constructor that many classes share reads their methods through a megamorphic cache. Inlined into a construct,
+// whose object has a known shape, it reads the method of that shape as a constant and inlines its call.
+
+function makeClass(scale) {
+ const constructor = function (value) {
+ this.initialize(value);
+ };
+ constructor.prototype = {
+ initialize(value) {
+ this.value = value * scale;
+ },
+ };
+ return constructor;
+}
+const classes = [];
+for (let scale = 1; scale <= 8; ++scale) classes.push(makeClass(scale));
+const First = classes[0];
+function make(i) {
+ return new First(i).value;
+}
+jit.prepare(make);
+for (let i = 0; i < 10; ++i) {
+ for (const Class of classes) new Class(i);
+ make(i);
+}
+jit.compile(make);
+
+// CHECK-FUNCTION: make
+// CHECK-PASS: graph building
+// CHECK-NOT: CallSlowPath GetById
+// CHECK-NOT: CallSlowPath Call @
+// CHECK: Return