difftreelog
refactor turn Thunk structure around to reduce allocations
in: master
9 files changed
.gitignorediffbeforeafterboth--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,10 @@
.vscode
.direnv
+# Nix artifacts
+/result
+/result-*
+
cache
jsonnet-cpp
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -7,7 +7,7 @@
use super::ArrValue;
use crate::{
error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, typed::Typed,
- Context, Error, ObjValue, Result, Thunk, Val,
+ val::ThunkValue, Context, Error, ObjValue, Result, Thunk, Val,
};
pub trait ArrayLike: Any + Trace + Debug {
@@ -191,9 +191,25 @@
ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
};
- let arr_thunk = self.clone();
- Some(Thunk!(move || {
- arr_thunk.get(index).transpose().expect("index checked")
+ #[derive(Trace)]
+ struct ExprArrThunk {
+ expr: ExprArray,
+ index: usize,
+ }
+ impl ThunkValue for ExprArrThunk {
+ type Output = Val;
+
+ fn get(&self) -> Result<Self::Output> {
+ self.expr
+ .get(self.index)
+ .transpose()
+ .expect("index checked")
+ }
+ }
+
+ Some(Thunk::new(ExprArrThunk {
+ expr: self.clone(),
+ index,
}))
}
fn get_cheap(&self, _index: usize) -> Option<Val> {
@@ -484,9 +500,22 @@
ArrayThunk::Waiting(()) | ArrayThunk::Pending => {}
};
- let arr_thunk = self.clone();
- Some(Thunk!(move || {
- arr_thunk.get(index).transpose().expect("index checked")
+ #[derive(Trace)]
+ struct MappedArrayThunk<const WITH_INDEX: bool> {
+ arr: MappedArray<WITH_INDEX>,
+ index: usize,
+ }
+ impl<const WITH_INDEX: bool> ThunkValue for MappedArrayThunk<WITH_INDEX> {
+ type Output = Val;
+
+ fn get(&self) -> Result<Self::Output> {
+ self.arr.get(self.index).transpose().expect("index checked")
+ }
+ }
+
+ Some(Thunk::new(MappedArrayThunk {
+ arr: self.clone(),
+ index,
}))
}
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -40,8 +40,9 @@
impl<T: Trace + Clone> ThunkValue for Pending<T> {
type Output = T;
- fn get(self: Box<Self>) -> Result<Self::Output> {
+ fn get(&self) -> Result<Self::Output> {
let Some(value) = self.0.get() else {
+ // TODO: Other error?
bail!(InfiniteRecursionDetected);
};
Ok(value.clone())
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -23,7 +23,7 @@
gc::WithCapacityExt as _,
identity_hash, in_frame,
operator::evaluate_add_op,
- val::ArrValue,
+ val::{ArrValue, ThunkValue},
CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,
};
@@ -753,13 +753,45 @@
if !self.has_field_ex(key.clone(), true) {
return None;
}
- let obj = self.clone();
+ #[derive(Trace)]
+ struct ObjFieldThunk {
+ obj: ObjValue,
+ key: IStr,
+ }
+ impl ThunkValue for ObjFieldThunk {
+ type Output = Val;
- Some(Thunk!(move || Ok(obj.get(key)?.expect("field exists"))))
+ fn get(&self) -> Result<Self::Output> {
+ self.obj
+ .get(self.key.clone())
+ .transpose()
+ .expect("field existence checked")
+ }
+ }
+
+ Some(Thunk::new(ObjFieldThunk {
+ obj: self.clone(),
+ key,
+ }))
}
pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {
- let obj = self.clone();
- Thunk!(move || obj.get_or_bail(key))
+ #[derive(Trace)]
+ struct ObjFieldThunk {
+ obj: ObjValue,
+ key: IStr,
+ }
+ impl ThunkValue for ObjFieldThunk {
+ type Output = Val;
+
+ fn get(&self) -> Result<Self::Output> {
+ self.obj.get_or_bail(self.key.clone())
+ }
+ }
+
+ Thunk::new(ObjFieldThunk {
+ obj: self.clone(),
+ key,
+ })
}
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
Cc::ptr_eq(&a.0, &b.0)
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -2,13 +2,14 @@
cell::RefCell,
cmp::Ordering,
fmt::{self, Debug, Display},
+ marker::PhantomData,
mem::replace,
num::NonZeroU32,
ops::Deref,
rc::Rc,
};
-use jrsonnet_gcmodule::{Acyclic, Cc, Trace, TraceBox};
+use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace};
use jrsonnet_interner::IStr;
pub use jrsonnet_macros::Thunk;
use jrsonnet_types::ValType;
@@ -28,56 +29,106 @@
pub trait ThunkValue: Trace {
type Output;
- fn get(self: Box<Self>) -> Result<Self::Output>;
+ fn get(&self) -> Result<Self::Output>;
}
#[derive(Trace)]
-pub struct ThunkValueClosure<D: Trace, O: 'static> {
- env: D,
- // Carries no data, as it is not a real closure, all the
- // captured environment is stored in `env` field.
- #[trace(skip)]
- closure: fn(D) -> Result<O>,
+enum MemoizedClusureThunkInner<D: Trace, T: Trace> {
+ Computed(T),
+ Errored(Error),
+ Waiting {
+ env: D,
+ // Carries no data, as it is not a real closure, all the
+ // captured environment is stored in `env` field.
+ #[trace(skip)]
+ closure: fn(D) -> Result<T>,
+ },
+ Pending,
}
-impl<D: Trace, O: 'static> ThunkValueClosure<D, O> {
- pub fn new(env: D, closure: fn(D) -> Result<O>) -> Self {
- Self { env, closure }
+#[derive(Trace)]
+pub struct MemoizedClosureThunk<D: Trace, T: Trace>(RefCell<MemoizedClusureThunkInner<D, T>>);
+impl<D: Trace, T: Trace> MemoizedClosureThunk<D, T> {
+ pub fn new(env: D, closure: fn(D) -> Result<T>) -> Self {
+ Self(RefCell::new(MemoizedClusureThunkInner::Waiting {
+ env,
+ closure,
+ }))
}
}
-impl<D: Trace, O: 'static> ThunkValue for ThunkValueClosure<D, O> {
- type Output = O;
- fn get(self: Box<Self>) -> Result<Self::Output> {
- (self.closure)(self.env)
- }
-}
+impl<D: Trace, T: Trace + Clone> ThunkValue for MemoizedClosureThunk<D, T> {
+ type Output = T;
-#[derive(Trace)]
-enum ThunkInner<T: Trace> {
- Computed(T),
- Errored(Error),
- Waiting(TraceBox<dyn ThunkValue<Output = T>>),
- Pending,
+ fn get(&self) -> Result<Self::Output> {
+ match &*self.0.borrow() {
+ MemoizedClusureThunkInner::Computed(v) => return Ok(v.clone()),
+ MemoizedClusureThunkInner::Errored(e) => return Err(e.clone()),
+ MemoizedClusureThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),
+ MemoizedClusureThunkInner::Waiting { .. } => (),
+ };
+ let MemoizedClusureThunkInner::Waiting { env, closure } = replace(
+ &mut *self.0.borrow_mut(),
+ MemoizedClusureThunkInner::Pending,
+ ) else {
+ unreachable!();
+ };
+ let new_value = match closure(env) {
+ Ok(v) => v,
+ Err(e) => {
+ *self.0.borrow_mut() = MemoizedClusureThunkInner::Errored(e.clone());
+ return Err(e);
+ }
+ };
+ *self.0.borrow_mut() = MemoizedClusureThunkInner::Computed(new_value.clone());
+ Ok(new_value)
+ }
}
-/// Lazily evaluated value
-#[allow(clippy::module_name_repetitions)]
-#[derive(Clone, Trace)]
-pub struct Thunk<T: Trace>(Cc<RefCell<ThunkInner<T>>>);
+cc_dyn!(
+ /// Lazily evaluated value
+ #[derive(Clone)] Thunk<V: Trace>,
+ ThunkValue<Output = V>,
+ pub fn new() {...}
+);
impl<T: Trace> Thunk<T> {
- pub fn evaluated(val: T) -> Self {
- Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))
- }
- pub fn new(f: impl ThunkValue<Output = T> + 'static) -> Self {
- Self(Cc::new(RefCell::new(ThunkInner::Waiting(TraceBox(
- Box::new(f),
- )))))
+ pub fn evaluated(val: T) -> Self
+ where
+ T: Clone,
+ {
+ #[derive(Trace)]
+ struct EvaluatedThunk<T: Trace>(T);
+ impl<T> ThunkValue for EvaluatedThunk<T>
+ where
+ T: Clone + Trace,
+ {
+ type Output = T;
+
+ fn get(&self) -> Result<Self::Output> {
+ Ok(self.0.clone())
+ }
+ }
+ Self::new(EvaluatedThunk(val))
}
pub fn errored(e: Error) -> Self {
- Self(Cc::new(RefCell::new(ThunkInner::Errored(e))))
+ #[derive(Trace)]
+ struct ErroredThunk<T: Trace>(Error, PhantomData<T>);
+ impl<T> ThunkValue for ErroredThunk<T>
+ where
+ T: Trace,
+ {
+ type Output = T;
+
+ fn get(&self) -> Result<Self::Output> {
+ Err(self.0.clone())
+ }
+ }
+ Self::new(ErroredThunk(e, PhantomData))
}
- pub fn result(res: Result<T, Error>) -> Self {
+ pub fn result(res: Result<T, Error>) -> Self
+ where
+ T: Clone,
+ {
match res {
Ok(o) => Self::evaluated(o),
Err(e) => Self::errored(e),
@@ -101,25 +152,7 @@
/// - Lazy value evaluation returned error
/// - This method was called during inner value evaluation
pub fn evaluate(&self) -> Result<T> {
- match &*self.0.borrow() {
- ThunkInner::Computed(v) => return Ok(v.clone()),
- ThunkInner::Errored(e) => return Err(e.clone()),
- ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),
- ThunkInner::Waiting(..) => (),
- };
- let ThunkInner::Waiting(value) = replace(&mut *self.0.borrow_mut(), ThunkInner::Pending)
- else {
- unreachable!();
- };
- let new_value = match value.0.get() {
- Ok(v) => v,
- Err(e) => {
- *self.0.borrow_mut() = ThunkInner::Errored(e.clone());
- return Err(e);
- }
- };
- *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());
- Ok(new_value)
+ self.0.get()
}
}
@@ -134,7 +167,7 @@
pub fn map<M>(self, mapper: M) -> Thunk<M::Output>
where
M: ThunkMapper<Input>,
- M::Output: Trace,
+ M::Output: Trace + Clone,
{
let inner = self;
Thunk!(move || {
@@ -145,7 +178,7 @@
}
}
-impl<T: Trace> From<Result<T>> for Thunk<T> {
+impl<T: Trace + Clone> From<Result<T>> for Thunk<T> {
fn from(value: Result<T>) -> Self {
match value {
Ok(o) => Self::evaluated(o),
@@ -162,7 +195,7 @@
}
}
-impl<T: Trace + Default> Default for Thunk<T> {
+impl<T: Trace + Default + Clone> Default for Thunk<T> {
fn default() -> Self {
Self::evaluated(T::default())
}
crates/jrsonnet-formatter/src/comments.rsdiffbeforeafterboth1use std::string::String;23use dprint_core::formatting::PrintItems;4use jrsonnet_rowan_parser::{nodes::TriviaKind, AstToken};56use crate::{children::ChildTrivia, p, pi};78pub enum CommentLocation {9 /// Above local, field, other things10 AboveItem,11 /// After item12 ItemInline,13 /// After all items in object14 EndOfItems,15}1617#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]18pub fn format_comments(comments: &ChildTrivia, loc: CommentLocation, out: &mut PrintItems) {19 for c in comments {20 let Ok(c) = c else {21 let mut text = c.as_ref().unwrap_err() as &str;22 while !text.is_empty() {23 let pos = text.find(['\n', '\t']).unwrap_or(text.len());24 let sliced = &text[..pos];25 p!(out, string(sliced.to_string()));26 text = &text[pos..];27 if !text.is_empty() {28 match text.as_bytes()[0] {29 b'\n' => p!(out, nl),30 b'\t' => p!(out, tab),31 _ => unreachable!(),32 }33 text = &text[1..];34 }35 }36 continue;37 };38 match c.kind() {39 TriviaKind::Whitespace => {}40 TriviaKind::MultiLineComment => {41 let mut text = c42 .text()43 .strip_prefix("/*")44 .expect("ml comment starts with /*")45 .strip_suffix("*/")46 .expect("ml comment ends with */");47 // doc-style comment, /**48 let doc = if text.starts_with('*') {49 text = &text[1..];50 true51 } else {52 false53 };54 // Is comment starts with text immediatly, i.e /*text55 let mut immediate_start = true;56 let mut lines = text57 .split('\n')58 .map(|l| l.trim_end().to_string())59 .skip_while(|l| {60 if l.is_empty() {61 immediate_start = false;62 true63 } else {64 false65 }66 })67 .collect::<Vec<_>>();68 while lines.last().is_some_and(String::is_empty) {69 lines.pop();70 }71 if lines.len() == 1 && !doc {72 if matches!(loc, CommentLocation::ItemInline) {73 p!(out, str(" "));74 }75 p!(out, str("/* ") string(lines[0].trim().to_string()) str(" */"));76 if matches!(77 loc,78 CommentLocation::AboveItem | CommentLocation::EndOfItems79 ) {80 p!(out, nl);81 }82 } else if !lines.is_empty() {83 fn common_ws_prefix<'a>(a: &'a str, b: &str) -> &'a str {84 let offset = a85 .bytes()86 .zip(b.bytes())87 .take_while(|(a, b)| a == b && (a.is_ascii_whitespace() || *a == b'*'))88 .count();89 &a[..offset]90 }91 // First line is not empty, extract ws prefix of it92 let mut common_ws_padding = (if immediate_start && lines.len() > 1 {93 common_ws_prefix(&lines[1], &lines[1])94 } else {95 common_ws_prefix(&lines[0], &lines[0])96 })97 .to_string();98 for line in lines99 .iter()100 .skip(if immediate_start { 2 } else { 1 })101 .filter(|l| !l.is_empty())102 {103 common_ws_padding = common_ws_prefix(&common_ws_padding, line).to_string();104 }105 for line in lines106 .iter_mut()107 .skip(usize::from(immediate_start))108 .filter(|l| !l.is_empty())109 {110 *line = line111 .strip_prefix(&common_ws_padding)112 .expect("all non-empty lines start with this padding")113 .to_string();114 }115116 p!(out, str("/*"));117 if doc {118 p!(out, str("*"));119 }120 p!(out, nl);121 for mut line in lines {122 if doc {123 p!(out, str(" *"));124 }125 if line.is_empty() {126 p!(out, nl);127 } else {128 if doc {129 p!(out, str(" "));130 }131 while let Some(new_line) = line.strip_prefix('\t') {132 if doc {133 p!(out, str(" "));134 } else {135 p!(out, tab);136 }137 line = new_line.to_string();138 }139 p!(out, string(line.to_string()) nl);140 }141 }142 if doc {143 p!(out, str(" "));144 }145 p!(out, str("*/") nl);146 }147 }148 // TODO: Keep common padding for multiple continous lines of single-line comments149 // I.e150 // ```151 // # Line1152 // # Line2153 // ```154 // Should be reformatted as155 // ```156 // # Line1157 // # Line2158 // ```159 // But currently comment formatter is not aware of continous comment lines, and reformats it as160 // ```161 // # Line1162 // # Line2163 // ```164 TriviaKind::SingleLineHashComment => {165 if matches!(loc, CommentLocation::ItemInline) {166 p!(out, str(" "));167 }168 p!(out, str("# ") string(c.text().strip_prefix('#').expect("hash comment starts with #").trim().to_string()));169 if !matches!(loc, CommentLocation::ItemInline) {170 p!(out, nl);171 }172 }173 TriviaKind::SingleLineSlashComment => {174 if matches!(loc, CommentLocation::ItemInline) {175 p!(out, str(" "));176 }177 p!(out, str("// ") string(c.text().strip_prefix("//").expect("comment starts with //").trim().to_string()));178 if !matches!(loc, CommentLocation::ItemInline) {179 p!(out, nl);180 }181 }182 // Garbage in - garbage out183 TriviaKind::ErrorCommentTooShort => p!(out, str("/*/")),184 TriviaKind::ErrorCommentUnterminated => p!(out, string(c.text().to_string())),185 }186 }187}crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -879,6 +879,6 @@
quote! {{
#move_check
#(#trace_check)*
- ::jrsonnet_evaluator::Thunk::new(::jrsonnet_evaluator::val::ThunkValueClosure::new(#env, #closure))
+ ::jrsonnet_evaluator::Thunk::new(::jrsonnet_evaluator::val::MemoizedClosureThunk::new(#env, #closure))
}}.into()
}
resultdiffbeforeafterboth--- a/result
+++ /dev/null
@@ -1 +0,0 @@
-/nix/store/nd6v7jksg1dqhpx4x4vqgy5ry1nkb9lk-jrsonnet-current
\ No newline at end of file
xtask/src/sourcegen/mod.rsdiffbeforeafterboth--- a/xtask/src/sourcegen/mod.rs
+++ b/xtask/src/sourcegen/mod.rs
@@ -203,49 +203,57 @@
});
let mut type_positions: HashMap<String, usize> = HashMap::new();
- let field_positions: Vec<_> = node.fields.iter().map(|field| {
- let ty_str = field.ty().to_string();
- let pos = *type_positions.get(&ty_str).unwrap_or(&0);
- type_positions.insert(ty_str, pos + 1);
- pos
- }).collect();
+ let field_positions: Vec<_> = node
+ .fields
+ .iter()
+ .map(|field| {
+ let ty_str = field.ty().to_string();
+ let pos = *type_positions.get(&ty_str).unwrap_or(&0);
+ type_positions.insert(ty_str, pos + 1);
+ pos
+ })
+ .collect();
- let methods = node.fields.iter().zip(field_positions.iter()).map(|(field, &pos)| {
- let method_name = field.method_name(kinds);
- let ty = field.ty();
+ let methods = node
+ .fields
+ .iter()
+ .zip(field_positions.iter())
+ .map(|(field, &pos)| {
+ let method_name = field.method_name(kinds);
+ let ty = field.ty();
- if field.is_many() {
- quote! {
- pub fn #method_name(&self) -> AstChildren<#ty> {
- support::children(&self.syntax)
+ if field.is_many() {
+ quote! {
+ pub fn #method_name(&self) -> AstChildren<#ty> {
+ support::children(&self.syntax)
+ }
}
- }
- } else if let Some(token_kind) = field.token_kind(kinds) {
- quote! {
- pub fn #method_name(&self) -> Option<#ty> {
- support::token(&self.syntax, #token_kind)
+ } else if let Some(token_kind) = field.token_kind(kinds) {
+ quote! {
+ pub fn #method_name(&self) -> Option<#ty> {
+ support::token(&self.syntax, #token_kind)
+ }
}
- }
- } else if field.is_token_enum(grammar) {
- quote! {
- pub fn #method_name(&self) -> Option<#ty> {
- support::token_child(&self.syntax)
+ } else if field.is_token_enum(grammar) {
+ quote! {
+ pub fn #method_name(&self) -> Option<#ty> {
+ support::token_child(&self.syntax)
+ }
}
- }
- } else if pos == 0 {
- quote! {
- pub fn #method_name(&self) -> Option<#ty> {
- support::children(&self.syntax).next()
+ } else if pos == 0 {
+ quote! {
+ pub fn #method_name(&self) -> Option<#ty> {
+ support::children(&self.syntax).next()
+ }
}
- }
- } else {
- quote! {
- pub fn #method_name(&self) -> Option<#ty> {
- support::children(&self.syntax).nth(#pos)
+ } else {
+ quote! {
+ pub fn #method_name(&self) -> Option<#ty> {
+ support::children(&self.syntax).nth(#pos)
+ }
}
}
- }
- });
+ });
(
quote! {
#[pretty_doc_comment_placeholder_workaround]