News for hackers. Stuff that matters.
Re:Why Hashbrown Does a Double Lookup by hyperman1 on Wednesday March 20, 2019 @03:19PM (#19445792)
x86 seems reasonable to me:

Fast array bounds checking is, handling the lower bound by unsigned integer math :

  cmp index,bound
  ja crash_handler

Overflow handling is mostly

   jo crash_handler
That's 1 or 2 instructions and the jump has perfect prediction. I'd assume the cost is negligible compared with jump mispredicts and cache misses.

I read somewhere the main problem is LLVM not being quite capable of optimizing these decently: It has to handle all these extra jumps which causes lots of extra basic blocks.

Re:Why Hashbrown Does a Double Lookup by est31 on Wednesday March 20, 2019 @08:01PM (#19448215)
Yeah, part of the unexplored optimization potential that Rust has is that LLVM was originally written with C in mind, and Rust achieves great results with it, but sometimes it can't use the additional info that the better typesystem provides because nobody wrote the code in LLVM, or because LLVM has bugs so the Rust devs have to turn off that additional info.
Re:Why Hashbrown Does a Double Lookup by monocasa on Wednesday March 20, 2019 @05:17PM (#19447034)
A jump, but assume not taken and don't populate the branch predictor might be useful, otherwise you can end up with a death by a thousand cuts thing. Superfluous branches thrash your branch predictor tables similar to how cache pressure thrashs.
Re:Why Hashbrown Does a Double Lookup by simcop2387 on Wednesday March 20, 2019 @03:30PM (#19445927)
I seem to recall, that at least jo has terrible pipelining implications because of the dependency on the flag register that way, no idea about the bounds check (might be a better way)
Re:Why Hashbrown Does a Double Lookup by temac on Wednesday March 20, 2019 @04:24PM (#19446455)
I don't see why JO would be any different from other Jcc, especially since JL is SF ≠ OF. Maybe you were thinking of the problem with INC/DEC and CF, but even then that's a problem of INC/DEC, not of Jcc.
Re:Why Hashbrown Does a Double Lookup by nkurz on Wednesday March 20, 2019 @05:19PM (#19447049)
It's not usually a big difference, but on modern Intel there is a some difference in performance between the different CMP/JCC options. The more common ones will "fuse" with the CMP and be executed as a single µop, but the rarer ones (like JO and JS) do not fuse with CMP, and thus can add a cycle of delay (and have the overhead associated with executing another µop). The optimization is called "macro op fusion". Details here https://en.wikichip.org/wiki/macro-operation_fusion and here https://www.agner.org/optimize/microarchitecture.pdf (pages 108 and 125).
Re:Why Hashbrown Does a Double Lookup by khuey on Wednesday March 20, 2019 @05:47PM (#19447309)
Fixing that wouldn't seem to require any architectural changes in x86 though, it's just that Intel hasn't cared enough about JO and friends to optimize them this way.