rustdoc: Separate filter-empty-string out into its own function#83717
rustdoc: Separate filter-empty-string out into its own function#83717bors merged 2 commits intorust-lang:masterfrom
Conversation
|
Some changes occurred in HTML/CSS/JS. |
|
(rust-highfive has picked a reviewer for you, use r? to override) |
|
When comparing performance between the two, the "manual" filter is much faster. Results say that your proposition is 17.61% slower. I tested it on jsbench.me with the following array: var x = ["a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","","a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","","a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","","a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","","a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","","a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","","a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","","a", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq", "hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq","hbqsdq",""];Then the two following codes: x = x.filter(function(f) { return f !== ""; });and: for (var i = 0, len = x.length; i < len; ++i) {
if (x[i] === "") {
x.splice(i, 1);
i -= 1;
}
}If you confirm the result, I think it can be closed. However, could you add a comment saying that the current code is faster please? |
|
Immediately below the code that I changed, it loops through every item in the search index, and then runs Honestly, I wasn't really trying to make this code faster, because I assume that |
|
Performance in the search is important. But you can move the code into a filter function instead if you prefer (but still not use the official filter function, I think it's the callback which slows things down...). A bit of readability there would definitely be appreciated. |
0cdf5d8 to
b716fad
Compare
|
Okay, I've separated the empty string filter out into its own function, and also used a version that's about 10% faster according to jsbench.me (I designed it thinking that Array.prototype.splice had to renumber everything, so it would be better to only call it once). |
This comment has been minimized.
This comment has been minimized.
|
About the CI failure, it's because you need to update the rustdoc-js script by adding the name of your function here. |
b716fad to
ce1b746
Compare
|
Looks all good to me now. Thanks a lot for both performance and code readability improvements! |
|
Okay, I fixed the test case, and also did further tweaks to the function... there's an annoying amount of engine-specific behaviour here. Given these two functions: function removeEmptyStringsFromArray_A(x) {
for (var i = 0, len = x.length; i < len; ++i) {
if (x[i] === "") {
x.splice(i, 1);
i -= 1;
}
}
}
function removeEmptyStringsFromArray_B(x) {
for (var i = x.length - 1, j = x.length - 1; j >= 0; j -= 1) {
if (x[j] !== "") {
x[i] = x[j];
i -= 1;
}
}
x.splice(0, i + 1);
}In Firefox, version A is reported to be 43% slower than version B, but in Chromium, version B is 12.5% slower than version A, using your original test data. What's really annoying is that there's never a point where B becomes faster. I would've expected a splice nested inside of a loop to have O(n2) runtime, since it ought to renumber the array, but given the apparent worst-case of an array containing nothing but empty strings, both engines perform better with version A. Does anyone have any idea what's going on? |
ce1b746 to
227f5ed
Compare
|
Might need to ask to the people who actually write the JS engine at this point... If you know any? |
|
For now, let's go forward with this already. @bors: r+ rollup |
|
📌 Commit 227f5ed has been approved by |
…llaumeGomez rustdoc: Separate filter-empty-string out into its own function
Rollup of 7 pull requests Successful merges: - rust-lang#80525 (wasm64 support) - rust-lang#83019 (core: disable `ptr::swap_nonoverlapping_one`'s block optimization on SPIR-V.) - rust-lang#83717 (rustdoc: Separate filter-empty-string out into its own function) - rust-lang#83807 (Tests: Remove redundant `ignore-tidy-linelength` annotations) - rust-lang#83815 (ptr::addr_of documentation improvements) - rust-lang#83820 (Remove attribute `#[link_args]`) - rust-lang#83841 (Allow clobbering unsupported registers in asm!) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
No description provided.