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

Store scope and symbol stacks in fixed direction #226

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions stack-graphs/include/stack-graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ struct sg_partial_scope_stack {
// The handle of the first element in the partial scope stack, or SG_LIST_EMPTY_HANDLE if the
// list is empty, or 0 if the list is null.
sg_partial_scope_stack_cell_handle cells;
enum sg_deque_direction direction;
uint32_t length;
// The scope stack variable representing the unknown content of a partial scope stack, or 0 if
// the variable is missing. (If so, this partial scope stack can only match a scope stack
Expand Down Expand Up @@ -345,7 +344,6 @@ struct sg_partial_symbol_stack {
// The handle of the first element in the partial symbol stack, or SG_LIST_EMPTY_HANDLE if the
// list is empty, or 0 if the list is null.
sg_partial_symbol_stack_cell_handle cells;
enum sg_deque_direction direction;
uint32_t length;
// The symbol stack variable representing the unknown content of a partial symbol stack, or 0
// if the variable is missing. (If so, this partial symbol stack can only match a symbol
Expand Down
9 changes: 6 additions & 3 deletions stack-graphs/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ where
/// Ensures that the reversal of this list is available. It can be useful to precalculate this
/// when you have mutable access to the arena, so that you can then reverse and un-reverse the
/// list later when you only have shared access to it.
pub fn ensure_reversal_available(&mut self, arena: &mut ReversibleListArena<T>) {
pub fn ensure_reversal_available(&self, arena: &mut ReversibleListArena<T>) {
// First check to see if the list has already been reversed.
if self.is_empty() {
// The empty list is already reversed.
Expand Down Expand Up @@ -869,9 +869,7 @@ impl<T> Copy for ReversibleList<T> {}
///
/// [`List`]: struct.List.html
#[repr(C)]
#[derive(Niche)]
pub struct Deque<T> {
#[niche]
list: ReversibleList<T>,
direction: DequeDirection,
}
Expand Down Expand Up @@ -964,6 +962,11 @@ where
self.direction = DequeDirection::Forwards;
}

/// Ensures that this deque has computed both directions of elements.
pub fn ensure_both_directions(&self, arena: &mut DequeArena<T>) {
self.list.ensure_reversal_available(arena);
}

/// Pushes a new element onto the front of this deque.
pub fn push_front(&mut self, arena: &mut DequeArena<T>, element: T) {
self.ensure_forwards(arena);
Expand Down
27 changes: 11 additions & 16 deletions stack-graphs/src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,6 @@ pub struct sg_partial_symbol_stack {
/// The handle of the first element in the partial symbol stack, or SG_LIST_EMPTY_HANDLE if the
/// list is empty, or 0 if the list is null.
pub cells: sg_partial_symbol_stack_cell_handle,
pub direction: sg_deque_direction,
pub length: u32,
/// The symbol stack variable representing the unknown content of a partial symbol stack, or 0
/// if the variable is missing. (If so, this partial symbol stack can only match a symbol
Expand Down Expand Up @@ -854,13 +853,12 @@ pub extern "C" fn sg_partial_path_arena_add_partial_symbol_stacks(
} else {
PartialSymbolStack::from_variable(variables[i].try_into().unwrap())
};
for j in 0..length {
for j in (0..length).rev() {
let symbol = symbols_slice[j].into();
stack.push_back(partials, symbol);
stack.push_front(partials, symbol);
}
// We pushed the edges onto the list in reverse order. Requesting a forwards iterator
// before we return ensures that it will also be available in forwards order.
let _ = stack.iter(partials);
// We ensure that the list will also be available in reverse order.
stack.ensure_both_directions(partials);
out[i] = stack.into();
unsafe { symbols = symbols.add(length) };
}
Expand All @@ -880,7 +878,6 @@ pub struct sg_partial_scope_stack {
/// The handle of the first element in the partial scope stack, or SG_LIST_EMPTY_HANDLE if the
/// list is empty, or 0 if the list is null.
pub cells: sg_partial_scope_stack_cell_handle,
pub direction: sg_deque_direction,
pub length: u32,
/// The scope stack variable representing the unknown content of a partial scope stack, or 0 if
/// the variable is missing. (If so, this partial scope stack can only match a scope stack
Expand Down Expand Up @@ -964,13 +961,12 @@ pub extern "C" fn sg_partial_path_arena_add_partial_scope_stacks(
} else {
PartialScopeStack::from_variable(variables[i].try_into().unwrap())
};
for j in 0..length {
for j in (0..length).rev() {
let node = scopes_slice[j].into();
stack.push_back(partials, node);
stack.push_front(partials, node);
}
// We pushed the edges onto the list in reverse order. Requesting a forwards iterator
// before we return ensures that it will also be available in forwards order.
let _ = stack.iter_scopes(partials);
// We ensure that the list will also be available in reverse order.
stack.ensure_both_directions(partials);
out[i] = stack.into();
unsafe { scopes = scopes.add(length) };
}
Expand Down Expand Up @@ -1077,9 +1073,8 @@ pub extern "C" fn sg_partial_path_arena_add_partial_path_edge_lists(
let edge: PartialPathEdge = edges_slice[j].into();
list.push_back(partials, edge);
}
// We pushed the edges onto the list in reverse order. Requesting a forwards iterator
// before we return ensures that it will also be available in forwards order.
let _ = list.iter(partials);
// We ensure that the list will also be available in reverse order.
list.ensure_both_directions(partials);
out[i] = list.into();
unsafe { edges = edges.add(length) };
}
Expand Down Expand Up @@ -1182,7 +1177,7 @@ pub extern "C" fn sg_partial_path_arena_find_partial_paths_in_file(
graph,
file,
&AtomicUsizeCancellationFlag(cancellation_flag),
|_graph, partials, mut path| {
|_graph, partials, path| {
path.ensure_both_directions(partials);
partial_path_list.partial_paths.push(path);
},
Expand Down
4 changes: 2 additions & 2 deletions stack-graphs/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ impl Serialize for InPartialPaths<'_, &PartialSymbolStack> {
if symbol_stack.has_variable() {
len += 1;
}
let symbols = symbol_stack.iter_unordered(paths).collect::<Vec<_>>();
let symbols = symbol_stack.iter(paths).collect::<Vec<_>>();

let mut ser = serializer.serialize_struct("partial_symbol_stack", len)?;
ser.serialize_field("symbols", &self.with(&symbols))?;
Expand Down Expand Up @@ -765,7 +765,7 @@ impl Serialize for InPartialPaths<'_, &PartialScopeStack> {
len += 1;
}
let scopes = scope_stack
.iter_unordered(paths)
.iter(paths)
.map(|n| graph[n].id())
.collect::<Vec<_>>();

Expand Down
Loading