perf(transformer): optimize inserting var/let statements#10654
Conversation
CodSpeed Instrumentation Performance ReportMerging #10654 will not alter performanceComparing Summary
|
0479363 to
68620ef
Compare
3153921 to
b44d2a9
Compare
overlookmotel
left a comment
There was a problem hiding this comment.
It's definitely an improvement not to allocate a temp Vec.
What is less good is that we always copy the whole of stmts into a new ArenaVec. If stmts has spare capacity, it'd be preferable to shift up the existing Statements to make room for the new ones at the start, and then move the new statements into the newly-created space. That doesn't reduce the amount of memory copying that needs to happen, but at least it does it without consuming more memory in arena for a brand new Vec.
However, I was surprised to see that Vec::splice was doing the same unoptimal thing - allocate a new Vec and copy everything over. Vec::insert is also surprisingly bad! If the Vec is already full to capacity, it copies all the existing elements of the Vec twice, which is completely unnecessary.
To get this (and the many similar operations in our code) optimal, we'd need new APIs like:
impl<'a, T> Vec<'a, T> {
/// Same as `vec.insert(0, value)`
fn push_start(&mut self, value: T);
/// Same as `vec.splice(0..0, iter)`
fn extend_start<I: IntoIterator<Item = T>>(&mut self, iter: I);
}We could optimize these to use the "shift up" method.
Merge activity
|
Found while working on #9881, this PR aims to optimize inserting statements by reducing an unnecessary `Vec` allocation. Previously, `get_var_statement` would collect `var` statement and `let` statement into a `std::vec::Vec` and return it, then immediately `extend`/`insert` to another `ArenaVec`. Considering here only have two elements, we don't need to store them in a `Vec`, just return them, and use them depending on how they are used. Seems to have very minor improvement, and I guess `transformer/checker.ts` has 0.1 regression is noisy. <img width="767" alt="image" src="https://github.com/user-attachments/assets/c0aeeb5a-e2da-4e2a-9990-56b239f6ab65" />
b44d2a9 to
91df9d4
Compare

Found while working on #9881, this PR aims to optimize inserting statements by reducing an unnecessary
Vecallocation.Previously,
get_var_statementwould collectvarstatement andletstatement into astd::vec::Vecand return it, then immediatelyextend/insertto anotherArenaVec. Considering here only have two elements, we don't need to store them in aVec, just return them, and use them depending on how they are used.Seems to have very minor improvement, and I guess
transformer/checker.tshas 0.1 regression is noisy.