num-bigint
num-bigint copied to clipboard
BinopAssign<&uX> is not implemented for BigUint
BigUint(and probably also BigInt) implements Add<u8> and Add<&u8>, but only AddAssign<u8>. We should probably either implement AddAssign<&u8> as well or remove Add<&u8> as the current state is somewhat surprising/unintuitive.
The same is true for all BinopAssign traits: AddAssign, MulAssign, DivAssign etc.
We shouldn't remove anything -- references can be a big saver when clones can be avoided. The assign ops should be implemented for references too.
I ran the test at my criterion.rs benchmark suite to check the influence of passing integers by reference instead of by value.
While I am currently running the tests with all integers instead of only u128, I still believe that passing by reference is actually not recommendable. There seems to be a non negligible performance hit when passing &u128 instead of u128.
first bench this, by value then this, by ref
don't have a lot of time right now, but by ref seems to mostly hurt the performance.
Oh, yeah, I was only thinking of & among BigUint parameters. For primitive integers, it is a bit wasteful, but consistent with the std implementions. I think it's a matter of convenience for when you already have a reference. That case ought to be roughly equal for performance whether it's dereferenced before the operator or within, so it might as well be made easy for ergonomics.
I didn't know Add<&Self> is implemented for number types. :open_mouth:
So we should probably implemented the following binops for all integers to be similar to std:
Binop<BigInt> for intBinop<&BigInt> for intBinop<int> for BigIntBinop<&int> for BigIntBinop<int> for &BigIntBinop<&int> for &BigIntBinopAssign<int> for BigIntBinopAssign<&int> for BigIntBinopAssign<Bigint> for intBinopAssign<&Bigint> for int
I might implement this if it is desired.
* BinopAssign<&int> for BigInt
I think this is the only one missing -- the rest should be covered by macro forwarding.