Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upConsider making operators transparently work on borrows wherever possible #1936
Comments
This comment has been minimized.
This comment has been minimized.
burdges
commented
Feb 28, 2017
•
|
I find it helps readability if I borrow everything as early as possible. I write roughly this sometimes :
Your constants could be borrows :
You can move some
All that costs you some consistency though. In general, we should expect APIs to give users the choice locally with the
Yet, this only hides performance problems by permitting pass by value. I could imagine deactivating In fact, I wonder if |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Jun 13, 2017
|
It appears one can mostly address this with Aside from numerical operators though, the builder pattern needs fast by value operation too. It's possible |
isislovecruft commentedFeb 28, 2017
Hello!
I've been working on a pure-Rust elliptic curve cryptography library with @hdevalence, called curve25519-dalek. We've implemented operators
Add,Sub,Mul,Neg, for the field and, similarly,AddandSubfor elliptic curve points. At first, we implemented the operators on the typesT. (Tin this case is some representation of a point, comprised internally fourFieldElements). But that was obviously sloooooow, because now everytime you want to add two points together, you're copying all those internalFieldElements around (and eachFieldElementis itself, internally, basically an[i32; 10]). With that implementation, we were copying 160 bytes around everytime we did any point operation. We then switched to implementing them for&Tto avoid copies, e.g.:Voilá, fast as hell! No more copies. And it's still quite readable. However, now, as I'm implementing the Elligator2 mapping (a way encode a point into a uniformly random string), I'm started to see the ampersands pile up pretty quickly:
let mut u: FieldElement = &(-(&A)) * &(&one + &nrr).invert();Wat. I just wanted
u = -A/(1 + nr²)!let w: FieldElement = &u * &(&(&u.square() + &(&A * &u)) + &one);Wat the wat. I wanted
w = u(u² + Au + 1).It seems @aturon and others have discussed potential solutions to this issue on Reddit, namely providing some "auto-ref" functionality, similar to
&self.Is this a change Rust would like to see? Are there more concerns which might arise if this were implemented? I really want to have the "my code is pretty" cake and eat the "my code is fast" cake too. :)