-
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
perf: RuleTable::any_enabled
#10971
perf: RuleTable::any_enabled
#10971
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,6 +234,7 @@ impl RuleSet { | |
/// assert!(set.contains(Rule::AmbiguousFunctionName)); | ||
/// assert!(!set.contains(Rule::BreakOutsideLoop)); | ||
/// ``` | ||
#[inline] | ||
pub const fn contains(&self, rule: Rule) -> bool { | ||
let rule = rule as u16; | ||
let index = rule as usize / Self::SLICE_BITS as usize; | ||
|
@@ -243,6 +244,20 @@ impl RuleSet { | |
self.0[index] & mask != 0 | ||
} | ||
|
||
/// Returns `true` if any of the rules in `rules` are in this set. | ||
#[inline] | ||
pub const fn any(&self, rules: &[Rule]) -> bool { | ||
let mut any = false; | ||
let mut i = 0; | ||
|
||
while i < rules.len() { | ||
any |= self.contains(rules[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intentionally use an bitwise OR here to avoid any branching. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd bet it would compile down to the same code if you used an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acutally, it does not The iter version has a jump
The loop version does not (but it requires more instructions)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I just meant, It makes sense that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yeah, that probably compiles down to the same |
||
i += 1; | ||
} | ||
|
||
any | ||
} | ||
|
||
/// Returns an iterator over the rules in this set. | ||
/// | ||
/// ## Examples | ||
|
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 looks like this entire function could just be written as
rules.iter().any(|r| self.contains(r))
? Did you try that and it was slower? (I believe it also has the benefit of short circuiting, which may or may not help depending on the typical length ofrules
.)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 typical length is like 1-8 rules (where 8 is rare). The downside is that the function can't be const anymore ;).
But the performance is about the same. So lets use
any
as it is easier to understand.Edit: Codspeed disagrees. The shift version is slightly faster (23% speedup instead of 20%)
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.
Ah I missed the
const
requirement.