From 6633eabb897a2b107c9be664ee1467ebeefad6ca Mon Sep 17 00:00:00 2001 From: pca006132 Date: Fri, 27 Aug 2021 12:36:51 +0800 Subject: [PATCH] nac3core: optimized by using HashSet --- nac3core/src/typecheck/function_check.rs | 22 +++++++++---------- nac3core/src/typecheck/type_inferencer/mod.rs | 10 ++++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/nac3core/src/typecheck/function_check.rs b/nac3core/src/typecheck/function_check.rs index cf7956de..03306615 100644 --- a/nac3core/src/typecheck/function_check.rs +++ b/nac3core/src/typecheck/function_check.rs @@ -3,18 +3,18 @@ use crate::typecheck::typedef::TypeEnum; use super::type_inferencer::Inferencer; use super::typedef::Type; use rustpython_parser::ast::{self, Expr, ExprKind, Stmt, StmtKind}; -use std::iter::once; +use std::{collections::HashSet, iter::once}; impl<'a> Inferencer<'a> { fn check_pattern( &mut self, pattern: &Expr>, - defined_identifiers: &mut Vec, + defined_identifiers: &mut HashSet, ) -> Result<(), String> { match &pattern.node { ExprKind::Name { id, .. } => { if !defined_identifiers.contains(id) { - defined_identifiers.push(id.clone()); + defined_identifiers.insert(id.clone()); } Ok(()) } @@ -42,7 +42,7 @@ impl<'a> Inferencer<'a> { fn check_expr( &mut self, expr: &Expr>, - defined_identifiers: &mut Vec, + defined_identifiers: &mut HashSet, ) -> Result<(), String> { // there are some cases where the custom field is None if let Some(ty) = &expr.custom { @@ -58,7 +58,7 @@ impl<'a> Inferencer<'a> { ExprKind::Name { id, .. } => { if !defined_identifiers.contains(id) { if self.function_data.resolver.get_identifier_def(id).is_some() { - defined_identifiers.push(id.clone()); + defined_identifiers.insert(id.clone()); } else { return Err(format!( "unknown identifier {} (use before def?) at {}", @@ -104,10 +104,10 @@ impl<'a> Inferencer<'a> { } } ExprKind::Lambda { args, body } => { - let mut defined_identifiers = defined_identifiers.to_vec(); + let mut defined_identifiers = defined_identifiers.clone(); for arg in args.args.iter() { if !defined_identifiers.contains(&arg.node.arg) { - defined_identifiers.push(arg.node.arg.clone()); + defined_identifiers.insert(arg.node.arg.clone()); } } self.check_expr(body, &mut defined_identifiers)?; @@ -116,7 +116,7 @@ impl<'a> Inferencer<'a> { // in our type inference stage, we already make sure that there is only 1 generator let ast::Comprehension { target, iter, ifs, .. } = &generators[0]; self.check_expr(iter, defined_identifiers)?; - let mut defined_identifiers = defined_identifiers.to_vec(); + let mut defined_identifiers = defined_identifiers.clone(); self.check_pattern(target, &mut defined_identifiers)?; for term in once(elt.as_ref()).chain(ifs.iter()) { self.check_expr(term, &mut defined_identifiers)?; @@ -143,7 +143,7 @@ impl<'a> Inferencer<'a> { fn check_stmt( &mut self, stmt: &Stmt>, - defined_identifiers: &mut Vec, + defined_identifiers: &mut HashSet, ) -> Result { match &stmt.node { StmtKind::For { target, iter, body, orelse, .. } => { @@ -167,7 +167,7 @@ impl<'a> Inferencer<'a> { for ident in body_identifiers.iter() { if !defined_identifiers.contains(ident) && orelse_identifiers.contains(ident) { - defined_identifiers.push(ident.clone()) + defined_identifiers.insert(ident.clone()); } } Ok(body_returned && orelse_returned) @@ -217,7 +217,7 @@ impl<'a> Inferencer<'a> { pub fn check_block( &mut self, block: &[Stmt>], - defined_identifiers: &mut Vec, + defined_identifiers: &mut HashSet, ) -> Result { let mut ret = false; for stmt in block { diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index e1e4d3f2..c5c3134e 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::convert::{From, TryInto}; use std::iter::once; use std::{cell::RefCell, sync::Arc}; @@ -45,7 +45,7 @@ pub struct FunctionData { pub struct Inferencer<'a> { pub top_level: &'a TopLevelContext, - pub defined_identifiers: Vec, + pub defined_identifiers: HashSet, pub function_data: &'a mut FunctionData, pub unifier: &'a mut Unifier, pub primitives: &'a PrimitiveStore, @@ -161,7 +161,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> { ast::ExprKind::Name { id, .. } => { if !self.defined_identifiers.contains(id) { if self.function_data.resolver.get_identifier_def(id.as_str()).is_some() { - self.defined_identifiers.push(id.clone()); + self.defined_identifiers.insert(id.clone()); } else { return Err(format!( "unknown identifier {} (use before def?) at {}", @@ -215,7 +215,7 @@ impl<'a> Inferencer<'a> { match &pattern.node { ExprKind::Name { id, .. } => { if !self.defined_identifiers.contains(id) { - self.defined_identifiers.push(id.clone()); + self.defined_identifiers.insert(id.clone()); } Ok(()) } @@ -274,7 +274,7 @@ impl<'a> Inferencer<'a> { for arg in args.args.iter() { let name = &arg.node.arg; if !defined_identifiers.contains(name) { - defined_identifiers.push(name.clone()); + defined_identifiers.insert(name.clone()); } } let fn_args: Vec<_> = args