Uh oh!
There was an error while loading. Please reload this page.
libcollections: move Vec::push slow path out - #23670
Conversation
rust-highfive
commented
Mar 24, 2015
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
emberian
commented
Mar 24, 2015
r? @pcwalton |
Gankra
commented
Mar 24, 2015
Vec already has a resize fn |
Gankra
commented
Mar 24, 2015
See also the private grow_capacity method for some pre-existing refactoring of this functionality. |
emberian
commented
Mar 24, 2015
@gankro using |
Makes Vec::push considerably smaller: 25 instructions, rather than 42, on x86_64.
emberian
commented
Mar 24, 2015
Yeah, new version works the same. |
huonw
commented
Mar 24, 2015
Do you happen to have some before/after benchmarks? |
Gankra
commented
Mar 24, 2015
@cmr I was moreso suggesting there might be a better refactoring to reduce duplication, but this certainly doesn't make it worse. |
emberian
commented
Mar 24, 2015
@huonw No, but I could fabricate one that is predicated on icache residency. |
emberian
commented
Mar 24, 2015
(@erickt also mentioned wanting this in serde) |
alexcrichton
commented
Mar 24, 2015
I would personally also prefer to see at least one benchmark showing one way or another here. This is unfortunately a loss in readability in the code and is an idiom we only rarely apply today, so I would prefer to have some numbers backing it up showing why the loss in readability is worth it. |
emberian
commented
Mar 24, 2015
Smaller function size is the main goal (this makes push 40% smaller), which can help inlining in complex functions and reduce icache pressure. Here's one silly benchmark: externcrate test;#[bench]fnx(b:&mut test::Bencher){letmut v = Vec::with_capacity(100);
b.iter(|| { v.extend(0..100); v.truncate(0);});} |
emberian
commented
Mar 24, 2015
For reference, against the C++ equivalent: #include<vector>
#include<cstdint>extern"C" {
voidcpp_version() {
static std::vector<int32_t> v(100);
for (int i = 0; i < 100; i++) {
v.push_back(i);
}
v.resize(0);
}
}externcrate test;#[bench]fnx(b:&mut test::Bencher){letmut v = Vec::with_capacity(100);
b.iter(|| { v.extend(0..100); v.truncate(0);});}#[link(name = "foo")]#[link(name = "stdc++")]extern"C"{fncpp_version();}#[bench]fncpp(b:&mut test::Bencher){
b.iter(|| unsafe{cpp_version()});}Maybe framing it as "Make Vec::push faster than C++ vector::push_back" makes it go down better ;) |
emberian
commented
Mar 25, 2015
@bors: r=pcwalton rollup |
bors
commented
Mar 25, 2015
📌 Commit 0e838f7 has been approved by |
bors
commented
Mar 25, 2015
⌛ Testing commit 0e838f7 with merge 928e2e2... |
Makes Vec::push considerably smaller: 25 instructions, rather than 42, on x86_64.
Makes Vec::push considerably smaller: 25 instructions, rather than 42, on
x86_64.