Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize shift for dense arrays #3405

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,22 @@ impl Array {
// b. Return undefined.
return Ok(JsValue::undefined());
}

// Small optimization for arrays using dense properties
// TODO: this optimization could be generalized to many other objects with
// slot-based dense property maps.
if o.is_array() {
let mut o_borrow = o.borrow_mut();
if let Some(dense) = o_borrow.properties_mut().dense_indexed_properties_mut() {
if len <= dense.len() as u64 {
let v = dense.remove(0);
drop(o_borrow);
Self::set_length(&o, len - 1, context)?;
return Ok(v);
}
}
}

// 4. Let first be ? Get(O, "0").
let first = o.get(0, context)?;
// 5. Let k be 1.
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/object/property_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ enum IndexedProperties {
/// are not data descriptors with with a value field, writable field set to `true`, configurable field set to `true`, enumerable field set to `true`.
///
/// This method uses more space, since we also have to store the property descriptors, not just the value.
/// It is also slower because we need to to a hash lookup.
/// It is also slower because we need to do a hash lookup.
Sparse(Box<FxHashMap<u32, PropertyDescriptor>>),
}

Expand Down Expand Up @@ -150,7 +150,7 @@ impl IndexedProperties {
replaced
}

/// Inserts a property descriptor with the specified key.
/// Removes a property descriptor with the specified key.
fn remove(&mut self, key: u32) -> bool {
let vec = match self {
Self::Sparse(map) => {
Expand Down
Loading