Skip to content

Commit

Permalink
decrease some node cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Nov 25, 2024
1 parent 45a11b6 commit 799265e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pathdiff = "0.2.1"
rand = {version = "0.8.5", features = ["small_rng"]}
rayon = "1.9.0"
regex = "1.10.3"
serde = {version = "1", features = ["derive"]}
serde = {version = "1", features = ["derive", "rc"]}
serde_json = "1"
serde_tuple = "0.5.0"
thread_local = "1"
Expand Down
10 changes: 5 additions & 5 deletions src/compile/invert/un.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{check::nodes_sig, compile::algebra::algebraic_inverse};
use crate::{check::nodes_sig, compile::algebra::algebraic_inverse, CustomInverse};

use super::*;

Expand Down Expand Up @@ -664,23 +664,23 @@ inverse!(AntiJoinPat, input, _, {
});

inverse!(CustomPat, input, _, ref, CustomInverse(cust, span), {
let mut cust = cust.clone();
let mut cust = CustomInverse::clone(cust);
let un = cust.un.take().ok_or(Generic)?;
cust.un = cust.normal.ok();
cust.normal = Ok(un);
cust.anti = None;
cust.under = None;
Ok((input, CustomInverse(cust, *span)))
Ok((input, CustomInverse(cust.into(), *span)))
});

inverse!(AntiCustomPat, input, _, ref, CustomInverse(cust, span), {
let mut cust = cust.clone();
let mut cust = CustomInverse::clone(cust);
let anti = cust.anti.take().ok_or(Generic)?;
cust.anti = cust.normal.ok();
cust.normal = Ok(anti);
cust.un = None;
cust.under = None;
Ok((input, CustomInverse(cust, *span)))
Ok((input, CustomInverse(cust.into(), *span)))
});

inverse!(FormatPat, input, _, ref, Format(parts, span), {
Expand Down
1 change: 1 addition & 0 deletions src/compile/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl Node {
}
}
CustomInverse(cust, _) => {
let cust = Arc::make_mut(cust);
if let Ok(normal) = cust.normal.as_mut() {
normal.node.optimize();
}
Expand Down
10 changes: 5 additions & 5 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,10 @@ at {}",
boxed,
span,
..
} => self.with_span(span, |env| env.make_array(len, *inner, boxed)),
} => self.with_span(span, |env| env.make_array(len, inner.into(), boxed)),
Node::Call(f, span) => self.call_with_span(&f, span),
Node::CustomInverse(cust, span) => match cust.normal {
Ok(normal) => self.exec_with_span(normal, span),
Node::CustomInverse(cust, span) => match &cust.normal {
Ok(normal) => self.exec_with_span(normal.clone(), span),
Err(e) => self.with_span(span, |env| Err(env.error(e))),
},
Node::Switch {
Expand Down Expand Up @@ -702,10 +702,10 @@ at {}",
env.rt.stack.extend(env.rt.under_stack.drain(start..).rev());
Ok(())
}),
Node::NoInline(inner) => self.exec(*inner),
Node::NoInline(inner) => self.exec(inner),
Node::TrackCaller(inner) => {
self.rt.call_stack.last_mut().unwrap().track_caller = true;
self.exec(*inner)
self.exec(inner)
}
};
if self.rt.time_instrs {
Expand Down
15 changes: 11 additions & 4 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
mem::{discriminant, swap, take},
ops::Deref,
slice::{self, SliceIndex},
sync::Arc,
};

use ecow::{eco_vec, EcoString, EcoVec};
Expand All @@ -19,15 +20,15 @@ use crate::{
};

node!(
Array { len: ArrayLen, inner: Box<Node>, boxed: bool, prim: Option<Primitive>, span: usize },
Array { len: ArrayLen, inner: Arc<Node>, boxed: bool, prim: Option<Primitive>, span: usize },
CallGlobal(index(usize), sig(Signature)),
CallMacro { index: usize, sig: Signature, span: usize },
BindGlobal { index: usize, span: usize },
Label(label(EcoString), span(usize)),
RemoveLabel(label(Option<EcoString>), span(usize)),
Format(parts(EcoVec<EcoString>), span(usize)),
MatchFormatPattern(parts(EcoVec<EcoString>), span(usize)),
CustomInverse(cust(Box<CustomInverse>), span(usize)),
CustomInverse(cust(Arc<CustomInverse>), span(usize)),
Switch { branches: Ops, sig: Signature, under_cond: bool, span: usize },
Unpack { count: usize, unbox: bool, prim: Option<Primitive>, span: usize },
SetOutputComment { i: usize, n: usize },
Expand All @@ -36,8 +37,8 @@ node!(
PushUnder(n(usize), span(usize)),
CopyToUnder(n(usize), span(usize)),
PopUnder(n(usize), span(usize)),
NoInline(inner(Box<Node>)),
TrackCaller(inner(Box<Node>)),
NoInline(inner(Arc<Node>)),
TrackCaller(inner(Arc<Node>)),
(#[serde(untagged)] rep),
Push(val(Value)),
(#[serde(untagged)] rep),
Expand Down Expand Up @@ -116,6 +117,12 @@ impl From<SigNode> for Node {
}
}

impl From<Arc<Node>> for Node {
fn from(node: Arc<Node>) -> Self {
Arc::unwrap_or_clone(node)
}
}

impl Serialize for SigNode {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
(self.sig.args, self.sig.outputs, &self.node).serialize(serializer)
Expand Down

0 comments on commit 799265e

Please sign in to comment.