Skip to content

Commit

Permalink
perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
brockelmore committed Feb 28, 2024
1 parent c687003 commit d0bfb2e
Show file tree
Hide file tree
Showing 44 changed files with 1,676 additions and 524 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
**/dot.svg
**/dot.dot
**/flamegraph.svg
**/.swp
**/.swp
59 changes: 58 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ solc-expressions = { path = "crates/solc-expressions" }
solang-parser = { version = "0.2.4", features = ["pt-serde"] }
tracing = { version = "0.1", features = ["attributes"] }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter", "fmt"] }
tracing-tree = "0.3.0"
ethers-core = "*"
hex = "0.4.3"
ariadne = "0.2.0"
petgraph = "0.6.2"
ahash = "0.8.10"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ graph.workspace = true
ariadne.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
tracing-tree.workspace = true
petgraph.workspace = true
ethers-core.workspace = true

Expand Down
30 changes: 28 additions & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use graph::elem::RangeElem;
use analyzers::{FunctionVarsBoundAnalyzer, ReportConfig, ReportDisplay};
use graph::{
nodes::{ContractNode, FunctionNode},
Expand All @@ -11,7 +12,7 @@ use shared::Search;
use ariadne::sources;
use clap::{ArgAction, Parser, ValueHint};

use tracing_subscriber::prelude::*;
use tracing_subscriber::{Registry, prelude::*};

use std::{
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -109,8 +110,24 @@ pub fn subscriber() {
.init()
}

pub fn tree_subscriber() {
let subscriber = Registry::default().with(
tracing_tree::HierarchicalLayer::default()
.with_indent_lines(true)
.with_indent_amount(2)
.with_thread_names(true)
// .with_thread_ids(true)
// .with_verbose_exit(true)
// .with_verbose_entry(true)
// .with_targets(true)
)
.with(tracing_subscriber::filter::EnvFilter::from_default_env());
tracing::subscriber::set_global_default(subscriber).unwrap();

}

fn main() {
subscriber();
tree_subscriber();
let args = Args::parse();
let verbosity = args.verbosity;
let config = match verbosity {
Expand Down Expand Up @@ -290,6 +307,15 @@ fn main() {
analyzer.open_mermaid();
}

// println!("{}", analyzer.range_arena.ranges.iter().map(|i| {
// let j = i.borrow();
// let (min_cached, max_cached) = j.is_min_max_cached(&analyzer);
// format!("\t{j}, is cached: {min_cached}, {max_cached}\n")
// }).collect::<Vec<_>>().join(""));
// println!("{}", analyzer.range_arena.map.iter().map(|(k, v)| {
// format!("\t{}: {}\n", k, v)
// }).collect::<Vec<_>>().join(""));

if args.debug {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/graph/src/nodes/context/solving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ impl ContextNode {
}

/// Get the dependencies as normalized solver atoms
pub fn dep_atoms(&self, analyzer: &impl GraphBackend) -> Result<Vec<SolverAtom>, GraphError> {
pub fn dep_atoms(&self, analyzer: &mut impl GraphBackend) -> Result<Vec<SolverAtom>, GraphError> {
let deps: Vec<_> = self.ctx_deps(analyzer)?;
let mut ranges = BTreeMap::default();
deps.iter().try_for_each(|dep| {
let range = dep.ref_range(analyzer)?.unwrap();
let mut range = dep.range(analyzer)?.unwrap();
let r: Cow<'_, SolcRange> = range.flattened_range(analyzer)?;
ranges.insert(*dep, r.into_owned());
Ok(())
Expand Down Expand Up @@ -87,7 +87,7 @@ impl ContextNode {
// let underlying = self.underlying_mut(analyzer)?;
if !self.underlying(analyzer)?.ctx_deps.contains(&dep) {
// dep.cache_flattened_range(analyzer)?;
let range = dep.ref_range(analyzer)?.unwrap();
let mut range = dep.range(analyzer)?.unwrap();
let r = range.flattened_range(analyzer)?.into_owned();
// add the atomic constraint
if let Some(atom) = r.min.atomize(analyzer) {
Expand Down
18 changes: 16 additions & 2 deletions crates/graph/src/nodes/context/var/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ impl ContextVarNode {
if let Some(ref_range) = self.ref_range(analyzer)? {
let min_name = ref_range
.range_min()
.simplify_minimize(&mut Default::default(), analyzer)?
.simplify_minimize(analyzer)?
.to_range_string(false, analyzer)
.s;

let max_name = ref_range
.range_max()
.simplify_maximize(&mut Default::default(), analyzer)?
.simplify_maximize(analyzer)?
.to_range_string(true, analyzer)
.s;
if max_name == min_name {
Expand Down Expand Up @@ -205,6 +205,20 @@ impl ContextVarNode {
Ok(self.underlying(analyzer)?.tmp_of())
}

pub fn struct_to_fields(&self, analyzer: &impl GraphBackend) -> Result<Vec<ContextVarNode>, GraphError> {
if self.ref_range(analyzer)?.is_none() {
let fields = analyzer
.graph()
.edges_directed(self.first_version(analyzer).into(), Direction::Incoming)
.filter(|edge| *edge.weight() == Edge::Context(ContextEdge::AttrAccess("field")))
.map(|edge| ContextVarNode::from(edge.source()).latest_version(analyzer))
.collect::<Vec<_>>();
Ok(fields)
} else {
Ok(vec![])
}
}

pub fn array_to_len_var(&self, analyzer: &impl GraphBackend) -> Option<ContextVarNode> {
if let Some(len) = analyzer
.graph()
Expand Down
31 changes: 25 additions & 6 deletions crates/graph/src/nodes/func_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,26 @@ impl FunctionNode {
Ok(())
}

// fn returns_inner(
// &self,
// analyzer: &impl GraphBackend,
// ) -> Vec<FunctionReturnNode> {
// self.underlying(analyzer).unwrap().cache.returns.iter()
// // } else {
// // analyzer
// // .graph()
// // .edges_directed(self.0.into(), Direction::Incoming)
// // .filter(|edge| Edge::FunctionReturn == *edge.weight())
// // .map(|edge| FunctionReturnNode::from(edge.source()))
// // .collect()
// // }
// }

pub fn returns<'a>(
&self,
analyzer: &'a impl GraphBackend,
) -> impl Iterator<Item = FunctionReturnNode> + 'a {
analyzer
.graph()
.edges_directed(self.0.into(), Direction::Incoming)
.filter(|edge| Edge::FunctionReturn == *edge.weight())
.map(|edge| FunctionReturnNode::from(edge.source()))
) -> &'a [FunctionReturnNode] {
self.underlying(analyzer).unwrap().cache.returns.as_ref().unwrap()
}

pub fn is_public_or_ext(&self, analyzer: &impl GraphBackend) -> Result<bool, GraphError> {
Expand All @@ -462,6 +473,14 @@ impl FunctionNode {
.any(|attr| matches!(attr, FunctionAttribute::Mutability(Mutability::Pure(_)))))
}

pub fn is_view(&self, analyzer: &impl GraphBackend) -> Result<bool, GraphError> {
Ok(self
.underlying(analyzer)?
.attributes
.iter()
.any(|attr| matches!(attr, FunctionAttribute::Mutability(Mutability::View(_)))))
}

pub fn get_overriding(
&self,
other: &Self,
Expand Down
19 changes: 16 additions & 3 deletions crates/graph/src/range/elem/concrete.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

use crate::{
nodes::{Concrete, ContextVarNode},
range::elem::{Elem, RangeElem},
Expand All @@ -7,16 +8,30 @@ use crate::{
use shared::NodeIdx;

use std::collections::BTreeMap;
use std::hash::{Hash, Hasher};

use solang_parser::pt::Loc;

/// A concrete value for a range element
#[derive(Default, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[derive(Default, Clone, Debug, Ord, PartialOrd)]
pub struct RangeConcrete<T> {
pub val: T,
pub loc: Loc,
}

impl<T: std::cmp::PartialEq> PartialEq for RangeConcrete<T> {
fn eq(&self, other: &Self) -> bool {
self.val == other.val
}
}
impl<T: std::cmp::PartialEq> Eq for RangeConcrete<T> {}

impl Hash for RangeConcrete<Concrete> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.val.hash(state);
}
}

impl From<Concrete> for RangeConcrete<Concrete> {
fn from(c: Concrete) -> Self {
Self {
Expand Down Expand Up @@ -149,14 +164,12 @@ impl RangeElem<Concrete> for RangeConcrete<Concrete> {

fn simplify_maximize(
&self,
_seen_ops: &mut BTreeMap<Elem<Concrete>, Elem<Concrete>>,
_analyzer: &impl GraphBackend,
) -> Result<Elem<Concrete>, GraphError> {
Ok(Elem::Concrete(self.clone()))
}
fn simplify_minimize(
&self,
_seen_ops: &mut BTreeMap<Elem<Concrete>, Elem<Concrete>>,
_analyzer: &impl GraphBackend,
) -> Result<Elem<Concrete>, GraphError> {
Ok(Elem::Concrete(self.clone()))
Expand Down
Loading

0 comments on commit d0bfb2e

Please sign in to comment.