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-06-20 19:01:34 -04:00
2023-06-20 10:56:23 -07:00
2023-04-22 13:09:15 +03:00
2023-06-10 20:42:30 -07:00
2023-04-30 18:16:04 -07:00
2023-04-30 18:16:04 -07:00
2023-04-30 18:16:04 -07:00
2023-03-17 17:50:25 +01:00
2023-04-30 18:16:04 -07:00