Skip to content

Truly independent web browser

LibJS: Count the holes of holey elements whenever their storage grows

Andreas Klingd217037f8ad3
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index a69c021fe15..44cecdec982 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -519,6 +519,10 @@ pub fn plain_object_size_classes(heap: &Heap) -> &[SizeClassAllocator] {
const SPARSE_ARRAY_HOLE_THRESHOLD: u32 = 200;
+/// Holey elements whose storage grows stay holey while at least one in this many of them is filled. At that density,
+/// their storage takes about twice as much memory as a dictionary of the filled elements would.
+const SPARSE_ARRAY_DENSITY_RATIO: usize = 8;
+
/// Whether elements with `filled` of their `length` indices filled are dense enough for holey storage: three quarters.
fn are_dense_enough(filled: usize, length: usize) -> bool {
filled >= length - length / 4
@@ -4224,14 +4228,34 @@ impl Object {
if index < self.indexed_elements_capacity() {
return true;
}
- let filled = match kind {
+ are_dense_enough(
+ self.filled_dense_elements(materialized_elements) + 1,
+ index as usize + 1,
+ )
+ }
+
+ /// Whether holey elements whose storage grows for a store at `index`, past their first `materialized_elements`,
+ /// stay holey instead of becoming a dictionary: where they have no more than SPARSE_ARRAY_HOLE_THRESHOLD holes, or
+ /// where at least one in SPARSE_ARRAY_DENSITY_RATIO of them is filled.
+ ///
+ /// NB: Stores that each leave fewer holes than SPARSE_ARRAY_HOLE_THRESHOLD would otherwise make the elements
+ /// arbitrarily sparse, like {50, 100, 200, ..., 900} does. The storage grows by half at least, and copies the
+ /// elements anyway, so counting their holes takes linear time overall.
+ fn keeps_holey_storage_growing_for(&self, index: u32, materialized_elements: u32) -> bool {
+ let filled = self.filled_dense_elements(materialized_elements) + 1;
+ let length = index as usize + 1;
+ length - filled <= SPARSE_ARRAY_HOLE_THRESHOLD as usize || filled >= length / SPARSE_ARRAY_DENSITY_RATIO
+ }
+
+ /// The number of the first `materialized_elements` of packed or holey elements that are not holes.
+ fn filled_dense_elements(&self, materialized_elements: u32) -> usize {
+ match self.indexed_storage_kind() {
IndexedStorageKind::Packed => materialized_elements as usize,
IndexedStorageKind::Holey => (0..materialized_elements)
.filter(|index| !self.indexed_element(*index).is_empty())
.count(),
IndexedStorageKind::None | IndexedStorageKind::Dictionary => unreachable!(),
- };
- are_dense_enough(filled + 1, index as usize + 1)
+ }
}
fn indexed_put_dictionary_and_update_size(&self, index: u32, value: Value, attributes: PropertyAttributes) {
@@ -4261,9 +4285,15 @@ impl Object {
}
// Check for sparse threshold
- if u64::from(index) > u64::from(materialized_elements) + u64::from(SPARSE_ARRAY_HOLE_THRESHOLD)
- && !self.keeps_dense_storage_for(index, materialized_elements)
- {
+ let makes_dictionary =
+ if u64::from(index) > u64::from(materialized_elements) + u64::from(SPARSE_ARRAY_HOLE_THRESHOLD) {
+ !self.keeps_dense_storage_for(index, materialized_elements)
+ } else {
+ kind == IndexedStorageKind::Holey
+ && index >= self.indexed_elements_capacity()
+ && !self.keeps_holey_storage_growing_for(index, materialized_elements)
+ };
+ if makes_dictionary {
self.transition_to_dictionary();
self.indexed_put_dictionary_and_update_size(index, value, attributes);
return;
diff --git a/Tests/LibJS/Runtime/jit/dense-dictionary-arrays.js b/Tests/LibJS/Runtime/jit/dense-dictionary-arrays.js
index ef6e8086d0b..5a632eab1b9 100644
--- a/Tests/LibJS/Runtime/jit/dense-dictionary-arrays.js
+++ b/Tests/LibJS/Runtime/jit/dense-dictionary-arrays.js
@@ -162,3 +162,35 @@ test("repeated sparse stores past holey elements that stay dense keep them dense
expect(values[values.length - 1]).toBe(values.length - 1);
if (jit.enabled) expect(elementsKindSeen(values, 1025).includes("elements=Holey")).toBeTrue();
});
+
+test("stores that each leave few holes but make holey elements sparse make them a dictionary", () => {
+ const keys = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
+ const palette = {};
+ for (const key of keys) palette[key] = `#${key}`;
+ expect(Object.keys(palette)).toEqual(keys.map(String));
+ for (const key of keys) expect(palette[key]).toBe(`#${key}`);
+ expect(0 in palette).toBeFalse();
+ expect(150 in palette).toBeFalse();
+ if (jit.enabled) expect(elementsKindSeen(palette, 950).includes("elements=Other")).toBeTrue();
+
+ // Holey elements with few holes, or with many holes but at least an eighth filled, stay dense as they grow.
+ const values = [];
+ for (let i = 1; i < 1000; ++i) values[i] = i;
+ const strided = [];
+ for (let i = 0; i < 2000; i += 2) strided[i] = i;
+ const queue = [];
+ let head = 0;
+ for (let i = 0; i < 2000; ++i) {
+ queue.push(i);
+ if (i % 2) delete queue[head++];
+ }
+ for (const [elements, hole, last] of [
+ [values, 0, 999],
+ [strided, 1, 1998],
+ [queue, 0, 1999],
+ ]) {
+ expect(hole in elements).toBeFalse();
+ expect(elements[last]).toBe(last);
+ if (jit.enabled) expect(elementsKindSeen(elements, last).includes("elements=Holey")).toBeTrue();
+ }
+});