-
Notifications
You must be signed in to change notification settings - Fork 213
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
chore: manage call stacks using a tree #6791
Open
guipublic
wants to merge
17
commits into
master
Choose a base branch
from
gd/issue_6603
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 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f3e30a1
manage call stacks with a tree
guipublic 690ce1c
Merge branch 'master' into gd/issue_6603
guipublic 3255400
fix merge issues
guipublic 23f6af1
fix unit tests
guipublic 647cecd
Code review
guipublic 644e0db
Merge branch 'master' into gd/issue_6603
guipublic 456eae5
simple fix for the performance issue
guipublic 58a0fa8
format
guipublic 79793d0
fix merge issue
guipublic 79a229d
format
guipublic 523aa14
switch callstack to Vector
guipublic b5970ce
Merge branch 'master' into gd/issue_6603
guipublic 7d42406
fix unit test
guipublic 773214f
Merge branch 'master' into gd/issue_6603
guipublic 10cdd92
refactor call_stack
guipublic 4ad0806
Merge branch 'master' into gd/issue_6603
TomAFrench 1299d7b
Code review
guipublic 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 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 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use noirc_errors::Location; | ||
|
||
pub(crate) type CallStack = im::Vector<Location>; | ||
|
||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub(crate) struct CallStackId(u32); | ||
|
||
impl CallStackId { | ||
pub(crate) fn root() -> Self { | ||
Self::new(0) | ||
} | ||
|
||
fn new(id: usize) -> Self { | ||
Self(id as u32) | ||
} | ||
|
||
pub(crate) fn index(&self) -> usize { | ||
self.0 as usize | ||
} | ||
|
||
pub(crate) fn is_root(&self) -> bool { | ||
self.0 == 0 | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub(crate) struct LocationNode { | ||
pub(crate) parent: Option<CallStackId>, | ||
pub(crate) children: Vec<CallStackId>, | ||
pub(crate) value: Location, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub(crate) struct CallStackHelper { | ||
locations: Vec<LocationNode>, | ||
} | ||
|
||
impl CallStackHelper { | ||
/// Construct a CallStack from a CallStackId | ||
pub(crate) fn get_call_stack(&self, mut call_stack: CallStackId) -> CallStack { | ||
let mut result = im::Vector::new(); | ||
while let Some(parent) = self.locations[call_stack.index()].parent { | ||
result.push_back(self.locations[call_stack.index()].value); | ||
call_stack = parent; | ||
} | ||
result | ||
} | ||
|
||
/// Returns a new CallStackId which extends the call_stack with the provided call_stack. | ||
pub(crate) fn extend_call_stack( | ||
&mut self, | ||
mut call_stack: CallStackId, | ||
locations: &CallStack, | ||
) -> CallStackId { | ||
for location in locations { | ||
call_stack = self.add_child(call_stack, *location); | ||
} | ||
call_stack | ||
} | ||
|
||
/// Adds a location to the call stack | ||
pub(crate) fn add_child(&mut self, call_stack: CallStackId, location: Location) -> CallStackId { | ||
if let Some(result) = self.locations[call_stack.index()] | ||
.children | ||
.iter() | ||
.rev() | ||
.take(1000) | ||
.find(|child| self.locations[child.index()].value == location) | ||
{ | ||
return *result; | ||
} | ||
self.locations.push(LocationNode { | ||
parent: Some(call_stack), | ||
children: vec![], | ||
value: location, | ||
}); | ||
let new_location = CallStackId::new(self.locations.len() - 1); | ||
self.locations[call_stack.index()].children.push(new_location); | ||
new_location | ||
} | ||
|
||
/// Retrieve the CallStackId corresponding to call_stack with the last 'len' locations removed. | ||
pub(crate) fn unwind_call_stack( | ||
&self, | ||
mut call_stack: CallStackId, | ||
mut len: usize, | ||
) -> CallStackId { | ||
while len > 0 { | ||
if let Some(parent) = self.locations[call_stack.index()].parent { | ||
len -= 1; | ||
call_stack = parent; | ||
} else { | ||
break; | ||
} | ||
} | ||
call_stack | ||
} | ||
|
||
pub(crate) fn add_location_to_root(&mut self, location: Location) -> CallStackId { | ||
if self.locations.is_empty() { | ||
self.locations.push(LocationNode { parent: None, children: vec![], value: location }); | ||
CallStackId::root() | ||
} else { | ||
self.add_child(CallStackId::root(), location) | ||
} | ||
} | ||
|
||
/// Get (or create) a CallStackId corresponding to the given locations | ||
pub(crate) fn get_or_insert_locations(&mut self, locations: CallStack) -> CallStackId { | ||
self.extend_call_stack(CallStackId::root(), &locations) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
I assume we're searching in reverse so that we find recent additions faster; is there any significance as to why give up after 1000 children, after which a duplicate entry is acceptable? Is that value something that was actually observed?
I wonder if you thought some reverse index from e.g. the hash of the location to the child
CallStackId
could be used to make this faster and easier to reason about? Likechildren: HashMap<u64, CallStackId>
where the key isfxhash::hash64(location)
.