ethnum-rs icon indicating copy to clipboard operation
ethnum-rs copied to clipboard

Made Equality Tests Smarter

Open NCGThompson opened this issue 2 years ago • 0 comments

Currently, == compares the underlying arrays. This compiles to memcmp in llvm. memcmp is then optimized to bcmp. The problem with this is that llvm-opt doesn't do much with bcmp during the instcombine pass. It will also sometimes write to memory just to read it back again.

What I did to work around this in div-rem was use unreachable_unchecked() calls to tell the compiler that the bcmp is equivalent to two icmps. I have considered overloading the == operator with a bcmp plus hints, but now I believe it is best to not use bcmp at all. Ethnum's integers have public fields, so the current equality operation can always be called with a.0 == b.0.

I compared and annotated a few alternatives on Compiler Explorer. Note that I have not considered adding an llvm intrinsic.

NCGThompson avatar Nov 21 '23 04:11 NCGThompson