git.delta.rocks / jrsonnet / refs/commits / 03f24e72fbec

difftreelog

perf(evaluator) deserialize instead of parsing std

Лач2020-06-04parent: #f306bde.patch.diff
in: master

3 files changed

modifiedcrates/jsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jsonnet-evaluator/Cargo.toml
+++ b/crates/jsonnet-evaluator/Cargo.toml
@@ -6,7 +6,25 @@
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
+[features]
+default = ["serialized-stdlib"]
+serialized-stdlib = ["serde", "bincode"]
+
 [dependencies]
 jsonnet-parser = { path = "../jsonnet-parser" }
 closure = "0.3.0"
-jsonnet-stdlib = { version = "0.1.0", path = "../jsonnet-stdlib" }
+jsonnet-stdlib = { path = "../jsonnet-stdlib" }
+
+[dependencies.serde]
+version = "1.0.111"
+optional = true
+
+[dependencies.bincode]
+version = "1.2.1"
+optional = true
+
+[build-dependencies]
+jsonnet-parser = { path = "../jsonnet-parser" }
+jsonnet-stdlib = { path = "../jsonnet-stdlib" }
+serde = "1.0.111"
+bincode = "1.2.1"
addedcrates/jsonnet-evaluator/build.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jsonnet-evaluator/build.rs
@@ -0,0 +1,21 @@
+use bincode::serialize;
+use jsonnet_parser::{parse, ParserSettings};
+use jsonnet_stdlib::STDLIB_STR;
+use std::{env, fs::File, io::Write, path::Path};
+
+fn main() {
+	let parsed = parse(
+		STDLIB_STR,
+		&ParserSettings {
+			file_name: "std.jsonnet".to_owned(),
+			loc_data: true,
+		},
+	)
+	.expect("parse");
+
+	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();
+}
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
41pub struct EvaluationStateInternals {41pub struct EvaluationStateInternals {
42 /// Used for stack-overflows and stacktraces42 /// Used for stack-overflows and stacktraces
43 stack: RefCell<Vec<StackTraceElement>>,43 stack: RefCell<Vec<StackTraceElement>>,
44 /// Contains file source codes and evaluated results for imports and pretty printing stacktraces44 /// Contains file source codes and evaluated results for imports and pretty
45 /// printing stacktraces
45 files: RefCell<HashMap<String, FileData>>,46 files: RefCell<HashMap<String, FileData>>,
46 globals: RefCell<HashMap<String, Val>>,47 globals: RefCell<HashMap<String, Val>>,
47}48}
8485
85 Ok(())86 Ok(())
86 }87 }
88 pub fn add_parsed_file(
89 &self,
90 name: String,
91 code: String,
92 parsed: LocExpr,
93 ) -> std::result::Result<(), Box<dyn std::error::Error>> {
94 self.0
95 .files
96 .borrow_mut()
97 .insert(name, FileData(code, parsed, None));
98
99 Ok(())
100 }
87 pub fn get_source(&self, name: &str) -> String {101 pub fn get_source(&self, name: &str) -> String {
88 let ro_map = self.0.files.borrow();102 let ro_map = self.0.files.borrow();
89 let value = ro_map103 let value = ro_map
134 pub fn add_stdlib(&self) {148 pub fn add_stdlib(&self) {
135 self.begin_state();149 self.begin_state();
136 use jsonnet_stdlib::STDLIB_STR;150 use jsonnet_stdlib::STDLIB_STR;
151 if cfg!(feature = "serialized-stdlib") {
152 self.add_parsed_file(
153 "std.jsonnet".to_owned(),
154 STDLIB_STR.to_owned(),
155 bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode"))).expect("deserialize stdlib"),
156 ).unwrap();
157 } else {
137 self.add_file("std.jsonnet".to_owned(), STDLIB_STR.to_owned())158 self.add_file("std.jsonnet".to_owned(), STDLIB_STR.to_owned())
138 .unwrap();159 .unwrap();
160 }
139 let val = self.evaluate_file("std.jsonnet").unwrap();161 let val = self.evaluate_file("std.jsonnet").unwrap();
140 self.0.globals.borrow_mut().insert("std".to_owned(), val);162 self.0.globals.borrow_mut().insert("std".to_owned(), val);
141 self.end_state();163 self.end_state();