git.delta.rocks / jrsonnet / refs/commits / 6a58b0ec3386

difftreelog

perf use json serialization directly

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

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -5,7 +5,7 @@
 use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParserSettings};
 use location::{offset_to_location, CodeLocation};
 use std::env::current_dir;
-use std::{collections::HashMap, path::PathBuf, str::FromStr, rc::Rc};
+use std::{collections::HashMap, path::PathBuf, rc::Rc, str::FromStr};
 
 enum Format {
 	None,
@@ -173,40 +173,20 @@
 				}
 				v => v,
 			};
-			let v = match opts.format {
-				Format::Json => {
-					if opts.no_stdlib {
-						evaluator.with_stdlib();
-					}
-					evaluator.add_global("__tmp__to_json__".into(), v);
-					let v = evaluator.parse_evaluate_raw(&format!(
-						"std.manifestJsonEx(__tmp__to_json__, \"{}\")",
-						" ".repeat(opts.line_padding),
-					));
-					match v {
-						Ok(v) => v,
-						Err(err) => {
-							print_error(&err, evaluator, &opts);
-							std::process::exit(1);
-						}
-					}
-				}
+			let v = evaluator.run_in_state(|| match opts.format {
+				Format::Json => Ok(Val::Str(v.into_json(opts.line_padding)?)),
 				Format::Yaml => {
-					if opts.no_stdlib {
-						evaluator.with_stdlib();
-					}
 					evaluator.add_global("__tmp__to_yaml__".into(), v);
-					let v = evaluator
-						.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")");
-					match v {
-						Ok(v) => v,
-						Err(err) => {
-							print_error(&err, evaluator, &opts);
-							std::process::exit(1);
-						}
-					}
+					evaluator.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")")
+				}
+				_ => Ok(v),
+			});
+			let v = match v {
+				Ok(v) => v,
+				Err(err) => {
+					print_error(&err, evaluator, &opts);
+					std::process::exit(1);
 				}
-				_ => v,
 			};
 			match v {
 				Val::Str(s) => println!("{}", s),
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -668,6 +668,7 @@
 						) {
 							Val::Arr(Rc::new(
 								arr.iter()
+									.cloned()
 									.filter(|e| {
 										predicate
 											.evaluate_values(context.clone(), &[e.clone()])
@@ -675,7 +676,6 @@
 											.try_cast_bool("filter predicate")
 											.unwrap()
 									})
-									.cloned()
 									.collect(),
 							))
 						} else {
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -296,7 +296,7 @@
 		Err(LocError(err, self.stack_trace()))
 	}
 
