Refactor: Use Rc/RefCell for shared mutable state
This commit replaces `Arc<Mutex<T>>` with `Rc<RefCell<T>>` for managing shared mutable state within the AST and VM. This change is primarily an internal refactoring to leverage Rust's standard library more effectively for single-threaded scenarios, improving performance by avoiding the overhead of mutexes. The following types and their usage have been updated: - `Environment.global_names` and `Environment.global_values` - `Binder.globals` - `bound_nodes::BoundKind::Lambda.body` (now `Rc<Node>`) - `ast::types::NodeIdentity` (now `Rc<NodeIdentity>`) - `ast::types::Value::List`, `Value::Record`, `Value::Function`, `Value::Object`, `Value::Cell` - `ast::nodes::Scope` and `ast::nodes::Context` - `ast::vm::Closure.function_node` and `Closure.upvalues` - `ast::vm::VM.globals` This change does not alter the external behavior of the library but streamlines internal data management.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::compiler::binder::Binder;
|
||||
use crate::ast::vm::VM;
|
||||
@@ -74,11 +75,11 @@ mod tests {
|
||||
let mut parser = Parser::new(source).expect("Failed to create parser");
|
||||
let ast = parser.parse_expression().expect("Failed to parse");
|
||||
|
||||
let globals = Arc::new(Mutex::new(std::collections::HashMap::new()));
|
||||
let globals = Rc::new(RefCell::new(std::collections::HashMap::new()));
|
||||
let mut binder = Binder::new(globals.clone());
|
||||
let bound_ast = binder.bind(&ast).expect("Failed to bind");
|
||||
|
||||
let vm_globals = Arc::new(Mutex::new(Vec::new()));
|
||||
let vm_globals = Rc::new(RefCell::new(Vec::new()));
|
||||
let mut vm = VM::new(vm_globals);
|
||||
let result = vm.run(&bound_ast);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user