Truly independent web browser
LibJS+LibWeb: Cache missing properties of objects with indexed getters
Tim Ledbetterfc12c41700c5
diff --git a/Libraries/LibJS/HostClassBuilder.h b/Libraries/LibJS/HostClassBuilder.hindex 1f36f0af644..71c9544aec2 100644--- a/Libraries/LibJS/HostClassBuilder.h+++ b/Libraries/LibJS/HostClassBuilder.h@@ -559,6 +559,7 @@ consteval JSHostClass make_host_class(u8 kind, StringView name, JSHostClass cons| JS_HOST_CLASS_IS_GLOBAL_OBJECT;constexpr u32 host_object_flags = JS_HOST_CLASS_IMMUTABLE_PROTOTYPE| JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE+ | JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE| JS_HOST_CLASS_NOT_ELIGIBLE_FOR_OWN_PROPERTY_ENUMERATION_FAST_PATH;u32 allowed_flags = JS_HOST_CLASS_SHARES_ALLOCATOR_WITH_PARENT;diff --git a/Libraries/LibJS/HostObjectABI.h b/Libraries/LibJS/HostObjectABI.hindex 799708ebb41..94aaa8d63f2 100644--- a/Libraries/LibJS/HostObjectABI.h+++ b/Libraries/LibJS/HostObjectABI.h@@ -135,9 +135,10 @@ enum {//// Inline caches remember that a key is missing from an object, or found further up its prototype chain, and answer// from that without calling hooks while the shapes involved stay the same. Whether the engine may do so depends only- // on JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE and the is_cacheable_for_inherited_property hook, never on- // which other hooks a class has. A class whose hooks can answer for keys missing from its shape must set the flag,- // and one whose hooks can start shadowing inherited properties must have that hook return false.+ // on JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE, JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE and+ // the is_cacheable_for_inherited_property hook, never on which other hooks a class has. A class whose hooks can+ // answer for keys missing from its shape must set the first flag, or the second if they only answer for array+ // indices, and one whose hooks can start shadowing inherited properties must have that hook return false.//// Enumeration (for-in, Object.keys, Object.assign, object spread, JSON.stringify) has fast paths that read keys,// attributes and values straight from the shape and storage. The engine never takes them for an object whose class@@ -146,6 +147,7 @@ enum {JS_HOST_CLASS_IMMUTABLE_PROTOTYPE = 1 << 5,JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE = 1 << 6,JS_HOST_CLASS_NOT_ELIGIBLE_FOR_OWN_PROPERTY_ENUMERATION_FAST_PATH = 1 << 7,+ JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE = 1 << 10,// Only for JS_HOST_CLASS_FUNCTION, which must then have a construct hook.JS_HOST_CLASS_HAS_CONSTRUCTOR = 1 << 8,diff --git a/Libraries/LibJS/Rust/generate/layout_header.rs b/Libraries/LibJS/Rust/generate/layout_header.rsindex 8a7591892d9..43801666b03 100644--- a/Libraries/LibJS/Rust/generate/layout_header.rs+++ b/Libraries/LibJS/Rust/generate/layout_header.rs@@ -493,6 +493,7 @@ pub fn generate() -> LayoutHeader {JS_HOST_CLASS_NOT_ELIGIBLE_FOR_OWN_PROPERTY_ENUMERATION_FAST_PATH,JS_HOST_CLASS_HAS_CONSTRUCTOR,JS_HOST_CLASS_SHARES_ALLOCATOR_WITH_PARENT,+ JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE,JS_RESOLVED_BINDING_BINDING_NAME,JS_RESOLVED_BINDING_NAMESPACE,JS_RESOLVED_BINDING_AMBIGUOUS,diff --git a/Libraries/LibJS/Rust/src/embedding/host/class_table.rs b/Libraries/LibJS/Rust/src/embedding/host/class_table.rsindex 53612d38c2d..0067edfda1c 100644--- a/Libraries/LibJS/Rust/src/embedding/host/class_table.rs+++ b/Libraries/LibJS/Rust/src/embedding/host/class_table.rs@@ -16,7 +16,7 @@ use crate::layout::host_class::{JS_HOST_ABI_VERSION, JS_HOST_CLASS_ARRAY, JS_HOST_CLASS_FUNCTION, JS_HOST_CLASS_HAS_CONSTRUCTOR,JS_HOST_CLASS_IMMUTABLE_PROTOTYPE, JS_HOST_CLASS_IS_GLOBAL_OBJECT, JS_HOST_CLASS_IS_HTMLDDA,JS_HOST_CLASS_IS_PLATFORM_OBJECT, JS_HOST_CLASS_MAY_INTERFERE_WITH_INDEXED_PROPERTY_ACCESS, JS_HOST_CLASS_MODULE,- JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE,+ JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE, JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE,JS_HOST_CLASS_NOT_ELIGIBLE_FOR_OWN_PROPERTY_ENUMERATION_FAST_PATH, JS_HOST_CLASS_OBJECT,JS_HOST_CLASS_REQUIRES_SLOW_ADD_OWN_PROPERTY, JS_HOST_CLASS_SHARES_ALLOCATOR_WITH_PARENT,JS_PROPERTY_LOOKUP_PHASE_OWN_PROPERTY, JS_PROPERTY_LOOKUP_PHASE_PROTOTYPE_CHAIN, JSCompletion, JSGetCacheMetadata,@@ -35,6 +35,7 @@ const FLAGS_COPIED_INTO_OBJECTS: u32 = JS_HOST_CLASS_IS_PLATFORM_OBJECTconst FLAGS_OF_HOST_OBJECTS_ONLY: u32 = JS_HOST_CLASS_IMMUTABLE_PROTOTYPE| JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE+ | JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE| JS_HOST_CLASS_NOT_ELIGIBLE_FOR_OWN_PROPERTY_ENUMERATION_FAST_PATH;static HOST_OBJECT_HOOKS_THAT_ARE_ALL_ORDINARY: JSHostObjectHooks = JSHostObjectHooks {diff --git a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs b/Libraries/LibJS/Rust/src/embedding/host/host_object.rsindex 8431b8875fe..b26daab4bff 100644--- a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs+++ b/Libraries/LibJS/Rust/src/embedding/host/host_object.rs@@ -41,7 +41,8 @@ use crate::gc::visitor::{Trace, Visitor};use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::host_class::{- JS_HOST_CLASS_IMMUTABLE_PROTOTYPE, JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE,+ JS_HOST_CLASS_IMMUTABLE_PROTOTYPE, JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE,+ JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE,JS_HOST_CLASS_NOT_ELIGIBLE_FOR_OWN_PROPERTY_ENUMERATION_FAST_PATH, JS_HOST_CLASS_OBJECT, JS_PD_CONFIGURABLE,JS_PD_ENUMERABLE, JS_PD_HAS_CONFIGURABLE, JS_PD_HAS_ENUMERABLE, JS_PD_HAS_VALUE, JS_PD_HAS_WRITABLE, JS_PD_PRESENT,JS_PD_WRITABLE, JSHostClass, JSHostObjectHooks, JSObject, JSPropertyDescriptor, JSVM, JSValue, JSValueSink,@@ -133,6 +134,8 @@ fn host_object_methods(table: &'static JSHostClass) -> ObjectMethods {}if table.has_flag(JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE) {methods.is_cacheable_for_property_absence = |_| false;+ } else if table.has_flag(JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE) {+ methods.is_cacheable_for_absence_of = |_, _, property_key| !property_key.is_number();}if hooks.is_cacheable_for_inherited_property.is_some() {methods.is_cacheable_for_inherited_property = is_cacheable_for_inherited_property_through_hook;diff --git a/Libraries/LibJS/Rust/src/layout/host_class.rs b/Libraries/LibJS/Rust/src/layout/host_class.rsindex 14e8b3965d6..9f2f330d25f 100644--- a/Libraries/LibJS/Rust/src/layout/host_class.rs+++ b/Libraries/LibJS/Rust/src/layout/host_class.rs@@ -107,6 +107,7 @@ pub const JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE: u32 = 1 << 6;pub const JS_HOST_CLASS_NOT_ELIGIBLE_FOR_OWN_PROPERTY_ENUMERATION_FAST_PATH: u32 = 1 << 7;pub const JS_HOST_CLASS_HAS_CONSTRUCTOR: u32 = 1 << 8;pub const JS_HOST_CLASS_SHARES_ALLOCATOR_WITH_PARENT: u32 = 1 << 9;+pub const JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE: u32 = 1 << 10;#[repr(C)]pub struct JSHostObjectHooks {diff --git a/Meta/Generators/libweb_bindings/wrappers.py b/Meta/Generators/libweb_bindings/wrappers.pyindex 4d916a153de..134f5cc0658 100644--- a/Meta/Generators/libweb_bindings/wrappers.py+++ b/Meta/Generators/libweb_bindings/wrappers.py@@ -150,11 +150,19 @@ def legacy_platform_object_info_fields(context: GenerationContext, interface: Inreturn [field for field in LEGACY_PLATFORM_OBJECT_INFO_FIELDS if field in fields]+# The LegacyPlatformObjectInfo fields of wrappers whose hooks only answer for array indices.+INDEXED_PROPERTY_FIELDS = {+ "supports_indexed_properties",+ "has_indexed_property_setter",+ "indexed_property_setter_has_identifier",+}++def wrapper_host_class_flags(context: GenerationContext, interface: Interface) -> list[str]:interfaces = interface_and_inherited_interfaces(context, interface)is_location = any(interface_in_chain.name == "Location" for interface_in_chain in interfaces)is_global = any("Global" in interface_in_chain.extended_attributes for interface_in_chain in interfaces)- is_legacy_platform_object = legacy_platform_object_info_fields(context, interface) is not None+ legacy_platform_object_fields = legacy_platform_object_info_fields(context, interface)flags = ["platform_object_host_class_flags"]if is_location:@@ -165,7 +173,9 @@ def wrapper_host_class_flags(context: GenerationContext, interface: Interface) -flags.append("JS_HOST_CLASS_IS_GLOBAL_OBJECT")if is_global:flags.append("JS_HOST_CLASS_IMMUTABLE_PROTOTYPE")- if is_legacy_platform_object or is_location:+ if legacy_platform_object_fields is not None and set(legacy_platform_object_fields) <= INDEXED_PROPERTY_FIELDS:+ flags.append("JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE")+ elif legacy_platform_object_fields is not None or is_location:flags.append("JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE")return flags