Skip to content
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

Attempt at making TreeNode more flexible #58

Merged
merged 3 commits into from
Mar 20, 2023
Merged

Conversation

ogxd
Copy link
Collaborator

@ogxd ogxd commented Mar 19, 2023

Here is an attempt at making it even more flexible, EG usable with thread IDs.

Main difference is that TreeNode no longer has inclusive / exclusive numeric fields. Instead it will store whatever object you need to store, like a group of thread IDs. Then you can use the optional get_inclusive_value function get an inclusive view of whatever data you have in there. It's a little less performant because it's not precomputed but it's no big hassle since this is computed after the report generation stage.

See this "test" I added:

type FunctionID = u32;
type ThreadID = u32;

// Required to wrap Vec<ThreadID> in order to implement AddAssign
#[derive(Clone, Default, Debug)]
pub struct Threads(Vec<ThreadID>);

// Implement AddAssign for get_inclusive_value to be usable
impl AddAssign for Threads {
    fn add_assign(&mut self, other: Self) {
        self.0.extend(other.0);
    }
}

let sequences: HashMap<Vec<FunctionID>, Threads> = HashMap::from([
    (vec![1, 2, 3], Threads(vec![1001, 1002, 1003])),
    (vec![2, 2, 3], Threads(vec![1004, 1005])),
    (vec![1, 2], Threads(vec![1006])),
    (vec![1, 2, 4], Threads(vec![1007, 1008, 1009])),
    (vec![1, 3, 5], Threads(vec![1010])),
    (vec![2, 3, 2, 1, 4], Threads(vec![1010, 1011, 1012, 1013])),
    (vec![1, 3, 5, 1], Threads(vec![1014, 1015])),
]);

let mut tree = TreeNode::build_from_sequences(&sequences, 0);

println!("Unsorted:");
print(&tree, 0, &|node| format!("{} [inc:{:?}, exc:{:?}]", node.key, node.get_inclusive_value(), node.value));

// Sorts by descending inclusive value
// Here we use get_inclusive_value() to aggregate threads by inclusive paths and then len() to compute to total count for the sorting to operate on
tree.sort_by(&|a, b| b.get_inclusive_value().0.len().cmp(&a.get_inclusive_value().0.len()));

println!("Sorted:");
print(&tree, 0, &|node| format!("{} [inc:{:?}, exc:{:?}]", node.key, node.get_inclusive_value(), node.value));

Here is the sorted output:

- 0 [inc:Threads([1006, 1001, 1002, 1003, 1007, 1008, 1009, 1010, 1014, 1015, 1010, 1011, 1012, 1013, 1004, 1005]), exc:None]
 - 1 [inc:Threads([1006, 1001, 1002, 1003, 1007, 1008, 1009, 1010, 1014, 1015]), exc:None]
  - 2 [inc:Threads([1006, 1001, 1002, 1003, 1007, 1008, 1009]), exc:Some(Threads([1006]))]
   - 3 [inc:Threads([1001, 1002, 1003]), exc:Some(Threads([1001, 1002, 1003]))]
   - 4 [inc:Threads([1007, 1008, 1009]), exc:Some(Threads([1007, 1008, 1009]))]
  - 3 [inc:Threads([1010, 1014, 1015]), exc:None]
   - 5 [inc:Threads([1010, 1014, 1015]), exc:Some(Threads([1010]))]
    - 1 [inc:Threads([1014, 1015]), exc:Some(Threads([1014, 1015]))]
 - 2 [inc:Threads([1010, 1011, 1012, 1013, 1004, 1005]), exc:None]
  - 3 [inc:Threads([1010, 1011, 1012, 1013]), exc:None]
   - 2 [inc:Threads([1010, 1011, 1012, 1013]), exc:None]
    - 1 [inc:Threads([1010, 1011, 1012, 1013]), exc:None]
     - 4 [inc:Threads([1010, 1011, 1012, 1013]), exc:Some(Threads([1010, 1011, 1012, 1013]))]
  - 2 [inc:Threads([1004, 1005]), exc:None]
   - 3 [inc:Threads([1004, 1005]), exc:Some(Threads([1004, 1005]))]

@ogxd ogxd requested a review from Bluezen March 19, 2023 19:03
@ogxd ogxd self-assigned this Mar 19, 2023
@ogxd ogxd changed the base branch from master to stack-utils March 19, 2023 19:10
@Bluezen Bluezen merged commit 8185a9e into stack-utils Mar 20, 2023
ogxd added a commit that referenced this pull request Mar 20, 2023
…ref trees conveniently (#56)

* POC

* Add tree structure for convenient stack traces / reftree building

* Attempt at making TreeNode more flexible (#58)

* Attempt at making TreeNode more flexible

* Fix some bugs

* Improve performance by removing need for cloning when computing inclusive value

---------

Co-authored-by: Olivier Giniaux <oginiaux@smartadserver.com>
@ogxd ogxd deleted the stack-utils-2 branch May 6, 2023 10:30
@ogxd ogxd added this to the Version 1 milestone May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants