-
Notifications
You must be signed in to change notification settings - Fork 177
[perf] use arrayvec 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
Open
atuchin-m
wants to merge
11
commits into
master
Choose a base branch
from
add-stack-vector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6d24c8b
[perf] use StackVector to save memory & CPU
atuchin-m a48509d
Change get_tokens_optimized()
atuchin-m 5c1e3bf
cargo fmt
atuchin-m 2db0ac2
Fix the test expectations
atuchin-m 906e550
Fix clippy
atuchin-m cba077f
add drop()
atuchin-m c36bbe8
Remove unsafe{}
atuchin-m d6a346b
Restore logic to reserve a free slot for zero token
atuchin-m d3df508
Rename and move the struct
atuchin-m ae17f9b
Cleanup in tests
atuchin-m f8dd45a
use arrayvec
atuchin-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ use seahash::hash; | |
| #[cfg(target_pointer_width = "32")] | ||
| use seahash::reference::hash; | ||
|
|
||
| pub use arrayvec::ArrayVec; | ||
|
|
||
| pub type Hash = u64; | ||
|
|
||
| // A smaller version of Hash that is used in serialized format. | ||
|
|
@@ -27,25 +29,23 @@ fn is_allowed_filter(ch: char) -> bool { | |
| ch.is_alphanumeric() || ch == '%' | ||
| } | ||
|
|
||
| pub(crate) const TOKENS_BUFFER_SIZE: usize = 128; | ||
| pub(crate) const TOKENS_BUFFER_RESERVED: usize = 1; | ||
| const TOKENS_MAX: usize = TOKENS_BUFFER_SIZE - TOKENS_BUFFER_RESERVED; | ||
| pub type TokensBuffer = ArrayVec<Hash, 256>; | ||
|
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. doc comment would be useful here |
||
|
|
||
| fn fast_tokenizer_no_regex( | ||
| pattern: &str, | ||
| is_allowed_code: &dyn Fn(char) -> bool, | ||
| skip_first_token: bool, | ||
| skip_last_token: bool, | ||
| tokens_buffer: &mut Vec<Hash>, | ||
| tokens_buffer: &mut TokensBuffer, | ||
| ) { | ||
| // let mut tokens_buffer_index = 0; | ||
| let mut inside: bool = false; | ||
| let mut start = 0; | ||
| let mut preceding_ch: Option<char> = None; // Used to check if a '*' is not just before a token | ||
|
|
||
| for (i, c) in pattern.char_indices() { | ||
| if tokens_buffer.len() >= TOKENS_MAX { | ||
| return; | ||
| if tokens_buffer.capacity() - tokens_buffer.len() <= 1 { | ||
| return; // reserve one free slot for the zero token | ||
| } | ||
| if is_allowed_code(c) { | ||
| if !inside { | ||
|
|
@@ -75,17 +75,17 @@ fn fast_tokenizer_no_regex( | |
| } | ||
| } | ||
|
|
||
| pub(crate) fn tokenize_pooled(pattern: &str, tokens_buffer: &mut Vec<Hash>) { | ||
| pub(crate) fn tokenize_pooled(pattern: &str, tokens_buffer: &mut TokensBuffer) { | ||
| fast_tokenizer_no_regex(pattern, &is_allowed_filter, false, false, tokens_buffer); | ||
| } | ||
|
|
||
| pub fn tokenize(pattern: &str) -> Vec<Hash> { | ||
| let mut tokens_buffer: Vec<Hash> = Vec::with_capacity(TOKENS_BUFFER_SIZE); | ||
| let mut tokens_buffer = TokensBuffer::default(); | ||
| tokenize_to(pattern, &mut tokens_buffer); | ||
| tokens_buffer | ||
| tokens_buffer.into_iter().collect() | ||
| } | ||
|
|
||
| pub(crate) fn tokenize_to(pattern: &str, tokens_buffer: &mut Vec<Hash>) { | ||
| pub(crate) fn tokenize_to(pattern: &str, tokens_buffer: &mut TokensBuffer) { | ||
| fast_tokenizer_no_regex(pattern, &is_allowed_filter, false, false, tokens_buffer); | ||
| } | ||
|
|
||
|
|
@@ -95,21 +95,21 @@ pub(crate) fn tokenize_filter( | |
| skip_first_token: bool, | ||
| skip_last_token: bool, | ||
| ) -> Vec<Hash> { | ||
| let mut tokens_buffer: Vec<Hash> = Vec::with_capacity(TOKENS_BUFFER_SIZE); | ||
| let mut tokens_buffer = TokensBuffer::default(); | ||
| tokenize_filter_to( | ||
| pattern, | ||
| skip_first_token, | ||
| skip_last_token, | ||
| &mut tokens_buffer, | ||
| ); | ||
| tokens_buffer | ||
| tokens_buffer.into_iter().collect() | ||
| } | ||
|
|
||
| pub(crate) fn tokenize_filter_to( | ||
| pattern: &str, | ||
| skip_first_token: bool, | ||
| skip_last_token: bool, | ||
| tokens_buffer: &mut Vec<Hash>, | ||
| tokens_buffer: &mut TokensBuffer, | ||
| ) { | ||
| fast_tokenizer_no_regex( | ||
| pattern, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 now also a breaking change 🥲