git.delta.rocks / jrsonnet / refs/commits / 562e7b71e322

difftreelog

refactor remove manual ThunkValue implementations

Yaroslav Bolyukin2024-08-30parent: #7cd3ab4.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -3,23 +3,11 @@
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{ArgsDesc, LocExpr};
 
-use crate::{evaluate, gc::GcHashMap, typed::Typed, val::ThunkValue, Context, Result, Thunk, Val};
+use crate::{evaluate, gc::GcHashMap, typed::Typed, Context, Result, Thunk, Val};
 
 /// Marker for arguments, which can be evaluated with context set to None
 pub trait OptionalContext {}
 
-#[derive(Trace)]
-struct EvaluateThunk {
-	ctx: Context,
-	expr: LocExpr,
-}
-impl ThunkValue for EvaluateThunk {
-	type Output = Val;
-	fn get(self: Box<Self>) -> Result<Val> {
-		evaluate(self.ctx, &self.expr)
-	}
-}
-
 pub trait ArgLike {
 	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>>;
 }
@@ -29,10 +17,8 @@
 		Ok(if tailstrict {
 			Thunk::evaluated(evaluate(ctx, self)?)
 		} else {
-			Thunk::new(EvaluateThunk {
-				ctx,
-				expr: (*self).clone(),
-			})
+			let expr = (*self).clone();
+			Thunk!(move || evaluate(ctx, &expr))
 		})
 	}
 }
@@ -65,10 +51,8 @@
 			Self::Code(code) => Ok(if tailstrict {
 				Thunk::evaluated(evaluate(ctx, code)?)
 			} else {
-				Thunk::new(EvaluateThunk {
-					ctx,
-					expr: code.clone(),
-				})
+				let code = code.clone();
+				Thunk!(move || evaluate(ctx, &code))
 			}),
 			Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
 			Self::Lazy(lazy) => Ok(lazy.clone()),
@@ -140,10 +124,10 @@
 				if tailstrict {
 					Thunk::evaluated(evaluate(ctx.clone(), arg)?)
 				} else {
-					Thunk::new(EvaluateThunk {
-						ctx: ctx.clone(),
-						expr: arg.clone(),
-					})
+					let ctx = ctx.clone();
+					let arg = arg.clone();
+
+					Thunk!(move || evaluate(ctx, &arg))
 				},
 			)?;
 		}
@@ -162,10 +146,10 @@
 				if tailstrict {
 					Thunk::evaluated(evaluate(ctx.clone(), arg)?)
 				} else {
-					Thunk::new(EvaluateThunk {
-						ctx: ctx.clone(),
-						expr: arg.clone(),
-					})
+					let ctx = ctx.clone();
+					let arg = arg.clone();
+
+					Thunk!(move || evaluate(ctx, &arg))
 				},
 			)?;
 		}
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -12,24 +12,9 @@
 	evaluate_named,
 	function::builtin::ParamDefault,
 	gc::GcHashMap,
-	val::ThunkValue,
 	Context, Pending, Thunk, Val,
 };
-
-#[derive(Trace)]
-struct EvaluateNamedThunk {
-	ctx: Pending<Context>,
-	name: IStr,
-	value: LocExpr,
-}
 
-impl ThunkValue for EvaluateNamedThunk {
-	type Output = Val;
-	fn get(self: Box<Self>) -> Result<Val> {
-		evaluate_named(self.ctx.unwrap(), &self.value, self.name)
-	}
-}
-
 /// Creates correct [context](Context) for function body evaluation returning error on invalid call.
 ///
 /// ## Parameters
@@ -105,11 +90,12 @@
 
 			destruct(
 				&param.0,
-				Thunk::new(EvaluateNamedThunk {
-					ctx: fctx.clone(),
-					name: param.0.name().unwrap_or_else(|| "<destruct>".into()),
-					value: param.1.clone().expect("default exists"),
-				}),
+				{
+					let ctx = fctx.clone();
+					let name = param.0.name().unwrap_or_else(|| "<destruct>".into());
+					let value = param.1.clone().expect("default exists");
+					Thunk!(move || evaluate_named(ctx.unwrap(), &value, name))
+				},
 				fctx.clone(),
 				&mut defaults,
 			)?;
