Rust 的 `sort_unstable_by` 使用更复杂的闭包意外地导致二进制文件缩小
3 分•作者: tracyspacy•3 个月前
我偶然发现的事情:
我在 `sort_unstable_by` 闭包中添加了一个布尔参数和一个 `if` 语句:
```
// 从
|&a, &b| compare(a, b)
// 到
|&a, &b| if is_reverse { compare(b, a) } else { compare(a, b) }
```
我原本以为二进制文件会稍微增大,但实际上它变小了大约 1KB。
我使用 bloaty 进行了深入分析,发现原因是:`quicksort` 函数变小了 980 字节。
仍然不确定原因。内联看起来一样,`quicksort` 的指令数反而减少了。
目标:`arm-unknown-linux-gnueabihf`
详情:https://pastebin.com/qJdTw49n
提交:https://github.com/tracyspacy/fli/commit/a87a98be77cc19
查看原文
Thing I stumbled upon:<p>I added boolean argument and if statement inside sort_unstable_by closure:<p>```
//from
|&a, &b| compare(a, b)
//to
|&a, &b| if is_reverse { compare(b, a) } else { compare(a, b) }
```<p>I expected binary to grow slightly, but actually it became ca 1KB smaller.<p>I dug into with bloaty and found that the reason is: quicksort fn became 980 bytes smaller.<p>Still not sure why. Inlining looks same, quicksort just has fewer instructions.<p>target: arm-unknown-linux-gnueabihf<p>details: https://
pastebin.com/qJdTw49n<p>commit: https://github.com/tracyspacy/fli/commit/a87a98be77cc1972a53e6399716a227d1135e76b