Skip to content
TopicTracker
From HackerNewsView original
TranslationTranslation

Rust sort_unstable_by with more complex closure unexpectedly shrunk binary

A Rust developer discovered that adding a boolean argument and an if-statement inside a sort_unstable_by closure unexpectedly reduced the compiled binary size by about 1KB, with the quicksort function alone shrinking by 980 bytes. The cause remains unclear despite similar inlining, and the effect was observed on the arm-unknown-linux-gnueabihf target.

Background

- Rust's `sort_unstable_by` uses an internal **quicksort** implementation that gets compiled (monomorphized) for the specific comparison closure you provide — the compiler can see through simple closures and heavily optimize the sort routine. - The author expected that adding an `if` branch inside the closure would make the binary larger; instead, the quicksort function got *smaller* (by ~1 KB). This is a surprising result that illustrates how compiler optimizations (inlining, constant propagation, branch prediction hints) are often non-obvious: more complex source code can sometimes produce *smaller* machine code by changing what gets inlined, how registers are used, or by enabling the compiler to merge code paths. - The target is ARM 32-bit (arm-unknown-linux-gnueabihf), a common embedded/SoC platform where binary size matters. - The post is on Hacker News, where such optimization curiosities are routinely discussed by systems programmers.

Related stories