git.delta.rocks / jrsonnet / refs/commits / 9e9c9376494e

difftreelog

perf rc imports

Лач2020-06-26parent: #4a1c9d0.patch.diff
in: master

3 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -136,7 +136,7 @@
 	let mut input = current_dir().unwrap();
 	input.push(opts.input.clone());
 	let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();
-	if let Err(e) = evaluator.add_file(input.clone(), code_string.clone()) {
+	if let Err(e) = evaluator.add_file(Rc::new(input.clone()), code_string.clone().into()) {
 		print_syntax_error(e, &input, &code_string);
 		std::process::exit(1);
 	}
modifiedcrates/jsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/import.rs
+++ b/crates/jsonnet-evaluator/src/import.rs
@@ -3,19 +3,19 @@
 use fs::File;
 use std::fs;
 use std::io::Read;
-use std::{cell::RefCell, collections::HashMap, path::PathBuf};
+use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};
 
 pub trait ImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<PathBuf>;
-	fn load_file_contents(&self, resolved: &PathBuf) -> Result<String>;
+	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>>;
+	fn load_file_contents(&self, resolved: &PathBuf) -> Result<Rc<str>>;
 }
 
 pub struct DummyImportResolver;
 impl ImportResolver for DummyImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<PathBuf> {
+	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
 		create_error(Error::ImportNotSupported(from.clone(), path.clone()))
 	}
-	fn load_file_contents(&self, _resolved: &PathBuf) -> Result<String> {
+	fn load_file_contents(&self, _resolved: &PathBuf) -> Result<Rc<str>> {
 		// Can be only caused by library direct consumer, not by supplied jsonnet
 		panic!("dummy resolver can't load any file")
 	}
@@ -30,23 +30,23 @@
 	pub library_paths: Vec<PathBuf>,
 }
 impl ImportResolver for FileImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<PathBuf> {
+	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
 		let mut new_path = from.clone();
 		new_path.push(path);
 		if new_path.exists() {
-			Ok(new_path)
+			Ok(Rc::new(new_path))
 		} else {
 			for library_path in self.library_paths.iter() {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(cloned);
+					return Ok(Rc::new(cloned));
 				}
 			}
 			create_error(Error::ImportFileNotFound(from.clone(), path.clone()))
 		}
 	}
