Skip to content

Commit 737a1fd

Browse files
committed
perf: add dense array fast-path for Array.prototype.unshift
1 parent 3016f49 commit 737a1fd

File tree

1 file changed

+49
-0
lines changed
  • core/engine/src/builtins/array

1 file changed

+49
-0
lines changed

core/engine/src/builtins/array/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,55 @@ impl Array {
12911291
.with_message("length + number of arguments exceeds the max safe integer limit")
12921292
.into());
12931293
}
1294+
1295+
// Small optimization for arrays using dense properties.
1296+
// Mirrors the fast-path in `shift`.
1297+
if o.is_array() {
1298+
let mut o_borrow = o.borrow_mut();
1299+
let props = &mut o_borrow.properties_mut().indexed_properties;
1300+
1301+
let fast_path_done = match props {
1302+
IndexedProperties::DenseI32(dense) if len <= dense.len() as u64 => {
1303+
// Only take the fast path if ALL new items are i32.
1304+
let all_i32: Option<Vec<i32>> = args.iter().map(|v| v.as_i32()).collect();
1305+
if let Some(items) = all_i32 {
1306+
for (i, item) in items.into_iter().enumerate() {
1307+
dense.insert(i, item);
1308+
}
1309+
true
1310+
} else {
1311+
false
1312+
}
1313+
}
1314+
IndexedProperties::DenseF64(dense) if len <= dense.len() as u64 => {
1315+
let all_f64: Option<Vec<f64>> =
1316+
args.iter().map(|v| v.as_number()).collect();
1317+
if let Some(items) = all_f64 {
1318+
for (i, item) in items.into_iter().enumerate() {
1319+
dense.insert(i, item);
1320+
}
1321+
true
1322+
} else {
1323+
false
1324+
}
1325+
}
1326+
IndexedProperties::DenseElement(dense) if len <= dense.len() as u64 => {
1327+
for (i, item) in args.iter().enumerate() {
1328+
dense.insert(i, item.clone());
1329+
}
1330+
true
1331+
}
1332+
_ => false,
1333+
};
1334+
1335+
drop(o_borrow);
1336+
1337+
if fast_path_done {
1338+
Self::set_length(&o, len + arg_count, context)?;
1339+
return Ok((len + arg_count).into());
1340+
}
1341+
}
1342+
12941343
// b. Let k be len.
12951344
let mut k = len;
12961345
// c. Repeat, while k > 0,

0 commit comments

Comments
 (0)