Skip to content

Commit

Permalink
perf
Browse files Browse the repository at this point in the history
  • Loading branch information
brockelmore committed Mar 18, 2024
1 parent 7bf324c commit b703c00
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 40 deletions.
83 changes: 58 additions & 25 deletions crates/graph/src/nodes/context/var/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,41 +309,74 @@ impl ContextVarNode {
.collect()
}

pub fn dependent_on(
pub fn set_dependent_on(
&self,
analyzer: &impl GraphBackend,
return_self: bool,
) -> Result<Vec<Self>, GraphError> {
self.dependent_on_no_recursion(analyzer, &mut vec![*self], return_self)
analyzer: &mut impl GraphBackend,
) -> Result<(), GraphError> {
let mut return_self = false;
let mut first_iter = true;
let mut stack = vec![*self];
let mut result = vec![];

while let Some(node) = stack.pop() {
if result.contains(&node) {
continue;
}

let underlying = node.underlying(analyzer)?;
if let Some(tmp) = underlying.tmp_of() {
stack.push(tmp.lhs);
if let Some(rhs) = tmp.rhs {
stack.push(rhs);
}
} else if return_self {
result.push(node);
}

if first_iter {
first_iter = false;
return_self = true;
}
}

self.underlying_mut(analyzer)?.dep_on = Some(result);
Ok(())
}

fn dependent_on_no_recursion(
pub fn dependent_on(
&self,
analyzer: &impl GraphBackend,
seen: &mut Vec<Self>,
return_self: bool,
mut return_self: bool,
) -> Result<Vec<Self>, GraphError> {
let underlying = self.underlying(analyzer)?;
if let Some(tmp) = underlying.tmp_of() {
let mut nodes = if !seen.contains(&tmp.lhs) {
seen.push(tmp.lhs);
tmp.lhs.dependent_on(analyzer, true)?
} else {
vec![]
};
if let Some(dep_on) = &self.underlying(analyzer)?.dep_on {
return Ok(dep_on.to_vec());
}
let mut first_iter = true;
let mut stack = vec![*self];
let mut result = vec![];

if let Some(rhs) = tmp.rhs {
if !seen.contains(&rhs) {
seen.push(rhs);
nodes.extend(rhs.dependent_on(analyzer, true)?);
while let Some(node) = stack.pop() {
if result.contains(&node) {
continue;
}

let underlying = node.underlying(analyzer)?;
if let Some(tmp) = underlying.tmp_of() {
stack.push(tmp.lhs);
if let Some(rhs) = tmp.rhs {
stack.push(rhs);
}
} else if return_self {
result.push(node);
}

if first_iter {
first_iter = false;
return_self = true;
}
Ok(nodes)
} else if return_self {
Ok(vec![*self])
} else {
Ok(vec![])
}

Ok(result)
}

pub fn graph_dependent_on(
Expand Down
14 changes: 14 additions & 0 deletions crates/graph/src/nodes/context/var/underlying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct ContextVar {
pub storage: Option<StorageLocation>,
pub is_tmp: bool,
pub tmp_of: Option<TmpConstruction>,
pub dep_on: Option<Vec<ContextVarNode>>,
pub is_symbolic: bool,
pub is_return: bool,
pub ty: VarType,
Expand Down Expand Up @@ -76,6 +77,7 @@ impl ContextVar {
storage: None,
is_tmp: true,
tmp_of: None,
dep_on: None,
is_symbolic: false,
is_return: false,
ty: VarType::Concrete(concrete_node),
Expand Down Expand Up @@ -128,6 +130,7 @@ impl ContextVar {
storage: None,
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty: VarType::User(
Expand All @@ -154,6 +157,7 @@ impl ContextVar {
storage: Some(StorageLocation::Memory(Loc::Implicit)),
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty: VarType::User(TypeNode::Struct(struct_node), None),
Expand All @@ -177,6 +181,7 @@ impl ContextVar {
storage: Some(StorageLocation::Memory(Loc::Implicit)),
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty: VarType::try_from_idx(analyzer, ty_node.0.into()).unwrap(),
Expand All @@ -195,6 +200,7 @@ impl ContextVar {
storage: None,
is_tmp: true,
tmp_of: None,
dep_on: None,
is_symbolic: false,
is_return: false,
ty: VarType::try_from_idx(analyzer, bn_node.into()).unwrap(),
Expand Down Expand Up @@ -490,6 +496,7 @@ impl ContextVar {
storage,
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty,
Expand Down Expand Up @@ -517,6 +524,7 @@ impl ContextVar {
storage: parent_var.storage,
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty,
Expand All @@ -541,6 +549,7 @@ impl ContextVar {
storage: None,
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty: VarType::User(
Expand All @@ -566,6 +575,7 @@ impl ContextVar {
storage: Some(parent_storage),
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: index.underlying(analyzer)?.is_symbolic,
is_return: false,
ty: parent_var.dynamic_underlying_ty(analyzer)?,
Expand All @@ -583,6 +593,7 @@ impl ContextVar {
storage: None,
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: false,
is_return: false,
ty: VarType::User(TypeNode::Func(func), None),
Expand All @@ -602,6 +613,7 @@ impl ContextVar {
storage: param.storage,
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty,
Expand All @@ -627,6 +639,7 @@ impl ContextVar {
storage: ret.storage,
is_tmp: false,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: true,
ty,
Expand Down Expand Up @@ -658,6 +671,7 @@ impl ContextVar {
storage: ret.storage,
is_tmp,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: true,
ty,
Expand Down
1 change: 1 addition & 0 deletions crates/graph/src/nodes/var_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl VarNode {
display_name: self.name(analyzer)?,
storage: None,
is_tmp: false,
dep_on: None,
tmp_of: None,
is_symbolic: true,
is_return: false,
Expand Down
5 changes: 3 additions & 2 deletions crates/graph/src/range/elem/elem_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ impl Elem<Concrete> {
},
Elem::Null => {}
Elem::Arena(_) => {
let s = self.dearenaize(analyzer).clone();
s.borrow_mut().replace_dep(to_replace, replacement, analyzer);
let mut s = self.dearenaize(analyzer).borrow().clone();
s.replace_dep(to_replace, replacement, analyzer);
*self = Elem::Arena(analyzer.range_arena_idx_or_upsert(s));
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/solc-expressions/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ pub trait Array: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
RangeOp::SetIndices,
Some(index),
)),
dep_on: {
let mut deps = parent.dependent_on(self, true).into_expr_err(loc)?;
deps.extend(index.dependent_on(self, true).into_expr_err(loc)?);
Some(deps)
},
is_symbolic: true,
is_return: false,
ty,
Expand Down
7 changes: 7 additions & 0 deletions crates/solc-expressions/src/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ pub trait Assign: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized

new_lhs.underlying_mut(self).into_expr_err(loc)?.tmp_of =
rhs_cvar.tmp_of(self).into_expr_err(loc)?;

if let Some(ref mut dep_on) = new_lhs.underlying_mut(self).into_expr_err(loc)?.dep_on {
dep_on.push(rhs_cvar)
} else {
new_lhs.set_dependent_on(self).into_expr_err(loc)?;
}

if lhs_cvar.is_storage(self).into_expr_err(loc)? {
self.add_edge(new_lhs, rhs_cvar, Edge::Context(ContextEdge::StorageWrite));
}
Expand Down
21 changes: 20 additions & 1 deletion crates/solc-expressions/src/bin_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,16 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {

let new_lhs = if assign {
let new = self.advance_var_in_ctx_forcible(lhs_cvar, loc, ctx, true)?;
new.underlying_mut(self).into_expr_err(loc)?.tmp_of =
let underlying = new.underlying_mut(self).into_expr_err(loc)?;
underlying.tmp_of =
Some(TmpConstruction::new(lhs_cvar, op, Some(rhs_cvar)));

if let Some(ref mut dep_on) = underlying.dep_on {
dep_on.push(rhs_cvar)
} else {
new.set_dependent_on(self).into_expr_err(loc)?;
}

new
} else {
let mut new_lhs_underlying = ContextVar {
Expand All @@ -188,6 +196,11 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
|| rhs_cvar.is_symbolic(self).into_expr_err(loc)?,
is_return: false,
tmp_of: Some(TmpConstruction::new(lhs_cvar, op, Some(rhs_cvar))),
dep_on: {
let mut deps = lhs_cvar.dependent_on(self, true).into_expr_err(loc)?;
deps.extend(rhs_cvar.dependent_on(self, true).into_expr_err(loc)?);
Some(deps)
},
ty: lhs_cvar.underlying(self).into_expr_err(loc)?.ty.clone(),
};

Expand Down Expand Up @@ -575,6 +588,11 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
RangeOp::Gte,
Some(zero_node.into()),
)),
dep_on: {
let mut deps = tmp_rhs.dependent_on(self, true).into_expr_err(loc)?;
deps.push(zero_node.into());
Some(deps)
},
is_symbolic: true,
is_return: false,
ty: VarType::BuiltIn(
Expand Down Expand Up @@ -703,6 +721,7 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
storage: None,
is_tmp: true,
tmp_of: Some(TmpConstruction::new(lhs_cvar, RangeOp::BitNot, None)),
dep_on: Some(lhs_cvar.dependent_on(self, true).into_expr_err(loc)?),
is_symbolic: lhs_cvar.is_symbolic(self).into_expr_err(loc)?,
is_return: false,
ty: lhs_cvar.underlying(self).into_expr_err(loc)?.ty.clone(),
Expand Down
6 changes: 6 additions & 0 deletions crates/solc-expressions/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub trait Cmp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
storage: None,
is_tmp: true,
tmp_of: Some(TmpConstruction::new(lhs_cvar, RangeOp::Not, None)),
dep_on: Some(lhs_cvar.dependent_on(self, true).into_expr_err(loc)?),
is_symbolic: lhs_cvar.is_symbolic(self).into_expr_err(loc)?,
is_return: false,
ty: VarType::BuiltIn(
Expand Down Expand Up @@ -221,6 +222,11 @@ pub trait Cmp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
.into_expr_err(loc)?,
is_return: false,
tmp_of: Some(TmpConstruction::new(lhs_cvar, op, Some(rhs_cvar))),
dep_on: {
let mut deps = lhs_cvar.dependent_on(self, true).into_expr_err(loc)?;
deps.extend(rhs_cvar.dependent_on(self, true).into_expr_err(loc)?);
Some(deps)
},
ty: VarType::BuiltIn(
BuiltInNode::from(self.builtin_or_add(Builtin::Bool)),
Some(range),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub trait ConstructorCaller:
is_symbolic: false,
is_return: false,
tmp_of: None,
dep_on: None,
ty: ty.expect("No type for node"),
};

Expand All @@ -66,6 +67,7 @@ pub trait ConstructorCaller:
storage: None,
is_tmp: true,
tmp_of: None,
dep_on: None,
is_symbolic: true,
is_return: false,
ty: ContextVarNode::from(len_cvar)
Expand Down
Loading

0 comments on commit b703c00

Please sign in to comment.