fix performance issue by using reference counting for objects
This commit is contained in:
18
src/lib.rs
18
src/lib.rs
@@ -13,6 +13,8 @@ use std::fmt::Display;
|
||||
use std::io::BufRead;
|
||||
use std::fmt;
|
||||
use std::iter::Peekable;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Type {
|
||||
@@ -101,13 +103,13 @@ enum Cache {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Object {
|
||||
locals: HashMap<String, Object>,
|
||||
globals: HashMap<String, Object>,
|
||||
locals: HashMap<String, Rc<RefCell<Object>>>,
|
||||
globals: HashMap<String, Rc<RefCell<Object>>>,
|
||||
value: Cache,
|
||||
}
|
||||
|
||||
impl Object {
|
||||
pub fn variable(tree: ParseTree, globals: HashMap<String, Object>, locals: HashMap<String, Object>) -> Self {
|
||||
pub fn variable(tree: ParseTree, globals: HashMap<String, Rc<RefCell<Object>>>, locals: HashMap<String, Rc<RefCell<Object>>>) -> Self {
|
||||
Self {
|
||||
locals,
|
||||
globals,
|
||||
@@ -115,7 +117,7 @@ impl Object {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value(v: Value, globals: HashMap<String, Object>, locals: HashMap<String, Object>) -> Self {
|
||||
pub fn value(v: Value, globals: HashMap<String, Rc<RefCell<Object>>>, locals: HashMap<String, Rc<RefCell<Object>>>) -> Self {
|
||||
Self {
|
||||
locals,
|
||||
globals,
|
||||
@@ -123,7 +125,7 @@ impl Object {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn function(func: Function, globals: HashMap<String, Object>, locals: HashMap<String, Object>) -> Self {
|
||||
pub fn function(func: Function, globals: HashMap<String, Rc<RefCell<Object>>>, locals: HashMap<String, Rc<RefCell<Object>>>) -> Self {
|
||||
Self {
|
||||
locals,
|
||||
globals,
|
||||
@@ -150,11 +152,11 @@ impl Object {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn locals(&self) -> HashMap<String, Object> {
|
||||
pub fn locals(&self) -> HashMap<String, Rc<RefCell<Object>>> {
|
||||
self.locals.clone()
|
||||
}
|
||||
|
||||
pub fn globals(&self) -> HashMap<String, Object> {
|
||||
pub fn globals(&self) -> HashMap<String, Rc<RefCell<Object>>> {
|
||||
self.globals.clone()
|
||||
}
|
||||
}
|
||||
@@ -162,7 +164,7 @@ impl Object {
|
||||
pub struct Runtime<'a, R: BufRead> {
|
||||
tokenizer: Peekable<Tokenizer<R>>,
|
||||
global_types: HashMap<String, Type>,
|
||||
globals: HashMap<String, Object>,
|
||||
globals: HashMap<String, Rc<RefCell<Object>>>,
|
||||
parser: Option<Parser<'a, Tokenizer<R>>>,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user