Faster insert for the index == len
case by nnethercote · Pull Request #282 · servo/rust-smallvec (original) (raw)
This case popped up within rustc, where insert
was repeatedly used for a push
-like operation. (But in a context where push
wasn't always appropriate.) It speeds up rustc on one benchmark by 2% -- not huge, but also not bad for such a small change.
Here is some before and after results for the new benchmark along with the existing push
benchmarks.
Old:
test bench_push ... bench: 317 ns/iter (+/- 25)
test bench_push_small ... bench: 34 ns/iter (+/- 1)
test bench_insert_push ... bench: 627 ns/iter (+/- 32)
test bench_insert_push_small ... bench: 99 ns/iter (+/- 6)
New
test bench_push ... bench: 295 ns/iter (+/- 21)
test bench_push_small ... bench: 36 ns/iter (+/- 3)
test bench_insert_push ... bench: 504 ns/iter (+/- 17)
test bench_insert_push_small ... bench: 70 ns/iter (+/- 4)
The first two are testing identical code, and show that the time variation is non-trivial. The latter two show the effect of this PR's code changes, which are well beyond the timing variation.