-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use BindingId
copies in lieu of &BindingId
in semantic model methods
#4633
Conversation
pub fn bindings(&self) -> std::collections::hash_map::Iter<&'a str, BindingId> { | ||
self.bindings.iter() | ||
pub fn bindings(&self) -> impl Iterator<Item = (&&str, BindingId)> + '_ { | ||
self.bindings.iter().map(|(name, id)| (name, *id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit tedious. (Should this just be self.bindings.iter().copied()
? That would change to &str
.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. &&str
gets automatically dereferenced by Rust
to &str
but using copied()
is just simpler :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way I think about it (and I might be wrong) is that passing a value by reference means we pass and copy a pointer. A pointer is 8 bytes. Copying any data that is less or equal to 8 bytes should, thus, be as much work.
pub fn bindings(&self) -> std::collections::hash_map::Iter<&'a str, BindingId> { | ||
self.bindings.iter() | ||
pub fn bindings(&self) -> impl Iterator<Item = (&&str, BindingId)> + '_ { | ||
self.bindings.iter().map(|(name, id)| (name, *id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. &&str
gets automatically dereferenced by Rust
to &str
but using copied()
is just simpler :)
fbbc716
to
524eda8
Compare
524eda8
to
fc9235f
Compare
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
Summary
Partly putting this up because I don't have good intuition around these tradeoffs. Given that
BindingId
is small and implementsCopy
, is it ever advantageous to pass around reference?