From 891056631f898319e85b48ef728c83eb85453ef1 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Wed, 22 Sep 2021 16:04:25 +0800 Subject: [PATCH] nac3core: use Arc to reduce copy --- nac3core/src/codegen/mod.rs | 6 ++--- nac3core/src/toplevel/composer.rs | 39 ++++++++++++++++++------------- nac3core/src/toplevel/mod.rs | 4 ++-- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 413572906..fca41345a 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -42,7 +42,7 @@ pub struct CodeGenContext<'ctx, 'a> { pub var_assignment: HashMap>, pub type_cache: HashMap>, pub primitives: PrimitiveStore, - pub calls: HashMap, + pub calls: Arc>, pub registry: &'a WorkerRegistry, // stores the alloca for variables pub init_bb: BasicBlock<'ctx>, @@ -187,8 +187,8 @@ pub struct CodeGenTask { pub subst: Vec<(Type, Type)>, pub symbol_name: String, pub signature: FunSignature, - pub body: Vec>>, - pub calls: HashMap, + pub body: Arc>>>, + pub calls: Arc>, pub unifier: (SharedUnifier, PrimitiveStore), pub resolver: Arc>, } diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 1e915a33d..5c6817b63 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1344,22 +1344,24 @@ impl TopLevelComposer { calls: &mut calls, }; - let fun_body = if let ast::StmtKind::FunctionDef { body, decorator_list, .. } = - ast.clone().unwrap().node - { - if !decorator_list.is_empty() && - matches!(&decorator_list[0].node, - ast::ExprKind::Name{ id, .. } if id == "syscall") { - instance_to_symbol.insert("".to_string(), simple_name.clone()); - continue + let fun_body = + if let ast::StmtKind::FunctionDef { body, decorator_list, .. } = + ast.clone().unwrap().node + { + if !decorator_list.is_empty() + && matches!(&decorator_list[0].node, + ast::ExprKind::Name{ id, .. } if id == "syscall") + { + instance_to_symbol.insert("".to_string(), simple_name.clone()); + continue; + } + body + } else { + unreachable!("must be function def ast") } - body - } else { - unreachable!("must be function def ast") - } - .into_iter() - .map(|b| inferencer.fold_stmt(b)) - .collect::, _>>()?; + .into_iter() + .map(|b| inferencer.fold_stmt(b)) + .collect::, _>>()?; let returned = inferencer.check_block(fun_body.as_slice(), &mut identifiers)?; @@ -1393,7 +1395,12 @@ impl TopLevelComposer { }) .join(", ") }, - FunInstance { body: fun_body, unifier_id: 0, calls, subst }, + FunInstance { + body: Arc::new(fun_body), + unifier_id: 0, + calls: Arc::new(calls), + subst, + }, ); } } else { diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index edc5becdb..887d9a2d5 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -30,8 +30,8 @@ mod test; #[derive(Clone, Debug)] pub struct FunInstance { - pub body: Vec>>, - pub calls: HashMap, + pub body: Arc>>>, + pub calls: Arc>, pub subst: HashMap, pub unifier_id: usize, }