Truly independent web browser
LibJS: Compress runtime-only pointers in shapes, accessors and arrays
Andreas Kling0fd9401f6fcd
diff --git a/Libraries/LibJS/Rust/generate/layout_forward.rs b/Libraries/LibJS/Rust/generate/layout_forward.rsindex aea95cfe9df..fd16c1675d3 100644--- a/Libraries/LibJS/Rust/generate/layout_forward.rs+++ b/Libraries/LibJS/Rust/generate/layout_forward.rs@@ -11,6 +11,7 @@pub struct Class;pub struct Symbol;+pub struct SpecialPropertyCache;pub struct Script;pub struct Module;pub struct ObjectEnvironment;diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rsindex 71c759a4237..12f5ddbd8f1 100644--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs@@ -37,6 +37,7 @@ use crate::layout::cell::{CellHeader, Gc};use crate::layout::executable::ExecutableHead;use crate::layout::feedback::ExecutableFeedbackHead;use crate::layout::function_object::FunctionObject;+use crate::layout::member::NullableMember;use crate::layout::object::Object;use crate::layout::property_lookup_cache::KeyedPropertyLookupCacheEntryLayout;pub use crate::layout::property_lookup_cache::{@@ -1246,7 +1247,7 @@ pub struct Executable {/// The global variable caches of the dynamic binding accesses that found their binding in the global environment,/// indexed like the environment coordinate caches. Made when the first such access needs one.dynamic_global_variable_caches: OnceCell<Box<[GlobalVariableCache]>>,- environment_shape_caches: Box<[Cell<Option<Gc<EnvironmentShape>>>]>,+ environment_shape_caches: Box<[NullableMember<EnvironmentShape>]>,template_object_caches: Box<[Gc<TemplateObjectCache>]>,object_shape_caches: Box<[ObjectShapeCache]>,object_property_iterator_caches: Box<[ObjectPropertyIteratorCache]>,@@ -1443,7 +1444,7 @@ impl Executable {.map(|_| Cell::new(EnvironmentCoordinate::invalid())).collect();let environment_shape_caches = (0..cache_counts.environment_shape_caches)- .map(|_| Cell::new(None))+ .map(|_| NullableMember::null()).collect();let registers_and_locals_count = number_of_registers + number_of_locals;let constant_count = u32::try_from(constants.len()).expect("constant count fits in u32");@@ -2010,7 +2011,7 @@ impl Executable {EnvironmentShapeCache::Executable(Gc::from_ref(self), index)}- pub(crate) fn environment_shape_cache_slot(&self, index: u32) -> &Cell<Option<Gc<EnvironmentShape>>> {+ pub(crate) fn environment_shape_cache_slot(&self, index: u32) -> &NullableMember<EnvironmentShape> {&self.environment_shape_caches[index as usize]}diff --git a/Libraries/LibJS/Rust/src/layout/accessor.rs b/Libraries/LibJS/Rust/src/layout/accessor.rsindex 5b88e907dc5..da143e41186 100644--- a/Libraries/LibJS/Rust/src/layout/accessor.rs+++ b/Libraries/LibJS/Rust/src/layout/accessor.rs@@ -8,13 +8,17 @@ use core::cell::Cell;use super::cell::{CellHeader, Gc};use super::function_object::FunctionObject;+use super::member::NullableMember;use crate::layout_forward::Symbol;#[repr(C)]pub struct Accessor {pub header: CellHeader,+ /// NB: The fields the interpreter does not read come first, in the bytes after the header.+ pub cache_on_receiver: Cell<bool>,+ pub cached_value_key: NullableMember<Symbol>,pub getter: Cell<Option<Gc<FunctionObject>>>,pub setter: Cell<Option<Gc<FunctionObject>>>,- pub cache_on_receiver: Cell<bool>,- pub cached_value_key: Cell<Option<Gc<Symbol>>>,}++const _: () = assert!(size_of::<Accessor>() == 32);diff --git a/Libraries/LibJS/Rust/src/layout/shape.rs b/Libraries/LibJS/Rust/src/layout/shape.rsindex e80ed90ea6a..4eb117cc735 100644--- a/Libraries/LibJS/Rust/src/layout/shape.rs+++ b/Libraries/LibJS/Rust/src/layout/shape.rs@@ -7,10 +7,11 @@use core::cell::Cell;use super::cell::{CellHeader, Gc};+use super::member::NullableMember;use super::object::Object;use super::property_lookup_cache::ObjectPropertyIteratorCacheData;use super::realm::Realm;-use crate::layout_forward::{ShapeStorage, WatchpointSetStorage};+use crate::layout_forward::{ShapeStorage, SpecialPropertyCache, WatchpointSetStorage};#[repr(C)]pub struct Shape {@@ -18,8 +19,17 @@ pub struct Shape {pub flags: Cell<u8>,pub realm: Cell<Gc<Realm>>,pub prototype: Cell<Option<Gc<Object>>>,- pub prototype_chain_validity: Cell<Option<Gc<WatchpointSet>>>,- pub property_iterator_cache: Cell<Option<Gc<ObjectPropertyIteratorCacheData>>>,+ // NB: The interpreter does not read these four, which fill the word pair before the fields it reads after them.+ pub prototype_chain_validity: NullableMember<WatchpointSet>,+ pub property_iterator_cache: NullableMember<ObjectPropertyIteratorCacheData>,+ /// The shape this one transitioned from, unless it changed the prototype. Transition caches only hold their+ /// targets weakly, so every shape keeps its parent alive (like V8's back pointers and JSC's previous structure): as+ /// long as any object has a shape, the whole chain that leads to it stays cached and the next object built the same+ /// way gets the same shape, also after a garbage collection.+ pub previous: NullableMember<Shape>,+ /// What Get of the properties that conversions look up gives on the objects of this shape (see+ /// `SpecialPropertyCache`).+ pub special_property_cache: NullableMember<SpecialPropertyCache>,pub property_count: Cell<u32>,pub dictionary_generation: Cell<u32>,pub storage: ShapeStorage,diff --git a/Libraries/LibJS/Rust/src/layout_forward.rs b/Libraries/LibJS/Rust/src/layout_forward.rsindex 759ea1d30d3..9e1337bed4e 100644--- a/Libraries/LibJS/Rust/src/layout_forward.rs+++ b/Libraries/LibJS/Rust/src/layout_forward.rs@@ -15,6 +15,7 @@ pub use crate::runtime::object::PrivateElements;pub use crate::runtime::private_environment::PrivateEnvironmentStorage;pub use crate::runtime::realm::RealmStorage;pub use crate::runtime::shape::ShapeStorage;+pub use crate::runtime::special_property_cache::SpecialPropertyCache;pub use crate::runtime::symbol::Symbol;pub use crate::runtime::watchpoint_set::WatchpointSetStorage;pub use ak::Utf16StringDataHeader;diff --git a/Libraries/LibJS/Rust/src/runtime/accessor.rs b/Libraries/LibJS/Rust/src/runtime/accessor.rsindex 02e9699d4f1..9d298040078 100644--- a/Libraries/LibJS/Rust/src/runtime/accessor.rs+++ b/Libraries/LibJS/Rust/src/runtime/accessor.rs@@ -12,6 +12,7 @@ use crate::interpreter::vm::Vm;pub use crate::layout::accessor::Accessor;use crate::layout::cell::{CellHeader, Gc};use crate::layout::function_object::FunctionObject;+use crate::layout::member::NullableMember;use crate::runtime::symbol::Symbol;define_cell!(Accessor, Accessor);@@ -38,7 +39,7 @@ impl Accessor {getter: Cell::new(getter),setter: Cell::new(setter),cache_on_receiver: Cell::new(false),- cached_value_key: Cell::new(cached_value_key),+ cached_value_key: NullableMember::new(cached_value_key),})}diff --git a/Libraries/LibJS/Rust/src/runtime/array.rs b/Libraries/LibJS/Rust/src/runtime/array.rsindex 0ae5bb5fcaf..0c05acb4b51 100644--- a/Libraries/LibJS/Rust/src/runtime/array.rs+++ b/Libraries/LibJS/Rust/src/runtime/array.rs@@ -12,6 +12,7 @@ use crate::gc::class::{Class, GcCell, cast_ref, define_cell};use crate::gc::root::MarkedVec;use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;+use crate::layout::member::Member;use crate::layout::value::Value;use crate::runtime::abstract_operations::call_function_object;use crate::runtime::array_prototype::array_merge_sort;@@ -37,11 +38,13 @@ use crate::utf16::Utf16View;#[derive(Trace)]pub struct Array {base: Object,- realm: Cell<Gc<Realm>>,+ realm: Member<Realm>,length_writable: Cell<bool>,is_proxy_target: Cell<bool>,}+const _: () = assert!(size_of::<Array>() == 80);+/// The offsets of the flags JIT code checks before writing elements of an array.pub const ARRAY_LENGTH_WRITABLE_OFFSET: usize = core::mem::offset_of!(Array, length_writable);pub const ARRAY_IS_PROXY_TARGET_OFFSET: usize = core::mem::offset_of!(Array, is_proxy_target);@@ -99,7 +102,7 @@ impl Array {};let array = Self {base,- realm: Cell::new(realm),+ realm: Member::new(realm),length_writable: Cell::new(true),is_proxy_target: Cell::new(false),};diff --git a/Libraries/LibJS/Rust/src/runtime/environment_shape.rs b/Libraries/LibJS/Rust/src/runtime/environment_shape.rsindex 278928f6493..7d65ca73032 100644--- a/Libraries/LibJS/Rust/src/runtime/environment_shape.rs+++ b/Libraries/LibJS/Rust/src/runtime/environment_shape.rs@@ -4,7 +4,6 @@* SPDX-License-Identifier: BSD-2-Clause*/-use core::cell::Cell;use std::collections::HashMap;use ak::Utf16FlyString;@@ -18,6 +17,7 @@ use crate::layout::buffer::InterpreterBuffer;use crate::layout::cell::{CellHeader, Gc};use crate::layout::environment::BINDING_FLAG_MUTABLE;pub use crate::layout::environment::EnvironmentShape;+use crate::layout::member::NullableMember;use crate::runtime::shared_function_instance_data::SharedFunctionInstanceData;/// The names of an environment shape's bindings, whose flags the interpreter reads from the shape itself.@@ -115,7 +115,7 @@ pub enum EnvironmentShapeCache {}impl EnvironmentShapeCache {- fn slot(&self) -> &Cell<Option<Gc<EnvironmentShape>>> {+ fn slot(&self) -> &NullableMember<EnvironmentShape> {match self {Self::Executable(executable, index) => executable.environment_shape_cache_slot(*index),Self::FunctionEnvironment(shared_data) => shared_data.function_environment_shape_slot(),diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rsindex fd20d751719..f9d83d55763 100644--- a/Libraries/LibJS/Rust/src/runtime/shape.rs+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs@@ -20,6 +20,7 @@ use crate::gc::weak::GcWeak;use crate::interpreter::vm::Vm;use crate::jit::dependencies::{CellId, Dependency};use crate::layout::cell::{CellHeader, Gc};+use crate::layout::member::NullableMember;use crate::layout::object::Object;use crate::layout::property_lookup_cache::ObjectPropertyIteratorCacheData;pub use crate::layout::shape::Shape;@@ -145,34 +146,24 @@ pub struct ShapeStorage {property_storage: GcRefCell<PropertyStorage>,forward_transitions: GcRefCell<ForwardTransitions>,rare_data: GcRefCell<Option<Box<RareData>>>,- /// The shape this one transitioned from, unless it changed the prototype. Transition caches only hold their- /// targets weakly, so every shape keeps its parent alive (like V8's back pointers and JSC's previous structure): as- /// long as any object has a shape, the whole chain that leads to it stays cached and the next object built the same- /// way gets the same shape, also after a garbage collection.- previous: Cell<Option<Gc<Shape>>>,- /// What Get of the properties that conversions look up gives on the objects of this shape (see- /// `SpecialPropertyCache`).- special_property_cache: Cell<Option<Gc<SpecialPropertyCache>>>,}+// NB: Every shape has these bytes.+const _: () = assert!(size_of::<Shape>() == 128);+impl ShapeStorage {fn new(_: &DeferGc<'_>) -> Self {Self {property_storage: GcRefCell::new(PropertyStorage::Descriptors(None)),forward_transitions: GcRefCell::new(ForwardTransitions::Empty),rare_data: GcRefCell::new(None),- previous: Cell::new(None),- special_property_cache: Cell::new(None),}}}-// SAFETY: Visits the descriptor array, the keys this storage owns, the previous shape, the special property cache and-// the rare data. The shapes of transitions are weak.+// SAFETY: Visits the descriptor array, the keys this storage owns and the rare data. The shapes of transitions are weak.unsafe impl Trace for ShapeStorage {fn trace(&self, visitor: &mut Visitor) {- self.previous.trace(visitor);- self.special_property_cache.trace(visitor);self.property_storage.trace(visitor);self.forward_transitions.trace(visitor);self.rare_data.trace(visitor);@@ -244,13 +235,16 @@ unsafe impl Trace for RareData {define_cell!(Shape, Other);-// SAFETY: Visits the realm, the prototype, the validity, the iterator cache and what the storage owns.+// SAFETY: Visits the realm, the prototype, the validity, the iterator cache, the previous shape, the special property+// cache and what the storage owns.unsafe impl Trace for Shape {fn trace(&self, visitor: &mut Visitor) {self.realm.trace(visitor);self.prototype.trace(visitor);self.prototype_chain_validity.trace(visitor);self.property_iterator_cache.trace(visitor);+ self.previous.trace(visitor);+ self.special_property_cache.trace(visitor);self.storage.trace(visitor);}}@@ -332,8 +326,10 @@ impl Shape {flags: Cell::new(0),realm: Cell::new(realm),prototype: Cell::new(None),- prototype_chain_validity: Cell::new(None),- property_iterator_cache: Cell::new(None),+ prototype_chain_validity: NullableMember::null(),+ property_iterator_cache: NullableMember::null(),+ previous: NullableMember::null(),+ special_property_cache: NullableMember::null(),property_count: Cell::new(0),dictionary_generation: Cell::new(0),storage: ShapeStorage::new(defer_gc),@@ -350,7 +346,7 @@ impl Shape {.flags.set(previous_shape.flags.get() & shape_flag::HAS_PARAMETER_MAP);shape.prototype.set(previous_shape.prototype());- shape.storage.previous.set(Some(previous_shape.as_gc()));+ shape.previous.set(Some(previous_shape.as_gc()));let mut property_count = previous_shape.property_count();match property_count_change {PropertyCountChange::Preserve => {}@@ -1114,12 +1110,12 @@ impl Shape {}pub fn special_property_cache(&self) -> Option<Gc<SpecialPropertyCache>> {- self.storage.special_property_cache.get()+ self.special_property_cache.get()}pub fn set_special_property_cache(&self, cache: Gc<SpecialPropertyCache>) {assert!(!self.is_dictionary() && !self.is_prototype_shape());- self.storage.special_property_cache.set(Some(cache));+ self.special_property_cache.set(Some(cache));}/// The watchpoint set that fires when the value in slot `offset` of the object of this prototype shape changes, ordiff --git a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rsindex 5ecc59b8602..2450ac52ab6 100644--- a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs+++ b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs@@ -28,6 +28,7 @@ use crate::interpreter::vm::Vm;use crate::layout::cell::{CellHeader, Gc};use crate::layout::function_object::CallEnvironmentTemplate;pub use crate::layout::function_object::{SharedFunctionInstanceData, asm_call_metadata};+use crate::layout::member::NullableMember;use crate::runtime::environment_shape::{EnvironmentShape, EnvironmentShapeCache};use crate::runtime::private_environment::PrivateName;use crate::runtime::property_key::PropertyKey;@@ -106,8 +107,8 @@ pub struct SharedFunctionInstanceDataStorage {function_environment_bindings_count: Cell<usize>,var_environment_bindings_count: Cell<usize>,- function_environment_shape: Cell<Option<Gc<EnvironmentShape>>>,- var_environment_shape: Cell<Option<Gc<EnvironmentShape>>>,+ function_environment_shape: NullableMember<EnvironmentShape>,+ var_environment_shape: NullableMember<EnvironmentShape>,class_field_initializer_name: GcRefCell<ClassFieldInitializerName>, // [[ClassFieldInitializerName]]#[gc(untraced)]@@ -141,6 +142,9 @@ pub struct SharedFunctionInstanceDataStorage {closure_count: Cell<ClosureCount>,}+// NB: Every function has these bytes.+const _: () = assert!(size_of::<SharedFunctionInstanceData>() == 280);+/// The most properties that constructions make room for ahead (the largest inline capacity of plain objects).const MAX_CONSTRUCT_RESERVE: u32 = 16;@@ -246,8 +250,8 @@ impl SharedFunctionInstanceData {this_value_needs_environment_resolution: Cell::new(false),function_environment_bindings_count: Cell::new(0),var_environment_bindings_count: Cell::new(0),- function_environment_shape: Cell::new(None),- var_environment_shape: Cell::new(None),+ function_environment_shape: NullableMember::null(),+ var_environment_shape: NullableMember::null(),class_field_initializer_name: GcRefCell::new(ClassFieldInitializerName::Empty),constructor_kind: Cell::new(ConstructorKind::Base),is_class_constructor: Cell::new(false),@@ -801,11 +805,11 @@ impl SharedFunctionInstanceData {EnvironmentShapeCache::VarEnvironment(Gc::from_ref(self))}- pub(crate) fn function_environment_shape_slot(&self) -> &Cell<Option<Gc<EnvironmentShape>>> {+ pub(crate) fn function_environment_shape_slot(&self) -> &NullableMember<EnvironmentShape> {&self.storage.function_environment_shape}- pub(crate) fn var_environment_shape_slot(&self) -> &Cell<Option<Gc<EnvironmentShape>>> {+ pub(crate) fn var_environment_shape_slot(&self) -> &NullableMember<EnvironmentShape> {&self.storage.var_environment_shape}