-
Notifications
You must be signed in to change notification settings - Fork 3
perf: Improve time complexity for find_or_add
#14
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
Draft
kdy1
wants to merge
2
commits into
khonsulabs:main
Choose a base branch
from
kdy1:kdy1/find-symbol
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 std::ops::Range; | |
| use std::usize; | ||
|
|
||
| use byteorder::WriteBytesExt; | ||
| use indexmap::IndexMap; | ||
| use rustc_hash::FxBuildHasher; | ||
| use serde::de::{SeqAccess, Visitor}; | ||
| use serde::{ser, Deserialize, Serialize}; | ||
| #[cfg(feature = "tracing")] | ||
|
|
@@ -571,7 +573,7 @@ impl Debug for EphemeralSymbolMap { | |
| pub struct SymbolMap { | ||
| symbols: String, | ||
| entries: Vec<(Range<usize>, u32)>, | ||
| static_lookup: Vec<(usize, u32)>, | ||
| static_lookup: IndexMap<usize, u32, FxBuildHasher>, | ||
| compatibility: Compatibility, | ||
| } | ||
|
|
||
|
|
@@ -599,7 +601,7 @@ impl SymbolMap { | |
| Self { | ||
| symbols: String::new(), | ||
| entries: Vec::new(), | ||
| static_lookup: Vec::new(), | ||
| static_lookup: IndexMap::with_hasher(FxBuildHasher), | ||
| compatibility: Compatibility::const_default(), | ||
| } | ||
| } | ||
|
|
@@ -649,40 +651,36 @@ impl SymbolMap { | |
| // address of the str in the map. | ||
| let symbol_address = symbol.as_ptr() as usize; | ||
| // Perform a binary search to find this existing element. | ||
| match self | ||
| .static_lookup | ||
| .binary_search_by(|check| symbol_address.cmp(&check.0)) | ||
| { | ||
| Ok(position) => RegisteredSymbol { | ||
| id: self.static_lookup[position].1, | ||
| match self.static_lookup.entry(symbol_address) { | ||
| indexmap::map::Entry::Occupied(position) => RegisteredSymbol { | ||
| id: *position.get(), | ||
| new: false, | ||
| }, | ||
| Err(position) => { | ||
| indexmap::map::Entry::Vacant(position) => { | ||
| // This static symbol hasn't been encountered before. | ||
| let symbol = self.find_entry_by_str(symbol); | ||
| self.static_lookup | ||
| .insert(position, (symbol_address, symbol.id)); | ||
| let symbol = Self::find_entry_by_str(&mut self.entries, &mut self.symbols, symbol); | ||
| position.insert(symbol.id); | ||
| symbol | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[allow(clippy::cast_possible_truncation)] | ||
| fn find_entry_by_str(&mut self, symbol: &str) -> RegisteredSymbol { | ||
| match self | ||
| .entries | ||
| .binary_search_by(|check| self.symbols[check.0.clone()].cmp(symbol)) | ||
| { | ||
| fn find_entry_by_str( | ||
| entries: &mut Vec<(Range<usize>, u32)>, | ||
| symbols: &mut String, | ||
| symbol: &str, | ||
| ) -> RegisteredSymbol { | ||
| match entries.binary_search_by(|check| symbols[check.0.clone()].cmp(symbol)) { | ||
| Ok(index) => RegisteredSymbol { | ||
| id: self.entries[index].1, | ||
| id: entries[index].1, | ||
| new: false, | ||
| }, | ||
| Err(insert_at) => { | ||
| let id = self.entries.len() as u32; | ||
| let start = self.symbols.len(); | ||
| self.symbols.push_str(symbol); | ||
| self.entries | ||
| .insert(insert_at, (start..self.symbols.len(), id)); | ||
| let id = entries.len() as u32; | ||
| let start = symbols.len(); | ||
| symbols.push_str(symbol); | ||
| entries.insert(insert_at, (start..symbols.len(), id)); | ||
|
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. wont you have the same 'insert' concern here? whenever we insert a new static string via |
||
| RegisteredSymbol { id, new: true } | ||
| } | ||
| } | ||
|
|
@@ -693,7 +691,7 @@ impl SymbolMap { | |
| /// Returns true if this symbol had not previously been registered. Returns | ||
| /// false if the symbol was already included in the map. | ||
| pub fn insert(&mut self, symbol: &str) -> bool { | ||
| self.find_entry_by_str(symbol).new | ||
| Self::find_entry_by_str(&mut self.entries, &mut self.symbols, symbol).new | ||
| } | ||
|
|
||
| /// Returns the number of entries in this map. | ||
|
|
||
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.
nit: update comment