Truly independent web browser
LibJS: Compress the halves of rope strings and substring sources
Andreas Kling99a19ca7911f
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rsindex e8d27b6d888..ccc4f924077 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs@@ -665,7 +665,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {/// Whether the fields at `stored` (offsets and sizes of 4 or 8 bytes), which/// allocation stores itself, are aligned to their sizes and within the first/// `template_bytes` bytes of a cell.-fn stored_fields_fit(stored: &[(u32, u32)], template_bytes: u64) -> bool {+pub(super) fn stored_fields_fit(stored: &[(u32, u32)], template_bytes: u64) -> bool {stored.iter().all(|&(offset, size)| {matches!(size, 4 | 8) && offset.is_multiple_of(size) && u64::from(offset) + u64::from(size) <= template_bytes})@@ -674,7 +674,7 @@ fn stored_fields_fit(stored: &[(u32, u32)], template_bytes: u64) -> bool {/// The constant fields of a cell image `words` without the fields at/// `stored` (see `stored_fields_fit()`): whole words, and the halves of the/// words that share theirs with a 4-byte stored field.-fn template_fields(words: &[u64], stored: &[(u32, u32)]) -> Vec<ConstantField> {+pub(super) fn template_fields(words: &[u64], stored: &[(u32, u32)]) -> Vec<ConstantField> {let is_stored = |offset: u32, size: u32| {stored.iter()diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rs b/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rsindex 30231d458f0..cadb6a987f1 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rs@@ -16,7 +16,8 @@use super::Codegen;use super::allocation::AllocationCall;-use super::call::ConstantField;+use super::allocation::stored_fields_fit;+use super::allocation::template_fields;use crate::asm::Address;use crate::asm::Condition;use crate::asm::Gpr;@@ -25,6 +26,7 @@ use crate::asm::PortableMacroAssembler;use crate::asm::Scale;use crate::ir::NodeId;use crate::ir::value;+use crate::snapshot::RopeAllocationInfo;impl<M: PortableMacroAssembler> Codegen<'_, M> {/// `ConcatenateStrings`: the concatenation of two strings, or the empty@@ -124,9 +126,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {let rope = &self.runtime.rope_allocation;let bytes = 8 * rope.template.len() as u64;bytes <= u64::from(rope.cell_size)- && [rope.lhs_offset, rope.rhs_offset]- .iter()- .all(|offset| offset.is_multiple_of(8) && u64::from(*offset) + 8 <= bytes)+ && stored_fields_fit(&rope_stored_fields(rope), bytes)&& u64::from(rope.length_offset) + 4 <= bytes}@@ -182,18 +182,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.emit_pop_free_list(rope.allocator, cell, other, no_free_cell);self.emit_count_allocation(rope.cell_size, rope.cell_size, other, false);- let mut fields: Vec<ConstantField> = (0u32..)- .zip(&rope.template)- .map(|(index, word)| (8 * index, 8, *word))- .filter(|(offset, _, _)| *offset != rope.lhs_offset && *offset != rope.rhs_offset)- .collect();+ let mut fields = template_fields(&rope.template, &rope_stored_fields(&rope));self.emit_constant_fields(cell, other, &mut fields).expect("rope string fields are near the cell");self.masm.store32(&string_length(cell), length);- self.emit_unbox_cell(other, lhs);- self.masm.store64(&at(cell, rope.lhs_offset), other);- self.emit_unbox_cell(other, rhs);- self.masm.store64(&at(cell, rope.rhs_offset), other);+ // NB: The halves are compressed from their string values, whose tag+ // the 32-bit stores drop.+ self.emit_store_compressed_cell(&at(cell, rope.lhs_offset), lhs, other);+ self.emit_store_compressed_cell(&at(cell, rope.rhs_offset), rhs, other);self.box_cell_with_tag(output, cell, value::STRING_TAG);self.masm.jump(done);@@ -269,3 +265,9 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.jump(done);}}++/// The fields JIT code stores into a new rope string itself: its compressed+/// halves.+fn rope_stored_fields(rope: &RopeAllocationInfo) -> [(u32, u32); 2] {+ [(rope.lhs_offset, 4), (rope.rhs_offset, 4)]+}diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rsindex 3306ee39f14..8b1c9ba64b2 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs@@ -3899,7 +3899,7 @@ fn short_string_concatenations_through_the_fly_string_cache() {cell_size: 64,template: vec![0; 8],lhs_offset: 32,- rhs_offset: 40,+ rhs_offset: 36,length_offset: 16,min_length: 8,slow_path: 0,@@ -4469,7 +4469,7 @@ fn rope_strings_come_from_the_runtime_without_a_local_free_cell() {cell_size: 64,template: vec![0x1234; 8],lhs_offset: 32,- rhs_offset: 40,+ rhs_offset: 36,length_offset: 16,min_length: 8,slow_path: fake_create_rope_string as *const () as u64,@@ -4501,7 +4501,19 @@ fn rope_strings_come_from_the_runtime_without_a_local_free_cell() {// A local free cell becomes the rope string.let (result, strings, cell, address) = concatenate(lhs, rhs, 10_000, true);assert_eq!((result, strings), (string(address), [0, 0]));- assert_eq!(cell, [0x1234, 0x1234, 9, 0x1234, pointer(lhs), pointer(rhs)]);+ // NB: The halves are compressed, and the heap region base is 0 here.+ let compressed = |string: u64| (pointer(string) >> 3) & u64::from(u32::MAX);+ assert_eq!(+ cell,+ [+ 0x1234,+ 0x1234,+ 9,+ 0x1234,+ compressed(lhs) | compressed(rhs) << 32,+ 0x1234+ ]+ );// Without one, or without room before the next collection, the runtime// makes it.diff --git a/Libraries/LibJS/Rust/src/jit/allocation.rs b/Libraries/LibJS/Rust/src/jit/allocation.rsindex 28c42e01b25..eb9339720df 100644--- a/Libraries/LibJS/Rust/src/jit/allocation.rs+++ b/Libraries/LibJS/Rust/src/jit/allocation.rs@@ -200,8 +200,8 @@ fn rope_template(vm: &Vm) -> Vec<u64> {let rope = PrimitiveString::create_from_concatenation(vm, half, half).must();assert!(class_of(rope).is_subclass_of(RopeString::CLASS));let mut words = cell_words(rope, size_of::<RopeString>());- words[ROPE_STRING_LHS_OFFSET / 8] = 0;- words[ROPE_STRING_RHS_OFFSET / 8] = 0;+ clear_bytes(&mut words, ROPE_STRING_LHS_OFFSET, 4);+ clear_bytes(&mut words, ROPE_STRING_RHS_OFFSET, 4);words}diff --git a/Libraries/LibJS/Rust/src/runtime/primitive_string.rs b/Libraries/LibJS/Rust/src/runtime/primitive_string.rsindex 51d536315c7..3db77c68bb6 100644--- a/Libraries/LibJS/Rust/src/runtime/primitive_string.rs+++ b/Libraries/LibJS/Rust/src/runtime/primitive_string.rs@@ -14,6 +14,7 @@ use crate::gc::root::MarkedVec;use crate::gc::visitor::{Trace, Visitor};use crate::interpreter::vm::{StringToAtomCacheEntry, Vm};use crate::layout::cell::{CellHeader, Gc};+use crate::layout::member::NullableMember;use crate::layout::primitive_string::{DEFERRED_KIND_MASK, INTERNED_FLAG};pub use crate::layout::primitive_string::{DeferredKind, INLINE_STRING_CAPACITY, InlineString, PrimitiveString};use crate::layout::value::Value;@@ -50,10 +51,12 @@ unsafe impl Trace for PrimitiveString {#[derive(Trace)]pub struct RopeString {base: PrimitiveString,- lhs: Cell<Option<Gc<PrimitiveString>>>,- rhs: Cell<Option<Gc<PrimitiveString>>>,+ lhs: NullableMember<PrimitiveString>,+ rhs: NullableMember<PrimitiveString>,}+const _: () = assert!(size_of::<RopeString>() == 32);+/// Where a rope string keeps its two halves, for JIT code that creates rope strings itself.pub const ROPE_STRING_LHS_OFFSET: usize = core::mem::offset_of!(RopeString, lhs);pub const ROPE_STRING_RHS_OFFSET: usize = core::mem::offset_of!(RopeString, rhs);@@ -66,10 +69,13 @@ pub const ROPE_STRING_MIN_LENGTH: u32 = crate::utf16::MAX_SHORT_STRING_BYTE_COUN#[derive(Trace)]pub struct Substring {base: PrimitiveString,- source_string: Cell<Option<Gc<PrimitiveString>>>,- code_unit_offset: usize,+ source_string: NullableMember<PrimitiveString>,+ /// Strings are shorter than 2^32 code units, like their lengths.+ code_unit_offset: u32,}+const _: () = assert!(size_of::<Substring>() == 32);+// SAFETY: The characters hold no cells.unsafe impl Trace for InlineString {fn trace(&self, _: &mut Visitor) {}@@ -399,7 +405,7 @@ impl PrimitiveString {return Self::create_from_substring(vm,substring.source_string(),- substring.code_unit_offset + code_unit_offset,+ substring.code_unit_offset() + code_unit_offset,code_unit_length,);}@@ -532,7 +538,7 @@ impl PrimitiveString {let source_string = unsafe { source_string.as_non_null().as_ref() };return source_string.utf16_string_view()- .substring_view(substring.code_unit_offset, self.length_in_utf16_code_units());+ .substring_view(substring.code_unit_offset(), self.length_in_utf16_code_units());}self.resolve_if_needed();}@@ -704,8 +710,8 @@ impl RopeString {DeferredKind::Rope,lhs.length_in_utf16_code_units() + rhs.length_in_utf16_code_units(),),- lhs: Cell::new(Some(lhs)),- rhs: Cell::new(Some(rhs)),+ lhs: NullableMember::new(Some(lhs)),+ rhs: NullableMember::new(Some(rhs)),}}@@ -811,11 +817,15 @@ impl Substring {fn new(source_string: Gc<PrimitiveString>, code_unit_offset: usize, code_unit_length: usize) -> Self {Self {base: PrimitiveString::new_deferred(Self::CLASS, DeferredKind::Substring, code_unit_length),- source_string: Cell::new(Some(source_string)),- code_unit_offset,+ source_string: NullableMember::new(Some(source_string)),+ code_unit_offset: u32::try_from(code_unit_offset).expect("strings are shorter than 2^32 code units"),}}+ fn code_unit_offset(&self) -> usize {+ self.code_unit_offset as usize+ }+fn source_string(&self) -> Gc<PrimitiveString> {self.source_string.get()@@ -826,7 +836,7 @@ impl Substring {let source_string = self.source_string();let source_view = source_string.utf16_string_view()- .substring_view(self.code_unit_offset, self.base.length_in_utf16_code_units());+ .substring_view(self.code_unit_offset(), self.base.length_in_utf16_code_units());self.set_resolved_utf16_string(source_view.to_utf16_string());}