git.delta.rocks / jrsonnet / refs/commits / 87a7e44fcfaf

difftreelog

perf destruct capacity hints

Yaroslav Bolyukin2022-11-09parent: #06bf1b3.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
44 args: &dyn ArgsLike,44 args: &dyn ArgsLike,
45 tailstrict: bool,45 tailstrict: bool,
46) -> Result<Context> {46) -> Result<Context> {
47 let mut passed_args = GcHashMap::with_capacity(params.len());47 let mut passed_args =
48 GcHashMap::with_capacity(params.iter().map(|p| p.0.capacity_hint()).sum());
48 if args.unnamed_len() > params.len() {49 if args.unnamed_len() > params.len() {
49 throw!(TooManyArgsFunctionHas(50 throw!(TooManyArgsFunctionHas(
50 params.len(),51 params.len(),
84 // Default values should be created in newly created context85 // Default values should be created in newly created context
85 let fctx = Context::new_future();86 let fctx = Context::new_future();
86 let mut defaults =87 let mut defaults = GcHashMap::with_capacity(
87 GcHashMap::with_capacity(params.len() - filled_named - filled_positionals);88 params.iter().map(|p| p.0.capacity_hint()).sum::<usize>()
89 - filled_named - filled_positionals,
90 );
8891
241244
242 let fctx = Context::new_future();245 let fctx = Context::new_future();
243246
244 let mut bindings = GcHashMap::new();247 let mut bindings = GcHashMap::with_capacity(params.iter().map(|p| p.0.capacity_hint()).sum());
245248
246 for param in params.iter() {249 for param in params.iter() {
247 if let Some(v) = &param.1 {250 if let Some(v) = &param.1 {
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -228,6 +228,39 @@
 			_ => None,
 		}
 	}
+	pub fn capacity_hint(&self) -> usize {
+		#[cfg(feature = "exp-destruct")]
+		fn cap_rest(rest: &Option<DestructRest>) -> usize {
+			match rest {
+				Some(DestructRest::Keep(_)) => 1,
+				Some(DestructRest::Drop) => 0,
+				None => 0,
+			}
+		}
+		match self {
+			Self::Full(_) => 1,
+			#[cfg(feature = "exp-destruct")]
+			Self::Skip => 0,
+			#[cfg(feature = "exp-destruct")]
+			Self::Array { start, rest, end } => {
+				start.iter().map(Destruct::capacity_hint).sum::<usize>()
+					+ end.iter().map(Destruct::capacity_hint).sum::<usize>()
+					+ cap_rest(rest)
+			}
+			#[cfg(feature = "exp-destruct")]
+			Self::Object { fields, rest } => {
+				let mut out = 0;
+				for (_, into, _) in fields {
+					match into {
+						Some(v) => out += v.capacity_hint(),
+						// Field is destructured to default name
+						None => out += 1,
+					}
+				}
+				out + cap_rest(rest)
+			}
+		}
+	}
 }
 
 #[cfg_attr(feature = "structdump", derive(Codegen))]
@@ -244,6 +277,14 @@
 		value: LocExpr,
 	},
 }
+impl BindSpec {
+	pub fn capacity_hint(&self) -> usize {
+		match self {
+			BindSpec::Field { into, .. } => into.capacity_hint(),
+			BindSpec::Function { .. } => 1,
+		}
+	}
+}
 
 #[cfg_attr(feature = "structdump", derive(Codegen))]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]