difftreelog
perf std ast codegen
in: master
5 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -142,16 +142,18 @@
"jsonnet-stdlib",
"md5",
"serde",
+ "structdump",
]
[[package]]
name = "jsonnet-parser"
version = "0.1.0"
dependencies = [
- "bincode",
"jsonnet-stdlib",
"peg",
"serde",
+ "structdump",
+ "structdump-derive",
"unescape",
]
@@ -247,9 +249,9 @@
[[package]]
name = "quote"
-version = "1.0.5"
+version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "42934bc9c8ab0d3b273a16d8551c8f0fcff46be73276ca083ec2414c15c4ba5e"
+checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
@@ -275,6 +277,23 @@
]
[[package]]
+name = "structdump"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2e16ec33a0342fdb67d13913b4ffae6527ebccfa04b5d7da174bdc7a31db29b8"
+
+[[package]]
+name = "structdump-derive"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06c337fdc077e02ccbfcc62af0090564a4af342975c3b7be09705efab90c1888"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "syn"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
crates/jsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jsonnet-evaluator/Cargo.toml
+++ b/crates/jsonnet-evaluator/Cargo.toml
@@ -9,8 +9,10 @@
[features]
default = ["serialized-stdlib", "faster"]
# Serializes standard library AST instead of parsing them every run
-serialized-stdlib = ["serde", "bincode"]
-# Replace some standard library functions with faster implementations
+serialized-stdlib = ["serde", "bincode", "jsonnet-parser/deserialize"]
+# Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time
+codegenerated-stdlib = []
+# Replace some standard library functions with faster implementations (I.e manifestJsonEx)
# Library works fine without this feature, but requires more memory and time for std function calls
faster = []
@@ -21,16 +23,12 @@
indexmap = "1.4.0"
md5 = "0.7.0"
-[dependencies.serde]
-version = "1.0.114"
-optional = true
+serde = { version = "1.0.114", optional = true }
+bincode = { version = "1.3.1", optional = true }
-[dependencies.bincode]
-version = "1.3.1"
-optional = true
-
[build-dependencies]
-jsonnet-parser = { path = "../jsonnet-parser" }
+jsonnet-parser = { path = "../jsonnet-parser", features = ["dump", "serialize", "deserialize"] }
jsonnet-stdlib = { path = "../jsonnet-stdlib" }
+structdump = "0.1.2"
serde = "1.0.114"
bincode = "1.3.1"
crates/jsonnet-evaluator/README.mddiffbeforeafterboth--- a/crates/jsonnet-evaluator/README.md
+++ b/crates/jsonnet-evaluator/README.md
@@ -2,6 +2,35 @@
Interpreter for parsed jsonnet tree
+## Standard library
+
+jsonnet stdlib is embedded into evaluator, but there is different modes for this:
+
+- `codegenerated-stdlib`
+ - generates source code for reproducing stdlib AST ([Example](https://gist.githubusercontent.com/CertainLach/7b3149df556f3406f5e9368aaa9f32ec/raw/0c80d8ab9aa7b9288c6219a2779cb2ab37287669/a.rs))
+ - fastest on interpretation, slowest on compilation (it takes more than 5 minutes to optimize them by llvm)
+- `serialized-stdlib`
+ - serializes standard library AST using serde
+ - slower than `codegenerated-stdlib` at runtime, but have no compilation speed penality
+- none
+ - leaves only stdlib source code in binary, processing them same way as user supplied data
+ - slowest (as it involves parsing of standard library source code)
+
+Because of `codegenerated-stdlib` compilation slowdown, `serialized-stdlib` is used by default
+
+### Benchmark
+
+Can also be run via `cargo bench`
+
+```md
+# codegenerated-stdlib
+test tests::bench_codegen ... bench: 401,696 ns/iter (+/- 38,521)
+# serialized-stdlib
+test tests::bench_serialize ... bench: 1,763,999 ns/iter (+/- 76,211)
+# none
+test tests::bench_parse ... bench: 7,206,164 ns/iter (+/- 1,067,418)
+```
+
## Intristics
Some functions from stdlib are implemented as intristics
crates/jsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/build.rs
+++ b/crates/jsonnet-evaluator/build.rs
@@ -10,6 +10,7 @@
path::{Path, PathBuf},
rc::Rc,
};
+use structdump::CodegenResult;
fn main() {
let parsed = parse(
@@ -46,10 +47,19 @@
} else {
parsed
};
+ {
+ let mut codegen = CodegenResult::default();
+ let code = codegen.codegen(&parsed);
- let out_dir = env::var("OUT_DIR").unwrap();
- let dest_path = Path::new(&out_dir).join("stdlib.bincode");
- let mut f = File::create(&dest_path).unwrap();
- f.write_all(&serialize(&parsed).expect("serialize"))
- .unwrap();
+ let out_dir = env::var("OUT_DIR").unwrap();
+ let dest_path = Path::new(&out_dir).join("stdlib.rs");
+ let mut f = File::create(&dest_path).unwrap();
+ f.write_all(&code.as_bytes()).unwrap();
+ }
+ {
+ let out_dir = env::var("OUT_DIR").unwrap();
+ let dest_path = Path::new(&out_dir).join("stdlib.bincode");
+ let mut f = File::create(&dest_path).unwrap();
+ f.write_all(&serialize(&parsed).unwrap()).unwrap();
+ }
}
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![feature(box_syntax, box_patterns)]1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]3#![feature(debug_non_exhaustive)]4#![feature(test)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]67extern crate test;85mod ctx;9mod ctx;6mod dynamic;10mod dynamic;228 let std_path = Rc::new(PathBuf::from("std.jsonnet"));232 let std_path = Rc::new(PathBuf::from("std.jsonnet"));229 self.run_in_state(|| {233 self.run_in_state(|| {230 use jsonnet_stdlib::STDLIB_STR;234 use jsonnet_stdlib::STDLIB_STR;235 let mut parsed = false;236 #[cfg(feature = "codegenerated-stdlib")]237 if !parsed {238 parsed = true;239 #[allow(clippy::all)]240 let stdlib = {241 use jsonnet_parser::*;242 include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))243 };244 self.add_parsed_file(std_path.clone(), STDLIB_STR.to_owned().into(), stdlib)245 .unwrap();246 }247231 if cfg!(feature = "serialized-stdlib") {248 #[cfg(feature = "serialized-stdlib")]249 if !parsed {250 parsed = true;232 self.add_parsed_file(251 self.add_parsed_file(233 std_path,252 std_path.clone(),234 STDLIB_STR.to_owned().into(),253 STDLIB_STR.to_owned().into(),235 bincode::deserialize(include_bytes!(concat!(254 bincode::deserialize(include_bytes!(concat!(236 env!("OUT_DIR"),255 env!("OUT_DIR"),239 .expect("deserialize stdlib"),258 .expect("deserialize stdlib"),240 )259 )241 .unwrap();260 .unwrap();242 } else {261 }243 self.add_file(std_path, STDLIB_STR.to_owned().into())262244 .unwrap();263 if !parsed {245 }264 self.add_file(std_path, STDLIB_STR.to_owned().into())265 .unwrap();266 }246 let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();267 let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();247 self.add_global("std".into(), val);268 self.add_global("std".into(), val);248 });269 });663 );684 );664 }685 }686687 use test::Bencher;688689 // This test is commented out by default, because of huge compilation slowdown690 // #[bench]691 // fn bench_codegen(b: &mut Bencher) {692 // b.iter(|| {693 // #[allow(clippy::all)]694 // let stdlib = {695 // use jsonnet_parser::*;696 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))697 // };698 // stdlib699 // })700 // }701702 #[bench]703 fn bench_serialize(b: &mut Bencher) {704 b.iter(|| {705 bincode::deserialize::<jsonnet_parser::LocExpr>(include_bytes!(concat!(706 env!("OUT_DIR"),707 "/stdlib.bincode"708 )))709 .expect("deserialize stdlib")710 })711 }712713 #[bench]714 fn bench_parse(b: &mut Bencher) {715 b.iter(|| {716 jsonnet_parser::parse(717 jsonnet_stdlib::STDLIB_STR,718 &jsonnet_parser::ParserSettings {719 loc_data: true,720 file_name: Rc::new(PathBuf::from("std.jsonnet")),721 },722 )723 })724 }665}725}666726