Skip to content

Commit d29aee8

Browse files
committed
correct fault in collapse-threads command
do not assume that the thread_id states the order of the start time per thread
1 parent 3e14f95 commit d29aee8

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

crox/src/main.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,34 @@ fn generate_thread_to_collapsed_thread_mapping(
7171
.or_insert_with(|| (event.timestamp, event.timestamp));
7272
}
7373
// collect the the threads in order of the end time
74-
let mut end_to_thread = thread_start_and_end
74+
let mut end_and_thread = thread_start_and_end
7575
.iter()
7676
.map(|(&thread_id, &(_start, end))| (end, thread_id))
7777
.collect::<Vec<_>>();
7878

79-
end_to_thread.sort_unstable_by_key(|&(end, _thread_id)| end);
80-
let mut next_end_iter = end_to_thread.iter();
79+
end_and_thread.sort_unstable_by_key(|&(end, _thread_id)| end);
80+
let mut next_end_iter = end_and_thread.iter().peekable();
8181

82-
// used to get the thread that was first to end
83-
let &(temp_next_end, temp_next_thread_id) = next_end_iter.next().unwrap();
84-
let mut next_end = temp_next_end;
85-
let mut next_thread_id = temp_next_thread_id;
82+
// collect the the threads in order of the start time
83+
let mut start_and_thread = thread_start_and_end
84+
.iter()
85+
.map(|(&thread_id, &(start, _end))| (start, thread_id))
86+
.collect::<Vec<_>>();
87+
88+
start_and_thread.sort_unstable_by_key(|&(start, _thread_id)| start);
8689

8790
let mut current_thread_id = 0; // use new thread_ids to avoid strange gaps in the numbers
88-
for (&thread_id, &(start, _end)) in thread_start_and_end.iter() {
89-
if start > next_end {
91+
for &(start, thread_id) in start_and_thread.iter() {
92+
// safe to unwrap due to end_and_thread and start_and_thread have the same length
93+
let (next_end, next_thread_id) = next_end_iter.peek().unwrap();
94+
if start > *next_end {
95+
next_end_iter.next();
9096
// need to lookup the thread_id due to new and collapsed threads
9197
let mapped_thread_id = *thread_to_collapsed_thread
9298
.get(&next_thread_id)
9399
.unwrap_or(&next_thread_id);
94100

95101
thread_to_collapsed_thread.insert(thread_id, mapped_thread_id);
96-
let &(temp_next_end, temp_next_thread_id) = next_end_iter.next().unwrap();
97-
next_end = temp_next_end;
98-
next_thread_id = temp_next_thread_id;
99102
} else {
100103
thread_to_collapsed_thread.insert(thread_id, current_thread_id);
101104
current_thread_id += 1;

0 commit comments

Comments
 (0)