-
Notifications
You must be signed in to change notification settings - Fork 176
[perf] use StackVector to save memory & CPU #553
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
6d24c8b
a48509d
5c1e3bf
2db0ac2
906e550
cba077f
c36bbe8
d6a346b
d3df508
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 |
|---|---|---|
|
|
@@ -97,3 +97,57 @@ impl VerifiedFlatbufferMemory { | |
| &self.raw_data[self.start..] | ||
| } | ||
| } | ||
|
|
||
| /// A stack-allocated vector that uses [T; MAX_SIZE] with Default initialization. | ||
| /// All elements are initialized to T::default(), and we track the logical size separately. | ||
| /// Note: a future impl can switch to using MaybeUninit with unsafe code for better efficiency. | ||
| pub struct StackVector<T, const MAX_SIZE: usize> { | ||
| data: [T; MAX_SIZE], | ||
| size: usize, | ||
| } | ||
|
|
||
| impl<T: Default + Copy, const MAX_SIZE: usize> Default for StackVector<T, MAX_SIZE> { | ||
| fn default() -> Self { | ||
| Self { | ||
| data: [T::default(); MAX_SIZE], | ||
| size: 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Default, const MAX_SIZE: usize> StackVector<T, MAX_SIZE> { | ||
| pub fn push(&mut self, value: T) -> bool { | ||
|
||
| if self.size < MAX_SIZE { | ||
| self.data[self.size] = value; | ||
| self.size += 1; | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| pub fn is_empty(&self) -> bool { | ||
| self.size == 0 | ||
| } | ||
|
|
||
| pub fn get_free_capacity(&self) -> usize { | ||
| MAX_SIZE - self.size | ||
| } | ||
|
|
||
| pub fn clear(&mut self) { | ||
| self.size = 0; | ||
| } | ||
|
|
||
| pub fn as_slice(&self) -> &[T] { | ||
| &self.data[..self.size] | ||
| } | ||
|
|
||
| pub fn into_vec(mut self) -> Vec<T> { | ||
| let mut v = Vec::with_capacity(self.size); | ||
| for i in 0..self.size { | ||
| v.push(std::mem::take(&mut self.data[i])); | ||
| } | ||
| self.size = 0; | ||
| v | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,5 +199,5 @@ fn check_rule_matching_browserlike() { | |
| let (blocked, passes) = bench_rule_matching_browserlike(&engine, &requests); | ||
| let msg = "The number of blocked/passed requests has changed. ".to_string() | ||
| + "If this is expected, update the expected values in the test."; | ||
| assert_eq!((blocked, passes), (106860, 136085), "{msg}"); | ||
| assert_eq!((blocked, passes), (106861, 136084), "{msg}"); | ||
|
Collaborator
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. Why is this expected ?
Collaborator
Author
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 mentioned in the PR description: we hits the token limits for one rule.
Collaborator
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. nvm, the reason is in the descr |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.