Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions rust/crates/clj-eval/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,32 @@ impl Env {
self.len() == 0
}

/// Read a slot reference without cloning (#226: for borrow-path
/// evaluation and fast integer ops). Returns `None` for
/// `Patchable` frames or out-of-bounds indices.
#[must_use]
pub fn get_ref(&self, idx: usize) -> Option<&Value> {
let mut frame: &Frame = &self.frame;
while idx < frame.base {
frame = &frame
.parent
.as_ref()
.expect("parentless frame with base > 0")
.frame;
}
match &frame.slots {
Slots::Shared(slots) => slots.get(idx - frame.base),
Slots::Inline(value) => {
if idx == frame.base {
Some(value)
} else {
None
}
}
Slots::Patchable(_) => None,
}
}

/// Read a slot (cloned; missing slots read `nil`, unreachable
/// by construction). Shared frames read lock-free; only
/// `Patchable` frames lock.
Expand Down Expand Up @@ -1755,34 +1781,45 @@ mod tests {
assert!(env.is_empty());
assert_eq!(env.len(), 0);
assert_eq!(env.get(0), Value::Nil);
assert_eq!(env.get_ref(0), None);
assert_eq!(env.get(99), Value::Nil);
assert_eq!(env.get_ref(99), None);
assert_eq!(Env::default().len(), 0);

// Shared extension: absolute indexing, receiver untouched.
let e1 = env.extend(vec![Value::Int(1), Value::Int(2)]);
assert_eq!(e1.len(), 2);
assert_eq!(e1.get(0), Value::Int(1));
assert_eq!(e1.get_ref(0), Some(&Value::Int(1)));
assert_eq!(e1.get(1), Value::Int(2));
assert_eq!(e1.get_ref(1), Some(&Value::Int(2)));
assert_eq!(e1.get(2), Value::Nil);
assert_eq!(e1.get_ref(2), None);
assert_eq!(env.len(), 0);

// `extend_one` chains; reads fall through to the parent.
let e2 = e1.extend_one(Value::Int(3));
assert_eq!(e2.len(), 3);
assert_eq!(e2.get(0), Value::Int(1));
assert_eq!(e2.get_ref(0), Some(&Value::Int(1)));
assert_eq!(e2.get(2), Value::Int(3));
assert_eq!(e2.get_ref(2), Some(&Value::Int(3)));
assert_eq!(e2.get(9), Value::Nil);
assert_eq!(e2.get_ref(9), None);

// Empty extension keeps the base==len corner sound.
let e_empty = e1.extend(vec![]);
assert_eq!(e_empty.len(), 2);
assert_eq!(e_empty.get(0), Value::Int(1));
assert_eq!(e_empty.get_ref(0), Some(&Value::Int(1)));
assert_eq!(e_empty.get(2), Value::Nil);
assert_eq!(e_empty.get_ref(2), None);

// Patchable frames: `set` is visible through the shared Arc.
let p = e1.extend_patchable(2);
assert_eq!(p.len(), 4);
assert_eq!(p.get(2), Value::Nil);
assert_eq!(p.get_ref(2), None);
p.set(2, Value::Int(20));
p.set(3, Value::Int(30));
let p_alias = p.clone();
Expand Down
31 changes: 31 additions & 0 deletions rust/crates/clj-eval/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,13 +1994,39 @@ impl Evaluator {
out
}

/// Read an integer operand without cloning a [`Value`] (#226:
/// local reads and integer literals evaluate directly as `i64`).
fn eval_as_i64(&self, expr: &Expr, env: &Env) -> Option<i64> {
match expr {
Expr::Literal(Value::Int(n)) => Some(*n),
Expr::Local(idx) => match env.get_ref(*idx) {
Some(Value::Int(n)) => Some(*n),
_ => None,
},
_ => None,
}
}

/// A validated `inc`/`dec` call: the `Int` step computes
/// inline, anything else (non-`Int`, overflow, arity) runs
/// the genuine native.
fn call_direct_arith1(&mut self, call: &DirectCall, env: &Env) -> Result<Value, Signal> {
let [arg] = call.args.as_slice() else {
return self.call_direct_general(call, env);
};
if let Some(x) = self.eval_as_i64(arg, env) {
// Dispatch routes only `Inc`/`Dec` here, so the `else`
// is `Dec` — kept as `if/else` (not `match`) so no
// unreachable arm can trip the 100% line-coverage floor.
let res = if call.fast == FastArith::Inc {
x.checked_add(1)
} else {
x.checked_sub(1)
};
if let Some(y) = res {
return Ok(Value::Int(y));
}
}
let value = self.eval(arg, env)?;
match (call.fast, &value) {
(FastArith::Inc, Value::Int(x)) => match x.checked_add(1) {
Expand All @@ -2022,6 +2048,11 @@ impl Evaluator {
let [lhs, rhs] = call.args.as_slice() else {
return self.call_direct_general(call, env);
};
if let (Some(x), Some(y)) = (self.eval_as_i64(lhs, env), self.eval_as_i64(rhs, env)) {
if let Some(v) = Self::fast_int2(call.fast, x, y) {
return Ok(v);
}
}
let a = self.eval(lhs, env)?;
let b = self.eval(rhs, env)?;
if let (Value::Int(x), Value::Int(y)) = (&a, &b) {
Expand Down
Loading
Loading