From 5c236271c308d24744a2cc231f50a5111fad99b7 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 24 Sep 2021 09:58:58 +0800 Subject: [PATCH] nac3embedded: string interning --- nac3embedded/src/lib.rs | 5 +++-- nac3embedded/src/symbol_resolver.rs | 23 ++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/nac3embedded/src/lib.rs b/nac3embedded/src/lib.rs index fbbb66b4..bb2b9f36 100644 --- a/nac3embedded/src/lib.rs +++ b/nac3embedded/src/lib.rs @@ -102,9 +102,10 @@ impl Nac3 { let top_level = self.top_level.as_ref().unwrap(); let instance = { let defs = top_level.definitions.read(); - let class_def = defs[self.resolver.get_identifier_def(&class_name).unwrap().0].write(); + let class_def = defs[self.resolver.get_identifier_def(class_name.into()).unwrap().0].write(); let mut method_def = if let TopLevelDef::Class { methods, .. } = &*class_def { - if let Some((_name, _unification_key, definition_id)) = methods.iter().find(|method| method.0 == method_name) { + println!("{:?}", methods); + if let Some((_name, _unification_key, definition_id)) = methods.iter().find(|method| method.0.to_string() == method_name) { defs[definition_id.0].write() } else { return Err(exceptions::PyValueError::new_err("method not found")); diff --git a/nac3embedded/src/symbol_resolver.rs b/nac3embedded/src/symbol_resolver.rs index f8fe760e..92f0d0c5 100644 --- a/nac3embedded/src/symbol_resolver.rs +++ b/nac3embedded/src/symbol_resolver.rs @@ -8,20 +8,21 @@ use nac3core::{ }, }; use parking_lot::Mutex; +use rustpython_parser::ast::StrRef; use std::{collections::HashMap, sync::Arc}; pub struct ResolverInternal { - pub id_to_type: Mutex>, - pub id_to_def: Mutex>, - pub class_names: Mutex>, + pub id_to_type: Mutex>, + pub id_to_def: Mutex>, + pub class_names: Mutex>, } impl ResolverInternal { - pub fn add_id_def(&self, id: String, def: DefinitionId) { + pub fn add_id_def(&self, id: StrRef, def: DefinitionId) { self.id_to_def.lock().insert(id, def); } - pub fn add_id_type(&self, id: String, ty: Type) { + pub fn add_id_type(&self, id: StrRef, ty: Type) { self.id_to_type.lock().insert(id, ty); } } @@ -29,23 +30,23 @@ impl ResolverInternal { pub struct Resolver(pub Arc); impl SymbolResolver for Resolver { - fn get_symbol_type(&self, _: &mut Unifier, _: &PrimitiveStore, str: &str) -> Option { - let ret = self.0.id_to_type.lock().get(str).cloned(); + fn get_symbol_type(&self, _: &mut Unifier, _: &PrimitiveStore, str: StrRef) -> Option { + let ret = self.0.id_to_type.lock().get(&str).cloned(); if ret.is_none() { // println!("unknown here resolver {}", str); } ret } - fn get_symbol_value(&self, _: &str) -> Option { + fn get_symbol_value(&self, _: StrRef) -> Option { unimplemented!() } - fn get_symbol_location(&self, _: &str) -> Option { + fn get_symbol_location(&self, _: StrRef) -> Option { unimplemented!() } - fn get_identifier_def(&self, id: &str) -> Option { - self.0.id_to_def.lock().get(id).cloned() + fn get_identifier_def(&self, id: StrRef) -> Option { + self.0.id_to_def.lock().get(&id).cloned() } }