Truly independent web browser
LibJS: Keep string constants as fly strings from the start
Andreas Kling85a0bbe90ace
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constants.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constants.rsindex c99253da25d..f7e407abfc9 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constants.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constants.rs@@ -10,12 +10,14 @@use crate::bytecode::constant::AbstractOperationKind;use crate::bytecode::constant::WellKnownSymbolKind;use crate::bytecode::executable_data::ConstantValue;+use crate::bytecode::executable_data::MAXIMUM_INTERNED_CONSTANT_LENGTH;use crate::bytecode::operand::Operand;use crate::u32_from_usize;use crate::utf16::Utf16View;use ak::Utf16String;use super::context::MAXIMUM_SPARE_CAPACITY;+use super::names::NameTables;/// An open-addressed table from a key to a constant index that forgets everything by moving on to a new generation.#[derive(Default)]@@ -214,9 +216,10 @@ impl ConstantTable {Operand::constant(index)}- /// A string whose value is `text`, which is only copied if the table does not have it yet.+ /// A string whose value is `text`, which is only looked at if the table does not have it yet: a text that gets+ /// interned when the code is linked comes from what `names` knows of the script, and any other gets copied.#[inline(always)]- pub(super) fn add_string_view(&mut self, text: Utf16View<'_>) -> Operand {+ pub(super) fn add_string_view(&mut self, text: Utf16View<'_>, names: &mut NameTables) -> Operand {let key = hash_of_units(text);let values = &self.values;let found = self.strings.find(key, |index| {@@ -225,7 +228,12 @@ impl ConstantTable {match found {Ok(index) => Operand::constant(index),Err(slot) => {- let index = self.append(ConstantValue::String(text.into()));+ let string = if text.length_in_code_units() <= MAXIMUM_INTERNED_CONSTANT_LENGTH {+ Utf16String::from(names.fly_string_of_text(text, key))+ } else {+ text.into()+ };+ let index = self.append(ConstantValue::String(string));self.strings.insert_at(slot, key, index);Operand::constant(index)}diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rsindex 227b53fcf0e..59311d67494 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs@@ -358,13 +358,13 @@ impl Generator<'_> {fn sweep_class_element_key(&mut self, key: ClassElementKey, first_row: usize) -> Result<Operand, OutOfStack> {let tree = self.tree;let key = match key {- ClassElementKey::Name(name) => return Ok(self.context.constants.add_string_view(tree.name_text(name))),+ ClassElementKey::Name(name) => return Ok(self.add_constant_string_view(tree.name_text(name))),ClassElementKey::Expression(key) => key,};match tree.node_tag(key) {tag::STR_KEY => {let operand = match tree.string_literal_text(key) {- StringLiteralText::Source(text) => self.context.constants.add_string_view(text),+ StringLiteralText::Source(text) => self.add_constant_string_view(text),StringLiteralText::Processed(value) => self.context.constants.add_string(value),};Ok(operand)@@ -545,7 +545,7 @@ impl Generator<'_> {let mut strings = Vec::with_capacity(elements.len() + 1);for element in elements.iter().step_by(2) {let value = match tree.template_chunk_text(element, EscapeMode::TaggedTemplate) {- Some(StringLiteralText::Source(text)) => self.context.constants.add_string_view(text),+ Some(StringLiteralText::Source(text)) => self.add_constant_string_view(text),Some(StringLiteralText::Processed(value)) => self.context.constants.add_string(value),None => self.context.constants.add_undefined(),};@@ -556,7 +556,7 @@ impl Generator<'_> {strings.push(if units.code_units().any(|unit| unit == u16::from(b'\r')) {self.context.constants.add_string(raw_template_value(units))} else {- self.context.constants.add_string_view(units)+ self.add_constant_string_view(units)});}let first_temporary = self.next_temporary();@@ -603,7 +603,7 @@ impl Generator<'_> {.template_chunk_text(element, EscapeMode::StringLiteral).expect("only a tagged template can have an invalid escape");let operand = match text {- StringLiteralText::Source(text) => self.context.constants.add_string_view(text),+ StringLiteralText::Source(text) => self.add_constant_string_view(text),StringLiteralText::Processed(value) => self.context.constants.add_string(value),};(operand, true)@@ -1682,7 +1682,7 @@ impl Generator<'_> {self.put_get_by_id(property_value, object, name);if has_rest {let text = tree.name_text(name);- excluded_names.push(self.context.constants.add_string_view(text));+ excluded_names.push(self.add_constant_string_view(text));}// NB: The row of the key is the identifier that gets bound, which has no rows of its own.element = NodeId(tree.node_b(item));@@ -1727,7 +1727,7 @@ impl Generator<'_> {}if has_rest {let name = match &text {- StringLiteralText::Source(view) => self.context.constants.add_string_view(*view),+ StringLiteralText::Source(view) => self.add_constant_string_view(*view),StringLiteralText::Processed(value) => self.context.constants.add_string(value.clone()),};excluded_names.push(name);diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/context.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/context.rsindex 02afb34b21a..b3afc439a45 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/context.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/context.rs@@ -258,6 +258,13 @@ impl<'t> Generator<'t> {ScopedOperand::plain(self.context.constants.add_string(value))}+ /// A constant of the string `text`.+ #[inline(always)]+ pub(super) fn add_constant_string_view(&mut self, text: Utf16View<'_>) -> Operand {+ let context = &mut *self.context;+ context.constants.add_string_view(text, &mut context.names)+ }+pub(super) fn add_constant_bigint(&mut self, value: String) -> ScopedOperand {ScopedOperand::plain(self.context.constants.add_bigint(value))}diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/names.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/names.rsindex 2f9361cdd42..be56b85b172 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/names.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/names.rs@@ -405,6 +405,12 @@ impl NameTables {}}+ /// The fly string of a text with `hash`, which only takes the global table once per text of a script on this thread.+ pub(super) fn fly_string_of_text(&mut self, text: Utf16View<'_>, hash: u64) -> &Utf16FlyString {+ let id = self.script_names.text_id(text, hash);+ &self.script_names.texts[id as usize]+ }+/// The fly string of a name, which only takes the global table once per name of a script on this thread.pub(super) fn fly_string_of(&mut self, tree: &SyntaxTree, name: Name) -> Utf16FlyString {self.script_names.fly_string(tree, name).clone()diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rsindex 9758d8ee9de..e81dfc1086b 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs@@ -184,7 +184,7 @@ impl Generator<'_> {tag::NUM => self.value_of_number(tree.number_value(node)),tag::STR => {let operand = match tree.string_literal_text(node) {- StringLiteralText::Source(text) => self.context.constants.add_string_view(text),+ StringLiteralText::Source(text) => self.add_constant_string_view(text),StringLiteralText::Processed(value) => self.context.constants.add_string(value),};self.value_of_constant(operand);diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rsindex 19d2a359993..82095f83711 100644--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs@@ -17,7 +17,7 @@ use crate::bytecode::class_blueprint::ClassBlueprint;use crate::bytecode::constant::{AbstractOperationKind, WellKnownSymbolKind};use crate::bytecode::dump::{count_basic_blocks, dump_bytecode};use crate::bytecode::encoding::{IdentifierTableIndex, PropertyKeyTableIndex, StringTableIndex};-use crate::bytecode::executable_data::{ConstantValue, ExceptionHandler};+use crate::bytecode::executable_data::{ConstantValue, ExceptionHandler, MAXIMUM_INTERNED_CONSTANT_LENGTH};use crate::bytecode::executable_data::{ExecutableData, LocalVariableMetadata};use crate::bytecode::feedback::{ExecutableFeedback, FeedbackSlotCounts};use crate::bytecode::source_map::SourceMapEntry;@@ -59,6 +59,7 @@ use crate::slice_block::{SliceBlock, SliceBlockBuilder};use crate::source_code::SourceCode;use crate::source_range::{Position, SourceRange};use crate::utf16::Utf16View;+use crate::utf16::to_utf16_fly_string;use libjs_runtime_macros::Trace;/// The contents of a cache entry, copied out of the cache so that no reference into its storage outlives a call that@@ -2029,16 +2030,11 @@ fn constant_value(vm: &Vm, constant: &ConstantValue) -> Value {ConstantValue::Undefined => Value::UNDEFINED,ConstantValue::Empty => Value::EMPTY,ConstantValue::String(string) => {- // String constants are interned so that comparisons with them are pointer comparisons. Long ones are- // rarely compared, and not worth hashing.- const MAX_INTERNED_CONSTANT_LENGTH: usize = 256;- if string.length_in_code_units() > MAX_INTERNED_CONSTANT_LENGTH {+ // String constants are interned so that comparisons with them are pointer comparisons.+ if string.length_in_code_units() > MAXIMUM_INTERNED_CONSTANT_LENGTH {return Value::from_string(PrimitiveString::create(vm, string.clone()));}- Value::from_string(PrimitiveString::create_interned(- vm,- &Utf16View::of_string(string).to_utf16_fly_string(),- ))+ Value::from_string(PrimitiveString::create_interned(vm, &to_utf16_fly_string(string)))}ConstantValue::BigInt(literal) => Value::from_bigint(BigInt::create(vm, parse_big_int_literal(literal))),ConstantValue::WellKnownSymbol(WellKnownSymbolKind::SymbolIterator) => {diff --git a/Libraries/LibJS/Rust/src/bytecode/executable_data.rs b/Libraries/LibJS/Rust/src/bytecode/executable_data.rsindex 539982daa50..faba5054cdb 100644--- a/Libraries/LibJS/Rust/src/bytecode/executable_data.rs+++ b/Libraries/LibJS/Rust/src/bytecode/executable_data.rs@@ -29,6 +29,9 @@ pub struct ExceptionHandler {pub catches_exception: bool,}+/// The longest string constant that gets interned. Long ones are rarely compared, and not worth hashing.+pub(crate) const MAXIMUM_INTERNED_CONSTANT_LENGTH: usize = 256;+/// A typed constant value stored in the constant pool.////// The actual NaN-boxed encoding happens at the FFI boundary when