git.delta.rocks / jrsonnet / refs/commits / 625fbae2cdcf

difftreelog

perf(evaluator) use hashmap for object body

Лач2020-07-01parent: #dcbb761.patch.diff
in: master

4 files changed

modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -8,7 +8,7 @@
 	alloc::Layout,
 	any::Any,
 	cell::RefCell,
-	collections::BTreeMap,
+	collections::HashMap,
 	ffi::{CStr, CString},
 	fs::File,
 	io::Read,
@@ -191,7 +191,7 @@
 ) {
 	match obj {
 		Val::Obj(old) => {
-			let mut new = BTreeMap::new();
+			let mut new = HashMap::new();
 			new.insert(
 				CStr::from_ptr(name).to_str().unwrap().into(),
 				ObjMember {
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -9,10 +9,7 @@
 	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,
 	Visibility,
 };
-use std::{
-	collections::{BTreeMap, HashMap},
-	rc::Rc,
-};
+use std::{collections::HashMap, rc::Rc};
 
 pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (Rc<str>, LazyBinding) {
 	let b = b.clone();
@@ -242,7 +239,7 @@
 		new_bindings.fill(bindings);
 	}
 
-	let mut new_members = BTreeMap::new();
+	let mut new_members = HashMap::new();
 	for member in members.iter() {
 		match member {
 			Member::Field(FieldMember {
@@ -317,7 +314,7 @@
 		ObjBody::MemberList(members) => evaluate_member_list_object(context, &members)?,
 		ObjBody::ObjComp(obj) => {
 			let future_this = FutureObjValue::new();
-			let mut new_members = BTreeMap::new();
+			let mut new_members = HashMap::new();
 			for (k, v) in evaluate_comp(
 				context.clone(),
 				&|ctx| {
@@ -450,13 +447,13 @@
 					0, obj: [Val::Obj]!!Val::Obj, vec![ValType::Obj];
 					1, inc_hidden: [Val::Bool]!!Val::Bool, vec![ValType::Bool];
 				], {
-					Ok(Val::Arr(Rc::new(
-						obj.fields_visibility()
-							.into_iter()
-							.filter(|(_k, v)| *v || inc_hidden)
-							.map(|(k, _v)| Val::Str(k))
-							.collect(),
-					)))
+					let mut out = obj.fields_visibility()
+						.into_iter()
+						.filter(|(_k, v)| *v || inc_hidden)
+						.map(|(k, _v)|k)
+						.collect::<Vec<_>>();
+					out.sort();
+					Ok(Val::Arr(Rc::new(out.into_iter().map(Val::Str).collect())))
 				}))?
 			}
 			// object, field, includeHidden
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
1use crate::{evaluate_add_op, LazyBinding, Result, Val};1use crate::{evaluate_add_op, LazyBinding, Result, Val};
2use indexmap::IndexMap;2use indexmap::IndexMap;
3use jrsonnet_parser::Visibility;3use jrsonnet_parser::{ExprLocation, Visibility};
4use std::{4use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
5 cell::RefCell,
6 collections::{BTreeMap, HashMap},
7 fmt::Debug,
8 rc::Rc,
9};
105
11#[derive(Debug)]6#[derive(Debug)]
18#[derive(Debug)]13#[derive(Debug)]
19pub struct ObjValueInternals {14pub struct ObjValueInternals {
20 super_obj: Option<ObjValue>,15 super_obj: Option<ObjValue>,
21 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,16 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,
22 value_cache: RefCell<HashMap<Rc<str>, Val>>,17 value_cache: RefCell<HashMap<Rc<str>, Val>>,
23}18}
24#[derive(Clone)]19#[derive(Clone)]
44impl ObjValue {39impl ObjValue {
45 pub fn new(40 pub fn new(
46 super_obj: Option<ObjValue>,41 super_obj: Option<ObjValue>,
47 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,42 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,
48 ) -> ObjValue {43 ) -> ObjValue {
49 ObjValue(Rc::new(ObjValueInternals {44 ObjValue(Rc::new(ObjValueInternals {
50 super_obj,45 super_obj,
53 }))48 }))
54 }49 }
55 pub fn new_empty() -> ObjValue {50 pub fn new_empty() -> ObjValue {
56 Self::new(None, Rc::new(BTreeMap::new()))51 Self::new(None, Rc::new(HashMap::new()))
57 }52 }
58 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {53 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {
59 match &self.0.super_obj {54 match &self.0.super_obj {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -246,7 +246,8 @@
 		}
 		Val::Obj(obj) => {
 			buf.push_str("{\n");
-			let fields = obj.visible_fields();
+			let mut fields = obj.visible_fields();
+			fields.sort();
 			if !fields.is_empty() {
 				let old_len = cur_padding.len();
 				cur_padding.push_str(padding);