difftreelog
refactor remove manual ThunkValue implementations
in: master
3 files changed
crates/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))
},
)?;
}
crates/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(
¶m.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) = ¶m.1 {
destruct(
¶m.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,
)?;
nix/benchmarks.nixdiffbeforeafterboth1{2 lib,3 runCommand,4 jsonnet-bundler,5 cacert,6 stdenv,7 fetchFromGitHub,8 go-jsonnet,9 sjsonnet,10 jsonnet,11 rsjsonnet,12 hyperfine,13 quick ? false,14 jrsonnetVariants,15}:16with lib; let17 jsonnetBench = fetchFromGitHub {18 rev = "v0.19.1";19 owner = "google";20 repo = "jsonnet";21 hash = "sha256-q1MNdbyrx4vvN5woe0o90pPqaNtsZjI5RQ7KJt7rOpU=";22 };23 goJsonnetBench =24 (fetchFromGitHub {25 owner = "google";26 repo = "go-jsonnet";27 rev = "v0.19.1";28 hash = "sha256-FgQYnas0qkIedRAA8ApZXLzEylg6PS6+8zzl7j+yOeI=";29 })30 + "/builtin-benchmarks";31 graalvmBench = fetchFromGitHub {32 owner = "oracle";33 repo = "graal";34 rev = "bc305df3fe587960f7635f0185571500e5988475";35 hash = "sha256-4EKB1b2o4/qtYQ+nqbbs621OJrtjApsAWEBcw5EjrYc=";36 };37 kubePrometheusBench = let38 src = fetchFromGitHub {39 owner = "prometheus-operator";40 repo = "kube-prometheus";41 rev = "d3889807798d1697ea0691f10caf1b6a1997a8bd";42 hash = "sha256-TeYWHzoZAmDp2PzT7EH8XRUcvb3tR8Qfxel7o2QBvIM=";43 };44 in45 runCommand "kube-prometheus-vendor"46 {47 outputHash = "sha256-AGc0dHlD/Ld7I5b1+gOotzJkYrn+bB1VjISdD5NITtw=";48 outputHashMode = "recursive";49 buildInputs = [cacert];50 }51 ''52 mkdir -p $out53 cp -r ${src}/* $out/54 cd $out55 mkdir vendor56 ${jsonnet-bundler}/bin/jb install57 '';5859 # Removes outsiders from the output60 # Useful when comparing performance of different jrsonnet releases61 skipSlow =62 if quick63 then "slow benchmark, but only quick requested"64 else "";65in66 stdenv.mkDerivation {67 name = "benchmarks";68 __impure = true;69 unpackPhase = "true";7071 buildInputs = [72 sjsonnet73 jsonnet74 rsjsonnet75 go-jsonnet7677 hyperfine78 ];7980 installPhase = let81 mkBench = {82 name,83 path,84 omitSource ? false,85 pathIsGenerator ? false,86 skipRustAlternative ? "",87 skipScala ? "",88 skipCpp ? "",89 skipGo ? "",90 vendor ? "",91 }: ''92 echo >> $out93 echo "### ${name}" >> $out94 echo >> $out95 ${optionalString(skipRustAlternative!="")''96 echo "> Note: No results for Rust (alternative), ${skipRustAlternative}" >> $out97 echo >> $out98 ''}99 ${optionalString(skipGo!="")''100 echo "> Note: No results for Go, ${skipGo}" >> $out101 echo >> $out102 ''}103 ${optionalString(skipScala!="")''104 echo "> Note: No results for Scala, ${skipScala}" >> $out105 echo >> $out106 ''}107 ${optionalString(skipCpp!="")''108 echo "> Note: No results for C++, ${skipCpp}" >> $out109 echo >> $out110 ''}111 ${optionalString(!quick&&!omitSource)''112 echo "<details>" >> $out113 echo "<summary>Source</summary>" >> $out114 echo >> $out115 echo "\`\`\`jsonnet" >> $out116 ${optionalStringpathIsGenerator"echo \"// Generator source\" >> $out"}117 cat ${path} >> $out118 echo >> $out119 echo "\`\`\`" >> $out120 echo "</details>" >> $out121 echo >> $out122 ''}123 path=${path}124 ${optionalStringpathIsGenerator''125 go-jsonnet $path > generated.jsonnet126 path=generated.jsonnet127 ''}128 hyperfine -N -w4 -m20 --output=pipe --style=basic --export-markdown result.md \129 ${concatStringsSep" "(forEachjrsonnetVariants(130variant"\"${variant.drv}/bin/jrsonnet $path${optionalString(vendor!="")" -J${vendor}"}\" -n \"Rust${131 ifvariant.name!=""132then" (${variant.name})"133else""134 }\""135))} \136 ${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(skipScala=="")"\"sjsonnet $path${optionalString(vendor!="")" -J ${vendor}"}\" -n \"Scala\""} \139 ${optionalString(skipCpp=="")"\"jsonnet $path${optionalString(vendor!="")" -J ${vendor}"}\" -n \"C++\""}140 cat result.md >> $out141 '';142 in ''143 set -oux144145 temp=$(mktemp -d)146 cd $temp147148 touch $out149 ${optionalString(!quick)''150 cat ${} >> $out151 echo >> $out152153 echo "<details>" >> $out154 echo "<summary>Tested versions</summary>" >> $out155 echo >> $out156 echo Go: $(go-jsonnet --version) >> $out157 echo >> $out158 echo "\`\`\`" >> $out159 go-jsonnet --help >> $out160 echo "\`\`\`" >> $out161 echo >> $out162 echo C++: $(jsonnet --version) >> $out163 echo >> $out164 echo "\`\`\`" >> $out165 jsonnet --help >> $out166 echo "\`\`\`" >> $out167 echo >> $out168 echo Scala: >> $out169 echo >> $out170 echo "\`\`\`" >> $out171 sjsonnet 2>> $out || true172 echo "\`\`\`" >> $out173 echo >> $out174 echo "Rust (alternative):" >> $out175 echo >> $out176 echo "\`\`\`" >> $out177 rsjsonnet --help 2>> $out || true178 echo "\`\`\`" >> $out179 echo >> $out180 echo "</details>" >> $out181 echo >> $out182183 echo >> $out184 ''}185 echo "## Real world" >> $out186 ${mkBench{187name="Graalvm CI";188path="${graalvmBench}/ci.jsonnet";189skipCpp="takes longer than a hour";190skipGo=skipSlow;191skipScala=skipSlow;192}}193 ${mkBench{194name="Kube-prometheus manifests";195vendor="${kubePrometheusBench}/vendor";196path="${kubePrometheusBench}/example.jsonnet";197skipCpp=skipSlow;198skipGo=skipSlow;199skipScala=skipSlow;200}}201202 echo >> $out203 echo "## Benchmarks from C++ jsonnet (/perf_tests)" >> $out204 ${mkBench{205name="Large string join";206path="${jsonnetBench}/perf_tests/large_string_join.jsonnet";207skipScala=skipSlow;208}}209 ${mkBench{210name="Large string template";211omitSource=true;212path="${jsonnetBench}/perf_tests/large_string_template.jsonnet";213skipGo="fails with os stack size exhausion";214skipCpp=skipSlow;215skipScala=skipSlow;216}}217 ${mkBench{218name="Realistic 1";219path="${jsonnetBench}/perf_tests/realistic1.jsonnet";220skipGo=skipSlow;221skipCpp=skipSlow;222skipScala=skipSlow;223}}224 ${mkBench{225name="Realistic 2";226path="${jsonnetBench}/perf_tests/realistic2.jsonnet";227skipGo=skipSlow;228skipCpp=skipSlow;229skipScala=skipSlow;230}}231232 echo >> $out233 echo "## Benchmarks from C++ jsonnet (/benchmarks)" >> $out234 ${mkBench{235name="Tail call";236path="${jsonnetBench}/benchmarks/bench.01.jsonnet";237skipScala=skipSlow;238}}239 ${mkBench{240name="Inheritance recursion";241path="${jsonnetBench}/benchmarks/bench.02.jsonnet";242skipCpp=skipSlow;243skipGo=skipSlow;244}}245 ${mkBench{246name="Simple recursive call";247path="${jsonnetBench}/benchmarks/bench.03.jsonnet";248skipScala=skipSlow;249skipGo=skipSlow;250}}251 ${mkBench{252name="Foldl string concat";253path="${jsonnetBench}/benchmarks/bench.04.jsonnet";254skipCpp=skipSlow;255skipScala=skipSlow;256}}257 ${mkBench{258name="Array sorts";259path="${jsonnetBench}/benchmarks/bench.06.jsonnet";260skipCpp=skipSlow;261# std.assertEqual(reverse(std.range(1, 1000)), sort(std.range(1, 1000), keyF=function(x) -x))262skipScala="sjsonnet doesn't support keyF in std.sort: https://github.com/databricks/sjsonnet/issues/204";263}}264 ${mkBench{265name="Lazy array";266path="${jsonnetBench}/benchmarks/bench.07.jsonnet";267skipGo=skipSlow;268skipScala=skipSlow;269}}270 ${mkBench{271name="Inheritance function recursion";272path="${jsonnetBench}/benchmarks/bench.08.jsonnet";273skipCpp=skipSlow;274skipScala=skipSlow;275}}276 ${mkBench{277name="String strips";278path="${jsonnetBench}/benchmarks/bench.09.jsonnet";279skipCpp=skipSlow;280skipScala=skipSlow;281}}282 ${mkBench{283name="Big object";284path="${jsonnetBench}/benchmarks/gen_big_object.jsonnet";285pathIsGenerator=true;286skipScala=skipSlow;287}}288289 echo >> $out290 echo "## Benchmarks from Go jsonnet (builtins)" >> $out291 ${mkBench{292name="std.base64";293path="${goJsonnetBench}/base64.jsonnet";294skipRustAlternative=skipSlow;295skipCpp=skipSlow;296skipScala=skipSlow;297}}298 ${mkBench{299name="std.base64Decode";300path="${goJsonnetBench}/base64Decode.jsonnet";301skipRustAlternative=skipSlow;302skipCpp=skipSlow;303skipScala=skipSlow;304}}305 ${mkBench{306name="std.base64DecodeBytes";307path="${goJsonnetBench}/base64DecodeBytes.jsonnet";308skipRustAlternative=skipSlow;309skipCpp=skipSlow;310skipGo=skipSlow;311skipScala=skipSlow;312}}313 ${mkBench{314name="std.base64 (byte array)";315path="${goJsonnetBench}/base64_byte_array.jsonnet";316skipRustAlternative=skipSlow;317skipCpp=skipSlow;318skipGo=skipSlow;319skipScala=skipSlow;320}}321 ${mkBench{322name="std.foldl";323path="${goJsonnetBench}/foldl.jsonnet";324skipScala=skipSlow;325}}326 ${mkBench{327name="std.manifestJsonEx";328path="${goJsonnetBench}/manifestJsonEx.jsonnet";329skipScala=skipSlow;330skipCpp=skipSlow;331}}332 ${mkBench{333name="std.manifestTomlEx";334path="${goJsonnetBench}/manifestTomlEx.jsonnet";335skipScala="std.manifestTomlEx is not implemented: https://github.com/databricks/sjsonnet/issues/111";336skipCpp=skipSlow;337}}338 ${mkBench{339name="std.parseInt";340path="${goJsonnetBench}/parseInt.jsonnet";341skipScala=skipSlow;342skipCpp=skipSlow;343}}344 ${mkBench{345name="std.reverse";346path="${goJsonnetBench}/reverse.jsonnet";347skipCpp=skipSlow;348skipGo=skipSlow;349}}350 ${mkBench{351name="std.substr";352path="${goJsonnetBench}/substr.jsonnet";353skipScala=skipSlow;354}}355 ${mkBench{356name="Comparsion for array";357path="${goJsonnetBench}/comparison.jsonnet";358skipScala="array comparsion is not implemented";359skipCpp=skipSlow;360}}361 ${mkBench{362name="Comparsion for primitives";363path="${goJsonnetBench}/comparison2.jsonnet";364skipRustAlternative=skipSlow;365skipCpp="can't run: uses up to 192GB of RAM";366skipGo=skipSlow;367skipScala=skipSlow;368}}369 '';370 }