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:
Michael Schimmel
2026-02-17 13:00:25 +01:00
parent ce166f39e3
commit d55422272b
9 changed files with 112 additions and 148 deletions
+8 -6
View File
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::rc::Rc;
use std::cell::RefCell;
use crate::ast::nodes::{Node, UntypedKind};
use crate::ast::compiler::bound_nodes::{BoundKind, Address};
use crate::ast::types::{Identity, StaticType};
@@ -61,11 +62,12 @@ impl FunctionCompiler {
pub struct Binder {
functions: Vec<FunctionCompiler>,
globals: Arc<Mutex<HashMap<String, (u32, StaticType)>>>,
// Globals mapping: Name -> (Index, Type)
globals: Rc<RefCell<HashMap<String, (u32, StaticType)>>>,
}
impl Binder {
pub fn new(globals: Arc<Mutex<HashMap<String, (u32, StaticType)>>>) -> Self {
pub fn new(globals: Rc<RefCell<HashMap<String, (u32, StaticType)>>>) -> Self {
let mut binder = Self {
functions: Vec::new(),
globals,
@@ -121,7 +123,7 @@ impl Binder {
let ty = val_node.ty.clone();
if self.functions.len() == 1 {
let mut globals = self.globals.lock().unwrap();
let mut globals = self.globals.borrow_mut();
let idx = if let Some((idx, _)) = globals.get(name.as_ref()) {
*idx
} else {
@@ -191,7 +193,7 @@ impl Binder {
Ok(self.make_node(node.identity.clone(), BoundKind::Lambda {
param_count: params.len() as u32,
upvalues,
body: Arc::new(body_bound),
body: Rc::new(body_bound),
}, fn_ty))
},
@@ -256,7 +258,7 @@ impl Binder {
}
// 3. Try Global
let globals = self.globals.lock().unwrap();
let globals = self.globals.borrow();
if let Some((idx, ty)) = globals.get(name) {
return Ok((Address::Global(*idx), ty.clone()));
}
+2 -2
View File
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::rc::Rc;
use crate::ast::types::{Value, StaticType};
use crate::ast::nodes::Node;
@@ -39,7 +39,7 @@ pub enum BoundKind {
param_count: u32,
// The list of variables captured from enclosing scopes
upvalues: Vec<Address>,
body: Arc<Node<BoundKind, StaticType>>,
body: Rc<Node<BoundKind, StaticType>>,
},
Call {