@@ -241,11 +227,12 @@
 		if let Some(v) = &param.1 {
 			destruct(
 				&param.0.clone(),
-				Thunk::new(EvaluateNamedThunk {
-					ctx: fctx.clone(),
-					name: param.0.name().unwrap_or_else(|| "<destruct>".into()),
-					value: v.clone(),
-				}),
+				{
+					let ctx = fctx.clone();
+					let name = param.0.name().unwrap_or_else(|| "<destruct>".into());
+					let value = v.clone();
+					Thunk!(move || evaluate_named(ctx.unwrap(), &value, name))
+				},
 				fctx.clone(),
 				&mut bindings,
 			)?;
modifiednix/benchmarks.nixdiffbeforeafterboth
48 outputHashMode = "recursive";48 outputHashMode = "recursive";
49 buildInputs = [cacert];49 buildInputs = [cacert];
50 }50 }
51 ''51 ''
52 mkdir -p $out52 mkdir -p $out
53 cp -r ${src}/* $out/53 cp -r ${src}/* $out/
54 cd $out54 cd $out
55 mkdir vendor55 chmod u+w jsonnetfile.lock.json
56 mkdir vendor
56 ${jsonnet-bundler}/bin/jb install57 ${jsonnet-bundler}/bin/jb install
57 '';58 '';
5859
59 # Removes outsiders from the output60 # Removes outsiders from the output
60 # Useful when comparing performance of different jrsonnet releases61 # Useful when comparing performance of different jrsonnet releases
88 skipCpp ? "",89 skipCpp ? "",
89 skipGo ? "",90 skipGo ? "",
90 vendor ? "",91 vendor ? "",
91 }: ''92 }: ''
92 echo >> $out93 echo >> $out
93 echo "### ${name}" >> $out94 echo "### ${name}" >> $out
94 echo >> $out95 echo >> $out
95 ${optionalString (skipRustAlternative != "") ''96 ${optionalString (skipRustAlternative != "") ''
96 echo "> Note: No results for Rust (alternative), ${skipRustAlternative}" >> $out97 echo "> Note: No results for Rust (alternative), ${skipRustAlternative}" >> $out
97 echo >> $out98 echo >> $out
98 ''}99 ''}
99 ${optionalString (skipGo != "") ''100 ${optionalString (skipGo != "") ''
100 echo "> Note: No results for Go, ${skipGo}" >> $out101 echo "> Note: No results for Go, ${skipGo}" >> $out
101 echo >> $out102 echo >> $out
102 ''}103 ''}
103 ${optionalString (skipScala != "") ''104 ${optionalString (skipScala != "") ''
104 echo "> Note: No results for Scala, ${skipScala}" >> $out105 echo "> Note: No results for Scala, ${skipScala}" >> $out
105 echo >> $out106 echo >> $out
106 ''}107 ''}
107 ${optionalString (skipCpp != "") ''108 ${optionalString (skipCpp != "") ''
108 echo "> Note: No results for C++, ${skipCpp}" >> $out109 echo "> Note: No results for C++, ${skipCpp}" >> $out
109 echo >> $out110 echo >> $out
110 ''}111 ''}
111 ${optionalString (!quick && !omitSource) ''112 ${optionalString (!quick && !omitSource) ''
112 echo "<details>" >> $out113 echo "<details>" >> $out
113 echo "<summary>Source</summary>" >> $out114 echo "<summary>Source</summary>" >> $out
114 echo >> $out115 echo >> $out
115 echo "\`\`\`jsonnet" >> $out116 echo "\`\`\`jsonnet" >> $out
116 ${optionalString pathIsGenerator "echo \"// Generator source\" >> $out"}117 ${optionalString pathIsGenerator "echo \"// Generator source\" >> $out"}
117 cat ${path} >> $out118 cat ${path} >> $out
118 echo >> $out119 echo >> $out
119 echo "\`\`\`" >> $out120 echo "\`\`\`" >> $out
120 echo "</details>" >> $out121 echo "</details>" >> $out
121 echo >> $out122 echo >> $out
122 ''}123 ''}
123 path=${path}124 path=${path}
124 ${optionalString pathIsGenerator ''125 ${optionalString pathIsGenerator ''
125 go-jsonnet $path > generated.jsonnet126 go-jsonnet $path > generated.jsonnet
126 path=generated.jsonnet127 path=generated.jsonnet
127 ''}128 ''}
128 hyperfine -N -w4 -m20 --output=pipe --style=basic --export-markdown result.md \129 hyperfine -N -w4 -m20 --output=pipe --style=basic --export-asciidoc result.adoc \
129 ${concatStringsSep " " (forEach jrsonnetVariants (130 ${concatStringsSep " " (forEach jrsonnetVariants (
130 variant: "\"${variant.drv}/bin/jrsonnet $path${optionalString (vendor != "") " -J${vendor}"}\" -n \"Rust${131 variant: "\"${variant.drv}/bin/jrsonnet $path${optionalString (vendor != "") " -J${vendor}"}\" -n \"Rust${
131 if variant.name != ""132 if variant.name != ""
132 then " (${variant.name})"133 then " (${variant.name})"
133 else ""134 else ""
134 }\""135 }\""
135 ))} \136 ))} \
136 ${optionalString (skipRustAlternative == "") "\"rsjsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Rust (alternative, rsjsonnet)\""} \137 ${optionalString (skipRustAlternative == "") "\"rsjsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Rust (alternative, rsjsonnet)\""} \
137 ${optionalString (skipGo == "") "\"go-jsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Go\""} \138 ${optionalString (skipGo == "") "\"go-jsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Go\""} \
138 ${optionalString (skipScala == "") "\"sjsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Scala\""} \139 ${optionalString (skipScala == "") "\"sjsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Scala\""} \
139 ${optionalString (skipCpp == "") "\"jsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"C++\""}140 ${optionalString (skipCpp == "") "\"jsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"C++\""}
140 cat result.md >> $out141 cat result.adoc >> $out
141 '';142 '';
142 in ''143 in ''
143 set -oux144 set -oux
144145