-	fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {
+	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {
 		EVAL_STATE.with(|v| {
 			let has_state = v.borrow().is_some();
 			if !has_state {
@@ -364,14 +364,11 @@
 		($str: expr) => {{
 			let evaluator = EvaluationState::default();
 			evaluator.with_stdlib();
-			let val = evaluator.parse_evaluate_raw($str).unwrap();
-			evaluator.add_global("__tmp__to_yaml__".into(), val);
 			evaluator
-				.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")
+				.parse_evaluate_raw($str)
 				.unwrap()
-				.try_cast_str("there should be json string")
+				.into_json(0)
 				.unwrap()
-				.clone()
 				.replace("\n", "")
 			}};
 	}
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/val.rs
1use crate::{2	create_error, evaluate,3	function::{inline_parse_function_call, place_args},4	with_state, Context, Error, ObjValue, Result,5};6use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParamsDesc};7use std::{8	cell::RefCell,9	fmt::{Debug, Display},10	rc::Rc,11};1213enum LazyValInternals {14	Computed(Val),15	Waiting(Box<dyn Fn() -> Result<Val>>),16}17#[derive(Clone)]18pub struct LazyVal(Rc<RefCell<LazyValInternals>>);19impl LazyVal {20	pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {21		LazyVal(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))22	}23	pub fn new_resolved(val: Val) -> Self {24		LazyVal(Rc::new(RefCell::new(LazyValInternals::Computed(val))))25	}26	pub fn evaluate(&self) -> Result<Val> {27		let new_value = match &*self.0.borrow() {28			LazyValInternals::Computed(v) => return Ok(v.clone()),29			LazyValInternals::Waiting(f) => f()?,30		};31		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());32		Ok(new_value)33	}34}3536#[macro_export]37macro_rules! lazy_val {38	($f: expr) => {39		$crate::LazyVal::new(Box::new($f))40	};41}42#[macro_export]43macro_rules! resolved_lazy_val {44	($f: expr) => {45		$crate::LazyVal::new_resolved($f)46	};47}48impl Debug for LazyVal {49	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {50		write!(f, "Lazy")51	}52}53impl PartialEq for LazyVal {54	fn eq(&self, other: &Self) -> bool {55		Rc::ptr_eq(&self.0, &other.0)56	}57}5859#[derive(Debug, PartialEq, Clone)]60pub struct FuncDesc {61	pub ctx: Context,62	pub params: ParamsDesc,63	pub body: LocExpr,64}65impl FuncDesc {66	/// This function is always inlined to make tailstrict work67	pub fn evaluate(&self, call_ctx: Context, args: &ArgsDesc, tailstrict: bool) -> Result<Val> {68		let ctx = inline_parse_function_call(69			call_ctx,70			Some(self.ctx.clone()),71			&self.params,72			args,73			tailstrict,74		)?;75		evaluate(ctx, &self.body)76	}7778	pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {79		let ctx = place_args(call_ctx, Some(self.ctx.clone()), &self.params, args)?;80		evaluate(ctx, &self.body)81	}82}8384#[derive(Debug, Clone, Copy, PartialEq)]85pub enum ValType {86	Bool,87	Null,88	Str,89	Num,90	Arr,91	Obj,92	Func,93}94impl ValType {95	pub fn name(&self) -> &'static str {96		use ValType::*;97		match self {98			Bool => "boolean",99			Null => "null",100			Str => "string",101			Num => "number",102			Arr => "array",103			Obj => "object",104			Func => "function",105		}106	}107}108impl Display for ValType {109	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {110		write!(f, "{}", self.name())111	}112}113114#[derive(Debug, PartialEq, Clone)]115pub enum Val {116	Bool(bool),117	Null,118	Str(Rc<str>),119	Num(f64),120	Lazy(LazyVal),121	Arr(Rc<Vec<Val>>),122	Obj(ObjValue),123	Func(FuncDesc),124125	// Library functions implemented in native126	Intristic(Rc<str>, Rc<str>),127}128macro_rules! matches_unwrap {129	($e: expr, $p: pat, $r: expr) => {130		match $e {131			$p => $r,132			_ => panic!("no match"),133			}134	};135}136impl Val {137	pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {138		let this_type = self.value_type()?;139		if this_type != val_type {140			create_error(Error::TypeMismatch(context, vec![val_type], this_type))141		} else {142			Ok(())143		}144	}145	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {146		self.assert_type(context, ValType::Bool)?;147		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Bool(v), v))148	}149	pub fn try_cast_str(self, context: &'static str) -> Result<Rc<str>> {150		self.assert_type(context, ValType::Str)?;151		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Str(v), v))152	}153	pub fn unwrap_if_lazy(&self) -> Result<Self> {154		Ok(if let Val::Lazy(v) = self {155			v.evaluate()?.unwrap_if_lazy()?156		} else {157			self.clone()158		})159	}160	pub fn value_type(&self) -> Result<ValType> {161		Ok(match self {162			Val::Str(..) => ValType::Str,163			Val::Num(..) => ValType::Num,164			Val::Arr(..) => ValType::Arr,165			Val::Obj(..) => ValType::Obj,166			Val::Func(..) => ValType::Func,167			Val::Bool(_) => ValType::Bool,168			Val::Null => ValType::Null,169			Val::Intristic(_, _) => ValType::Func,170			Val::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,171		})172	}173	pub fn into_json(self, padding: usize) -> Result<Rc<str>> {174		with_state(|s| {175			let ctx = s176				.create_default_context()?177				.with_var("__tmp__to_json__".into(), self)?;178			Ok(evaluate(179				ctx,180				&el!(Expr::Apply(181					el!(Expr::Index(182						el!(Expr::Var("std".into())),183						el!(Expr::Str("manifestJsonEx".into()))184					)),185					ArgsDesc(vec![186						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),187						Arg(None, el!(Expr::Str(" ".repeat(padding).into())))188					]),189					false190				)),191			)?192			.try_cast_str("to json")?)193		})194	}195}196197pub fn manifest_json_ex(val: &Val, padding: &str) -> Result<String> {198	let mut out = String::new();199	manifest_json_ex_buf(val, &mut out, padding, &mut String::new())?;200	Ok(out)201}202fn manifest_json_ex_buf(203	val: &Val,204	buf: &mut String,205	padding: &str,206	cur_padding: &mut String,207) -> Result<()> {208	use std::fmt::Write;209	match val.unwrap_if_lazy()? {210		Val::Bool(v) => {211			if v {212				buf.push_str("true");213			} else {214				buf.push_str("false");215			}216		}217		Val::Null => buf.push_str("null"),218		Val::Str(s) => buf.push_str(&escape_string_json(&s)),219		Val::Num(n) => write!(buf, "{}", n).unwrap(),220		Val::Arr(items) => {221			buf.push_str("[\n");222			if !items.is_empty() {223				let old_len = cur_padding.len();224				cur_padding.push_str(padding);225				for (i, item) in items.iter().enumerate() {226					if i != 0 {227						buf.push_str(",\n")228					}229					buf.push_str(cur_padding);230					manifest_json_ex_buf(item, buf, padding, cur_padding)?;231				}232				cur_padding.truncate(old_len);233			}234			buf.push('\n');235			buf.push_str(cur_padding);236			buf.push(']');237		}238		Val::Obj(obj) => {239			buf.push_str("{\n");240			let fields = obj.visible_fields();241			if !fields.is_empty() {242				let old_len = cur_padding.len();243				cur_padding.push_str(padding);244				for (i, field) in fields.into_iter().enumerate() {245					if i != 0 {246						buf.push_str(",\n")247					}248					buf.push_str(cur_padding);249					buf.push_str(&escape_string_json(&field));250					buf.push_str(": ");251					manifest_json_ex_buf(&obj.get(field)?.unwrap(), buf, padding, cur_padding)?;252				}253				cur_padding.truncate(old_len);254			}255			buf.push('\n');256			buf.push_str(cur_padding);257			buf.push('}');258		}259		Val::Func(_) | Val::Intristic(_, _) => panic!("tried to manifest function"),260		Val::Lazy(_) => unreachable!(),261	};262	Ok(())263}264pub fn escape_string_json(s: &str) -> String {265	use std::fmt::Write;266	let mut out = String::new();267	out.push('"');268	for c in s.chars() {269		match c {270			'"' => out.push_str("\\\""),271			'\\' => out.push_str("\\\\"),272			'\u{0008}' => out.push_str("\\b"),273			'\u{000c}' => out.push_str("\\f"),274			'\n' => out.push_str("\\n"),275			'\r' => out.push_str("\\r"),276			'\t' => out.push_str("\\t"),277			c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {278				write!(out, "\\u{:04x}", c as u32).unwrap()279			}280			c => out.push(c),281		}282	}283	out.push('"');284	out285}286287#[test]288fn json_test() {289	assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")290}
after · crates/jsonnet-evaluator/src/val.rs
1use crate::{2	create_error, evaluate,3	function::{inline_parse_function_call, place_args},4	Context, Error, ObjValue, Result,5};6use jsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};7use std::{8	cell::RefCell,9	fmt::{Debug, Display},10	rc::Rc,11};1213enum LazyValInternals {14	Computed(Val),15	Waiting(Box<dyn Fn() -> Result<Val>>),16}17#[derive(Clone)]18pub struct LazyVal(Rc<RefCell<LazyValInternals>>);19impl LazyVal {20	pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {21		LazyVal(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))22	}23	pub fn new_resolved(val: Val) -> Self {24		LazyVal(Rc::new(RefCell::new(LazyValInternals::Computed(val))))25	}26	pub fn evaluate(&self) -> Result<Val> {27		let new_value = match &*self.0.borrow() {28			LazyValInternals::Computed(v) => return Ok(v.clone()),29			LazyValInternals::Waiting(f) => f()?,30		};31		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());32		Ok(new_value)33	}34}3536#[macro_export]37macro_rules! lazy_val {38	($f: expr) => {39		$crate::LazyVal::new(Box::new($f))40	};41}42#[macro_export]43macro_rules! resolved_lazy_val {44	($f: expr) => {45		$crate::LazyVal::new_resolved($f)46	};47}48impl Debug for LazyVal {49	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {50		write!(f, "Lazy")51	}52}53impl PartialEq for LazyVal {54	fn eq(&self, other: &Self) -> bool {55		Rc::ptr_eq(&self.0, &other.0)56	}57}5859#[derive(Debug, PartialEq, Clone)]60pub struct FuncDesc {61	pub ctx: Context,62	pub params: ParamsDesc,63	pub body: LocExpr,64}65impl FuncDesc {66	/// This function is always inlined to make tailstrict work67	pub fn evaluate(&self, call_ctx: Context, args: &ArgsDesc, tailstrict: bool) -> Result<Val> {68		let ctx = inline_parse_function_call(69			call_ctx,70			Some(self.ctx.clone()),71			&self.params,72			args,73			tailstrict,74		)?;75		evaluate(ctx, &self.body)76	}7778	pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {79		let ctx = place_args(call_ctx, Some(self.ctx.clone()), &self.params, args)?;80		evaluate(ctx, &self.body)81	}82}8384#[derive(Debug, Clone, Copy, PartialEq)]85pub enum ValType {86	Bool,87	Null,88	Str,89	Num,90	Arr,91	Obj,92	Func,93}94impl ValType {95	pub fn name(&self) -> &'static str {96		use ValType::*;97		match self {98			Bool => "boolean",99			Null => "null",100			Str => "string",101			Num => "number",102			Arr => "array",103			Obj => "object",104			Func => "function",105		}106	}107}108impl Display for ValType {109	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {110		write!(f, "{}", self.name())111	}112}113114#[derive(Debug, PartialEq, Clone)]115pub enum Val {116	Bool(bool),117	Null,118	Str(Rc<str>),119	Num(f64),120	Lazy(LazyVal),121	Arr(Rc<Vec<Val>>),122	Obj(ObjValue),123	Func(FuncDesc),124125	// Library functions implemented in native126	Intristic(Rc<str>, Rc<str>),127}128macro_rules! matches_unwrap {129	($e: expr, $p: pat, $r: expr) => {130		match $e {131			$p => $r,132			_ => panic!("no match"),133			}134	};135}136impl Val {137	pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {138		let this_type = self.value_type()?;139		if this_type != val_type {140			create_error(Error::TypeMismatch(context, vec![val_type], this_type))141		} else {142			Ok(())143		}144	}145	pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {146		self.assert_type(context, ValType::Bool)?;147		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Bool(v), v))148	}149	pub fn try_cast_str(self, context: &'static str) -> Result<Rc<str>> {150		self.assert_type(context, ValType::Str)?;151		Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Str(v), v))152	}153	pub fn unwrap_if_lazy(&self) -> Result<Self> {154		Ok(if let Val::Lazy(v) = self {155			v.evaluate()?.unwrap_if_lazy()?156		} else {157			self.clone()158		})159	}160	pub fn value_type(&self) -> Result<ValType> {161		Ok(match self {162			Val::Str(..) => ValType::Str,163			Val::Num(..) => ValType::Num,164			Val::Arr(..) => ValType::Arr,165			Val::Obj(..) => ValType::Obj,166			Val::Func(..) => ValType::Func,167			Val::Bool(_) => ValType::Bool,168			Val::Null => ValType::Null,169			Val::Intristic(_, _) => ValType::Func,170			Val::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,171		})172	}173	#[cfg(feature = "faster")]174	pub fn into_json(self, padding: usize) -> Result<Rc<str>> {175		manifest_json_ex(&self, &" ".repeat(padding)).map(|s| s.into())176	}177	#[cfg(not(feature = "faster"))]178	pub fn into_json(self, padding: usize) -> Result<Rc<str>> {179		with_state(|s| {180			let ctx = s181				.create_default_context()?182				.with_var("__tmp__to_json__".into(), self)?;183			Ok(evaluate(184				ctx,185				&el!(Expr::Apply(186					el!(Expr::Index(187						el!(Expr::Var("std".into())),188						el!(Expr::Str("manifestJsonEx".into()))189					)),190					ArgsDesc(vec![191						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),192						Arg(None, el!(Expr::Str(" ".repeat(padding).into())))193					]),194					false195				)),196			)?197			.try_cast_str("to json")?)198		})199	}200}201202pub fn manifest_json_ex(val: &Val, padding: &str) -> Result<String> {203	let mut out = String::new();204	manifest_json_ex_buf(val, &mut out, padding, &mut String::new())?;205	Ok(out)206}207fn manifest_json_ex_buf(208	val: &Val,209	buf: &mut String,210	padding: &str,211	cur_padding: &mut String,212) -> Result<()> {213	use std::fmt::Write;214	match val.unwrap_if_lazy()? {215		Val::Bool(v) => {216			if v {217				buf.push_str("true");218			} else {219				buf.push_str("false");220			}221		}222		Val::Null => buf.push_str("null"),223		Val::Str(s) => buf.push_str(&escape_string_json(&s)),224		Val::Num(n) => write!(buf, "{}", n).unwrap(),225		Val::Arr(items) => {226			buf.push_str("[\n");227			if !items.is_empty() {228				let old_len = cur_padding.len();229				cur_padding.push_str(padding);230				for (i, item) in items.iter().enumerate() {231					if i != 0 {232						buf.push_str(",\n")233					}234					buf.push_str(cur_padding);235					manifest_json_ex_buf(item, buf, padding, cur_padding)?;236				}237				cur_padding.truncate(old_len);238			}239			buf.push('\n');240			buf.push_str(cur_padding);241			buf.push(']');242		}243		Val::Obj(obj) => {244			buf.push_str("{\n");245			let fields = obj.visible_fields();246			if !fields.is_empty() {247				let old_len = cur_padding.len();248				cur_padding.push_str(padding);249				for (i, field) in fields.into_iter().enumerate() {250					if i != 0 {251						buf.push_str(",\n")252					}253					buf.push_str(cur_padding);254					buf.push_str(&escape_string_json(&field));255					buf.push_str(": ");256					manifest_json_ex_buf(&obj.get(field)?.unwrap(), buf, padding, cur_padding)?;257				}258				cur_padding.truncate(old_len);259			}260			buf.push('\n');261			buf.push_str(cur_padding);262			buf.push('}');263		}264		Val::Func(_) | Val::Intristic(_, _) => panic!("tried to manifest function"),265		Val::Lazy(_) => unreachable!(),266	};267	Ok(())268}269pub fn escape_string_json(s: &str) -> String {270	use std::fmt::Write;271	let mut out = String::new();272	out.push('"');273	for c in s.chars() {274		match c {275			'"' => out.push_str("\\\""),276			'\\' => out.push_str("\\\\"),277			'\u{0008}' => out.push_str("\\b"),278			'\u{000c}' => out.push_str("\\f"),279			'\n' => out.push_str("\\n"),280			'\r' => out.push_str("\\r"),281			'\t' => out.push_str("\\t"),282			c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {283				write!(out, "\\u{:04x}", c as u32).unwrap()284			}285			c => out.push(c),286		}287	}288	out.push('"');289	out290}291292#[test]293fn json_test() {294	assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")295}