Optimize bounds checking #5345
Original bug ID: 5345
Here is a sequence of patches to improve the code selection for Ccheckbound. The first patch avoids the separate right shifting for block headers in most cases. The second and third patches further improve the code selection for amd64 and i386. Ccheckbound is now a sequence of cmp,jbe instead of mov,shr,cmp,jbe for the common memory/immediate case.
Comment author: @protz
Can you provide benchmarks for real-world situations where this is useful? Also, there's a compile flag that allows one to disable array bounds checking (-unsafe). Can you provide a rationale for optimizing this? I would tend to think that people who want performance disable array bounds checking.
Comment author: @hcarty
A speed increase with bounds checking enabled would certainly be welcome. I would rather not give up the safety of bounds checking to gain speed when possible.
This assumes that the complexity of the patch is acceptable to the OCaml maintainers and that benchmarks show this patch provides a speed increase.
Comment author: @xavierleroy
Applied patch 0001 (the processor-independent part) in SVN trunk, commit 11934.
Even though array indexing with constant indices isn't that common, the patch is short, sweet, clever, and applies to all targets, so, let's go!
I prefer not to add x86-specific operations for this purpose, though. The "op mem, cst" form used to be a win over "mov reg, mem; op reg, cst" for early processors like the Pentium, but I doubt it makes much of a difference with contemporary x86 processors.