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
66
7# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html7# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
9[features]
10default = ["serialized-stdlib"]
11serialized-stdlib = ["serde", "bincode"]
812
9[dependencies]13[dependencies]
10jsonnet-parser = { path = "../jsonnet-parser" }14jsonnet-parser = { path = "../jsonnet-parser" }
11closure = "0.3.0"15closure = "0.3.0"
12jsonnet-stdlib = { version = "0.1.0", path = "../jsonnet-stdlib" }16jsonnet-stdlib = { path = "../jsonnet-stdlib" }
17
18[dependencies.serde]
19version = "1.0.111"
20optional = true
21
22[dependencies.bincode]
23version = "1.2.1"
24optional = true
25
26[build-dependencies]
27jsonnet-parser = { path = "../jsonnet-parser" }
28jsonnet-stdlib = { path = "../jsonnet-stdlib" }
29serde = "1.0.111"
30bincode = "1.2.1"
1331
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
--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -41,7 +41,8 @@
 pub struct EvaluationStateInternals {
 	/// Used for stack-overflows and stacktraces
 	stack: RefCell<Vec<StackTraceElement>>,
-	/// Contains file source codes and evaluated results for imports and pretty printing stacktraces
+	/// Contains file source codes and evaluated results for imports and pretty
+	/// printing stacktraces
 	files: RefCell<HashMap<String, FileData>>,
 	globals: RefCell<HashMap<String, Val>>,
 }
@@ -84,6 +85,19 @@
 
 		Ok(())
 	}
+	pub fn add_parsed_file(
+		&self,
+		name: String,
+		code: String,
+		parsed: LocExpr,
+	) -> std::result::Result<(), Box<dyn std::error::Error>> {
+		self.0
+			.files
+			.borrow_mut()
+			.insert(name, FileData(code, parsed, None));
+
+		Ok(())
+	}
 	pub fn get_source(&self, name: &str) -> String {
 		let ro_map = self.0.files.borrow();
 		let value = ro_map
@@ -134,8 +148,16 @@
 	pub fn add_stdlib(&self) {
 		self.begin_state();
 		use jsonnet_stdlib::STDLIB_STR;
-		self.add_file("std.jsonnet".to_owned(), STDLIB_STR.to_owned())
-			.unwrap();
+		if cfg!(feature = "serialized-stdlib") {
+			self.add_parsed_file(
+				"std.jsonnet".to_owned(),
+				STDLIB_STR.to_owned(),
+				bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode"))).expect("deserialize stdlib"),
+			).unwrap();
+		} else {
+			self.add_file("std.jsonnet".to_owned(), STDLIB_STR.to_owned())
+				.unwrap();
+		}
 		let val = self.evaluate_file("std.jsonnet").unwrap();
 		self.0.globals.borrow_mut().insert("std".to_owned(), val);
 		self.end_state();