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

no content

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();