Niles Salter 7d511d6428
[heapsort] Protect against integer overflow
(Firstly, I changed `n` to `b`, as that is less confusing. It's not a length, it's a right boundary.)

The invariant maintained is `cur < b`. In the worst case `2*cur + 1` results in a maximum of `2b`. Since `2b` is not guaranteed to be lower than `maxInt`, we have to add one overflow check to `siftDown` to make sure we avoid undefined behavior.

LLVM also seems to have a nicer time compiling this version of the function. It is about 2x faster in my tests (I think LLVM was stumped by the `child += @intFromBool` line), and adding/removing the overflow check has a negligible performance difference on my machine. Of course, we could check `2b <= maxInt` in the parent function, and dispatch to a version of the function without the overflow check in the common case, but that probably is not worth the code size just to eliminate a single instruction.
2023-06-22 17:32:28 +00:00
..
2023-03-18 16:01:09 -07:00
2023-06-20 12:55:38 -04:00
2023-04-05 23:22:29 -07:00
2023-01-26 16:36:13 -07:00
2023-01-26 16:36:14 -07:00
2023-02-04 15:19:53 -05:00
2022-11-04 00:09:27 +03:30