-	fn load_file_contents(&self, id: &PathBuf) -> Result<String> {
+	fn load_file_contents(&self, id: &PathBuf) -> Result<Rc<str>> {
 		let mut file = File::open(id).map_err(|_e| {
 			create_error::<()>(Error::ResolvedFileNotFound(id.clone()))
 				.err()
@@ -58,24 +58,24 @@
 				.err()
 				.unwrap()
 		})?;
-		Ok(out)
+		Ok(out.into())
 	}
 }
 
 pub struct CachingImportResolver {
-	resolution_cache: RefCell<HashMap<(PathBuf, PathBuf), Result<PathBuf>>>,
-	loading_cache: RefCell<HashMap<PathBuf, Result<String>>>,
+	resolution_cache: RefCell<HashMap<(PathBuf, PathBuf), Result<Rc<PathBuf>>>>,
+	loading_cache: RefCell<HashMap<PathBuf, Result<Rc<str>>>>,
 	inner: Box<dyn ImportResolver>,
 }
 impl ImportResolver for CachingImportResolver {
-	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<PathBuf> {
+	fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
 		self.resolution_cache
 			.borrow_mut()
 			.entry((from.clone(), path.clone()))
 			.or_insert_with(|| self.inner.resolve_file(from, path))
 			.clone()
 	}
-	fn load_file_contents(&self, resolved: &PathBuf) -> Result<String> {
+	fn load_file_contents(&self, resolved: &PathBuf) -> Result<Rc<str>> {
 		self.loading_cache
 			.borrow_mut()
 			.entry(resolved.clone())
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
57 }57 }
58}58}
5959
60pub struct FileData(String, LocExpr, Option<Val>);60pub struct FileData(Rc<str>, LocExpr, Option<Val>);
61#[derive(Default)]61#[derive(Default)]
62pub struct EvaluationStateInternals {62pub struct EvaluationStateInternals {
63 /// Used for stack-overflows and stacktraces63 /// Used for stack-overflows and stacktraces
64 stack: RefCell<Vec<StackTraceElement>>,64 stack: RefCell<Vec<StackTraceElement>>,
65 /// Contains file source codes and evaluated results for imports and pretty65 /// Contains file source codes and evaluated results for imports and pretty
66 /// printing stacktraces66 /// printing stacktraces
67 files: RefCell<HashMap<PathBuf, FileData>>,67 files: RefCell<HashMap<Rc<PathBuf>, FileData>>,
68 str_files: RefCell<HashMap<PathBuf, Rc<str>>>,68 str_files: RefCell<HashMap<Rc<PathBuf>, Rc<str>>>,
69 globals: RefCell<HashMap<Rc<str>, Val>>,69 globals: RefCell<HashMap<Rc<str>, Val>>,
7070
71 /// Values to use with std.extVar71 /// Values to use with std.extVar
109 ..Default::default()109 ..Default::default()
110 }))110 }))
111 }111 }
112 pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {112 pub fn add_file(
113 &self,
114 name: Rc<PathBuf>,
115 code: Rc<str>,
116 ) -> std::result::Result<(), ParseError> {
113 self.0.files.borrow_mut().insert(117 self.0.files.borrow_mut().insert(
114 name.clone(),118 name.clone(),
117 parse(121 parse(
118 &code,122 &code,
119 &ParserSettings {123 &ParserSettings {
120 file_name: Rc::new(name),124 file_name: name,
121 loc_data: true,125 loc_data: true,
122 },126 },
123 )?,127 )?,
129 }133 }
130 pub fn add_parsed_file(134 pub fn add_parsed_file(
131 &self,135 &self,
132 name: PathBuf,136 name: Rc<PathBuf>,
133 code: String,137 code: Rc<str>,
134 parsed: LocExpr,138 parsed: LocExpr,
135 ) -> std::result::Result<(), ()> {139 ) -> std::result::Result<(), ()> {
136 self.0140 self.0
140144
141 Ok(())145 Ok(())
142 }146 }
143 pub fn get_source(&self, name: &PathBuf) -> Option<String> {147 pub fn get_source(&self, name: &PathBuf) -> Option<Rc<str>> {
144 let ro_map = self.0.files.borrow();148 let ro_map = self.0.files.borrow();
145 ro_map.get(name).map(|value| value.0.clone())149 ro_map.get(name).map(|value| value.0.clone())
146 }150 }
221 }225 }
222226
223 pub fn with_stdlib(&self) -> &Self {227 pub fn with_stdlib(&self) -> &Self {
228 let std_path = Rc::new(PathBuf::from("std.jsonnet"));
224 self.run_in_state(|| {229 self.run_in_state(|| {
225 use jsonnet_stdlib::STDLIB_STR;230 use jsonnet_stdlib::STDLIB_STR;
226 if cfg!(feature = "serialized-stdlib") {231 if cfg!(feature = "serialized-stdlib") {
227 self.add_parsed_file(232 self.add_parsed_file(
228 PathBuf::from("std.jsonnet"),233 std_path,
229 STDLIB_STR.to_owned(),234 STDLIB_STR.to_owned().into(),
230 bincode::deserialize(include_bytes!(concat!(235 bincode::deserialize(include_bytes!(concat!(
231 env!("OUT_DIR"),236 env!("OUT_DIR"),
232 "/stdlib.bincode"237 "/stdlib.bincode"
235 )240 )
236 .unwrap();241 .unwrap();
237 } else {242 } else {
238 self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())243 self.add_file(std_path, STDLIB_STR.to_owned().into())
239 .unwrap();244 .unwrap();
240 }245 }
241 let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();246 let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();