Skip to content

Truly independent web browser

LibJS/JIT: Find strings of larger integers in IntegerToString

Andreas Kling370279e61b1a
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rs b/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rs
index f70ae7458c3..30231d458f0 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/concatenation.rs
@@ -61,7 +61,12 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
let (integer, string) = (self.input(node, 0), self.output(node));
let scratch = self.pinned.scratch;
let layout = self.runtime.layout;
- let (missing, done) = (self.masm.new_label(), self.masm.new_label());
+ let (large, found, missing, done) = (
+ self.masm.new_label(),
+ self.masm.new_label(),
+ self.masm.new_label(),
+ self.masm.new_label(),
+ );
if layout.numeric_string_cache == 0 {
self.masm.jump(missing);
}
@@ -71,11 +76,40 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
Condition::AboveOrEqual,
scratch,
layout.numeric_string_cache_size as i32,
- missing,
+ large,
);
self.masm.move_imm64(string, layout.numeric_string_cache);
self.masm
.load64(string, &Address::indexed(string, scratch, Scale::Eight, 0));
+ self.masm.jump(found);
+
+ // Larger integers have an entry of the cache of their strings, which
+ // has the string of the integer it names.
+ self.masm.bind(large);
+ let entry_size = layout.large_numeric_string_cache_entry_size;
+ if layout.large_numeric_string_cache == 0 || !entry_size.is_power_of_two() {
+ self.masm.jump(missing);
+ }
+ // NB: The scratch register keeps the integer, zero-extended, for the
+ // comparison with the entry's.
+ self.masm.branch32_imm(Condition::LessThan, scratch, 0, missing);
+ self.masm
+ .and32_imm(string, scratch, layout.large_numeric_string_cache_mask);
+ self.masm.shl64_imm(string, string, entry_size.trailing_zeros() as u8);
+ self.masm
+ .add64_imm(string, string, layout.large_numeric_string_cache as i64);
+ self.masm.branch64_memory(
+ Condition::NotEqual,
+ &Address::new(string, layout.large_numeric_string_cache_number as i32),
+ scratch,
+ missing,
+ );
+ self.masm.load64(
+ string,
+ &Address::new(string, layout.large_numeric_string_cache_string as i32),
+ );
+
+ self.masm.bind(found);
self.masm.branch_test64(Condition::Zero, string, u64::MAX, missing);
self.box_cell_with_tag(string, string, value::STRING_TAG);
self.masm.jump(done);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index 93286daab81..1e56ff605f0 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -1235,6 +1235,11 @@ fn fake_object_layout() -> RuntimeLayout {
fly_string_cache_mask: 0,
numeric_string_cache: 0,
numeric_string_cache_size: 0,
+ large_numeric_string_cache: 0,
+ large_numeric_string_cache_mask: 0,
+ large_numeric_string_cache_entry_size: 0,
+ large_numeric_string_cache_number: 0,
+ large_numeric_string_cache_string: 0,
environment_outer: 16,
environment_declarative: 13,
module_environment_class: MODULE_ENVIRONMENT_CLASS,
@@ -3675,6 +3680,17 @@ fn short_string_concatenations_through_the_fly_string_cache() {
let numbers: &'static mut [u64; 10] = Box::leak(Box::new([0; 10]));
numbers[7] = fake_string_of_length(1, short_string_word(b"7")) & ((1 << 48) - 1);
let numbers_address = numbers.as_ptr() as u64;
+ // The VM's cache of the strings of larger integers, entries of the
+ // integer and its string: only 1000 has one, in the entry of 1004 too.
+ let a1000 = short_string_word(b"a1000");
+ let cached_a1000 = fake_string_of_length(5, a1000);
+ cache[(murmur3_finalizer(a1000) & 1023) as usize] = cached_a1000 & ((1 << 48) - 1);
+ let large_numbers: &'static mut [[u64; 2]; 4] = Box::leak(Box::new([[0; 2]; 4]));
+ large_numbers[0] = [
+ 1000,
+ fake_string_of_length(4, short_string_word(b"1000")) & ((1 << 48) - 1),
+ ];
+ let large_numbers_address = large_numbers.as_ptr() as u64;
let concatenate = |lhs: u64, rhs: u64| {
let program = assemble(|_| {
vec![
@@ -3706,6 +3722,11 @@ fn short_string_concatenations_through_the_fly_string_cache() {
snapshot.runtime.layout.fly_string_cache_mask = 1023;
snapshot.runtime.layout.numeric_string_cache = numbers_address;
snapshot.runtime.layout.numeric_string_cache_size = 10;
+ snapshot.runtime.layout.large_numeric_string_cache = large_numbers_address;
+ snapshot.runtime.layout.large_numeric_string_cache_mask = 3;
+ snapshot.runtime.layout.large_numeric_string_cache_entry_size = 16;
+ snapshot.runtime.layout.large_numeric_string_cache_number = 0;
+ snapshot.runtime.layout.large_numeric_string_cache_string = 8;
// ArithFeedback::String and ArithFeedback::Int32.
snapshot.executables[0].feedback.arith = vec![1 << 3 | 1];
},
@@ -3728,6 +3749,9 @@ fn short_string_concatenations_through_the_fly_string_cache() {
assert_eq!(concatenate(short(b"a"), value::int32(6)), MARKER);
assert_eq!(concatenate(short(b"a"), value::int32(-7)), MARKER);
assert_eq!(concatenate(short(b"a"), value::int32(10)), MARKER);
+ assert_eq!(concatenate(short(b"a"), value::int32(1000)), cached_a1000);
+ assert_eq!(concatenate(short(b"a"), value::int32(1004)), MARKER);
+ assert_eq!(concatenate(short(b"a"), value::int32(1001)), MARKER);
}
#[test]
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
index 51dfdf9a074..bd2ac7aba13 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
@@ -1039,9 +1039,9 @@ pub enum Op {
/// strings, which come from the VM's cache of fly strings (when both
/// strings are short).
ConcatenateStrings,
- /// The VM's string of the `Repr::Int32` input 0, from its cache of the
- /// strings of small non-negative integers, or the empty value if the
- /// cache has none.
+ /// The VM's string of the `Repr::Int32` input 0, from its caches of the
+ /// strings of non-negative integers, or the empty value if they have
+ /// none.
IntegerToString,
// Arrays.
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index 2c1f93e74da..dea27deb9ed 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -1679,6 +1679,18 @@ pub struct RuntimeLayout {
/// none yet), or 0 for code without them.
pub numeric_string_cache: u64,
pub numeric_string_cache_size: u32,
+ /// The address of the VM's cache of the strings of larger integers, or
+ /// 0 for code without one: the entry of the integer `n` is at index
+ /// `n & large_numeric_string_cache_mask`, entries are
+ /// `large_numeric_string_cache_entry_size` bytes, and an entry has the
+ /// string (cell pointer, or null) at
+ /// `large_numeric_string_cache_string` if the integer (u64) at
+ /// `large_numeric_string_cache_number` is `n`.
+ pub large_numeric_string_cache: u64,
+ pub large_numeric_string_cache_mask: u32,
+ pub large_numeric_string_cache_entry_size: u32,
+ pub large_numeric_string_cache_number: u32,
+ pub large_numeric_string_cache_string: u32,
/// `Environment::outer` (cell pointer).
pub environment_outer: u32,
/// `Environment::declarative` (bool): whether the environment is a
diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
index 08a7527cce6..c7db5a49a59 100644
--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs
+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
@@ -29,7 +29,9 @@ use crate::bytecode::executable::{
};
use crate::gc::class::{Class, GcCell};
use crate::interpreter::runtime_functions::runtime_function_addresses;
-use crate::interpreter::vm::{FLY_STRING_CACHE_SIZE, NUMERIC_STRING_CACHE_SIZE, Vm};
+use crate::interpreter::vm::{
+ FLY_STRING_CACHE_SIZE, LARGE_NUMERIC_STRING_CACHE_SIZE, NUMERIC_STRING_CACHE_SIZE, NumericStringCacheEntry, Vm,
+};
use crate::layout::accessor::Accessor;
use crate::layout::cell::Gc;
use crate::layout::environment::{
@@ -233,6 +235,11 @@ fn runtime_layout() -> RuntimeLayout {
fly_string_cache_mask: (FLY_STRING_CACHE_SIZE - 1) as u32,
numeric_string_cache: 0,
numeric_string_cache_size: NUMERIC_STRING_CACHE_SIZE as u32,
+ large_numeric_string_cache: 0,
+ large_numeric_string_cache_mask: (LARGE_NUMERIC_STRING_CACHE_SIZE - 1) as u32,
+ large_numeric_string_cache_entry_size: size_of::<NumericStringCacheEntry>() as u32,
+ large_numeric_string_cache_number: offset(offset_of!(NumericStringCacheEntry, number)),
+ large_numeric_string_cache_string: offset(offset_of!(NumericStringCacheEntry, string)),
environment_outer: offset(offset_of!(Environment, outer)),
environment_declarative: offset(offset_of!(Environment, declarative)),
module_environment_class: core::ptr::from_ref(ModuleEnvironment::CLASS) as u64,
@@ -442,6 +449,7 @@ pub fn runtime_info(vm: &Vm, realm: Gc<Realm>, allocation: AllocationInfos) -> R
layout.single_ascii_character_strings = vm.single_ascii_character_strings_address();
layout.fly_string_cache = vm.fly_string_cache().as_ptr().expose_provenance() as u64;
layout.numeric_string_cache = vm.numeric_string_cache().as_ptr().expose_provenance() as u64;
+ layout.large_numeric_string_cache = vm.large_numeric_string_cache().as_ptr().expose_provenance() as u64;
let strings = vm.cached_strings();
let string = |string| Value::from_string(string).0;
layout.typeof_strings = TypeofStrings {