Skip to content

Commit

Permalink
Optimize shift for dense arrays (#3405)
Browse files Browse the repository at this point in the history
* Optimize `Array.prototype.shift` for dense arrays

* Optimize shifts for dense arrays
  • Loading branch information
jedel1043 authored Oct 20, 2023
1 parent a2b5885 commit 26d14a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit 26d14a8

Please sign in to comment.