-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Forwarder: clean up packet_vec filter #30921
Conversation
None | ||
} | ||
}) | ||
.filter(|p| !p.meta().forwarded()) |
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 new code is cleaner definitely. Isn't the left side more efficient though without multiple loops?
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.
It won't do multiple loops, iterators are a zero-cost abstraction: https://doc.rust-lang.org/book/ch13-04-performance.html
Closures and iterators are Rust features inspired by functional programming language ideas. They contribute to Rust’s capability to clearly express high-level ideas at low-level performance. The implementations of closures and iterators are such that runtime performance is not affected. This is part of Rust’s goal to strive to provide zero-cost abstractions.
They get compiled directly, so expansion isn't really a thing, but we kind of think how expanding these two versions might work...
Roughly the code before was:
let mut packet_vec = vec![];
for p in forwardable_packets {
if !p.meta().forwarded() && self.data_budget.take(p.meta().size) {
if let Some(data) = p.data(..) {
packet_vec.push(data.to_vec());
}
}
}
And after the change, (again roughly):
let mut packet_vec = vec![];
for p in forwardable_packets {
if p.meta().forwarded() { continue; }
if !p.data_budget.take(p.meta().size) { continue; }
if let Some(data) = p.data(..) {
packet_vec.push(data.to_vec());
}
}
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.
Good to know! Thanks
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.
LGTM
Codecov Report
@@ Coverage Diff @@
## master #30921 +/- ##
=========================================
- Coverage 81.5% 81.5% -0.1%
=========================================
Files 727 727
Lines 205118 205116 -2
=========================================
- Hits 167256 167246 -10
- Misses 37862 37870 +8 |
Problem
Saw an odd use of filter_map
Summary of Changes
Use chained methods to do equivalent operations in less code
Fixes #