alt.hn

7/2/2026 at 1:30:08 PM

Rust sort_unstable_by with more complex closure unexpectedly shrunk binary

by tracyspacy

7/2/2026 at 5:41:23 PM

I didn't look at the details you posted, but as a compiler engineer, the general statement doesn't surprise me. More complex control flow can block optimizations. Many compiler optimizations involve duplicating code in some way (loop unrolling, for example). Hence, more complex control flow can block optimizations that would increase code size.

by derdi

7/2/2026 at 7:30:00 PM

I don't think the loop unrolling plays a big role here, I guess it was more an example. While post body contains control flow, it can be simple arithmetic operation instead and it will lead to the similar result, so not the control flow itself causing this. But yeah, I also feel, that more complex closure blocks some optimizations here, thus quicksort having less instructions. I suspect that complex closure was outlined to a separate fn, and not copied to quicksort anymore.

by tracyspacy