difftreelog
perf(evaluator) use hashmap for object body
in: master
4 files changed
bindings/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 {
crates/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
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_parser::Visibility;4use std::{5 cell::RefCell,6 collections::{BTreeMap, HashMap},7 fmt::Debug,8 rc::Rc,9};1011#[derive(Debug)]12pub struct ObjMember {13 pub add: bool,14 pub visibility: Visibility,15 pub invoke: LazyBinding,16}1718#[derive(Debug)]19pub struct ObjValueInternals {20 super_obj: Option<ObjValue>,21 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,22 value_cache: RefCell<HashMap<Rc<str>, Val>>,23}24#[derive(Clone)]25pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);26impl Debug for ObjValue {27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {28 if let Some(super_obj) = self.0.super_obj.as_ref() {29 if f.alternate() {30 write!(f, "{:#?}", super_obj)?;31 } else {32 write!(f, "{:?}", super_obj)?;33 }34 write!(f, " + ")?;35 }36 let mut debug = f.debug_struct("ObjValue");37 for (name, member) in self.0.this_entries.iter() {38 debug.field(name, member);39 }40 debug.finish_non_exhaustive()41 }42}4344impl ObjValue {45 pub fn new(46 super_obj: Option<ObjValue>,47 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,48 ) -> ObjValue {49 ObjValue(Rc::new(ObjValueInternals {50 super_obj,51 this_entries,52 value_cache: RefCell::new(HashMap::new()),53 }))54 }55 pub fn new_empty() -> ObjValue {56 Self::new(None, Rc::new(BTreeMap::new()))57 }58 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {59 match &self.0.super_obj {60 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),61 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),62 }63 }64 pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {65 if let Some(s) = &self.0.super_obj {66 s.enum_fields(handler);67 }68 for (name, member) in self.0.this_entries.iter() {69 handler(&name, &member.visibility);70 }71 }72 pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {73 let out = Rc::new(RefCell::new(IndexMap::new()));74 self.enum_fields(&|name, visibility| {75 let mut out = out.borrow_mut();76 match visibility {77 Visibility::Normal => {78 if !out.contains_key(name) {79 out.insert(name.to_owned(), true);80 }81 }82 Visibility::Hidden => {83 out.insert(name.to_owned(), false);84 }85 Visibility::Unhide => {86 out.insert(name.to_owned(), true);87 }88 };89 });90 Rc::try_unwrap(out).unwrap().into_inner()91 }92 pub fn visible_fields(&self) -> Vec<Rc<str>> {93 self.fields_visibility()94 .into_iter()95 .filter(|(_k, v)| *v)96 .map(|(k, _)| k)97 .collect()98 }99 pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {100 if let Some(v) = self.0.value_cache.borrow().get(&key) {101 return Ok(Some(v.clone()));102 }103 if let Some(v) = self.get_raw(&key, self)? {104 let v = v.unwrap_if_lazy()?;105 self.0.value_cache.borrow_mut().insert(key, v.clone());106 Ok(Some(v))107 } else {108 Ok(None)109 }110 }111 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {112 match (self.0.this_entries.get(key), &self.0.super_obj) {113 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),114 (Some(k), Some(s)) => {115 let our = self.evaluate_this(k, real_this)?;116 if k.add {117 s.get_raw(key, real_this)?118 .map_or(Ok(Some(our.clone())), |v| {119 Ok(Some(evaluate_add_op(&v, &our)?))120 })121 } else {122 Ok(Some(our))123 }124 }125 (None, Some(s)) => s.get_raw(key, real_this),126 (None, None) => Ok(None),127 }128 }129 fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {130 Ok(v.invoke131 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?132 .evaluate()?)133 }134}135impl PartialEq for ObjValue {136 fn eq(&self, other: &Self) -> bool {137 Rc::ptr_eq(&self.0, &other.0)138 }139}1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_parser::{ExprLocation, Visibility};4use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};56#[derive(Debug)]7pub struct ObjMember {8 pub add: bool,9 pub visibility: Visibility,10 pub invoke: LazyBinding,11}1213#[derive(Debug)]14pub struct ObjValueInternals {15 super_obj: Option<ObjValue>,16 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,17 value_cache: RefCell<HashMap<Rc<str>, Val>>,18}19#[derive(Clone)]20pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);21impl Debug for ObjValue {22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {23 if let Some(super_obj) = self.0.super_obj.as_ref() {24 if f.alternate() {25 write!(f, "{:#?}", super_obj)?;26 } else {27 write!(f, "{:?}", super_obj)?;28 }29 write!(f, " + ")?;30 }31 let mut debug = f.debug_struct("ObjValue");32 for (name, member) in self.0.this_entries.iter() {33 debug.field(name, member);34 }35 debug.finish_non_exhaustive()36 }37}3839impl ObjValue {40 pub fn new(41 super_obj: Option<ObjValue>,42 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,43 ) -> ObjValue {44 ObjValue(Rc::new(ObjValueInternals {45 super_obj,46 this_entries,47 value_cache: RefCell::new(HashMap::new()),48 }))49 }50 pub fn new_empty() -> ObjValue {51 Self::new(None, Rc::new(HashMap::new()))52 }53 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {54 match &self.0.super_obj {55 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),56 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),57 }58 }59 pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {60 if let Some(s) = &self.0.super_obj {61 s.enum_fields(handler);62 }63 for (name, member) in self.0.this_entries.iter() {64 handler(&name, &member.visibility);65 }66 }67 pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {68 let out = Rc::new(RefCell::new(IndexMap::new()));69 self.enum_fields(&|name, visibility| {70 let mut out = out.borrow_mut();71 match visibility {72 Visibility::Normal => {73 if !out.contains_key(name) {74 out.insert(name.to_owned(), true);75 }76 }77 Visibility::Hidden => {78 out.insert(name.to_owned(), false);79 }80 Visibility::Unhide => {81 out.insert(name.to_owned(), true);82 }83 };84 });85 Rc::try_unwrap(out).unwrap().into_inner()86 }87 pub fn visible_fields(&self) -> Vec<Rc<str>> {88 self.fields_visibility()89 .into_iter()90 .filter(|(_k, v)| *v)91 .map(|(k, _)| k)92 .collect()93 }94 pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {95 if let Some(v) = self.0.value_cache.borrow().get(&key) {96 return Ok(Some(v.clone()));97 }98 if let Some(v) = self.get_raw(&key, self)? {99 let v = v.unwrap_if_lazy()?;100 self.0.value_cache.borrow_mut().insert(key, v.clone());101 Ok(Some(v))102 } else {103 Ok(None)104 }105 }106 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {107 match (self.0.this_entries.get(key), &self.0.super_obj) {108 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),109 (Some(k), Some(s)) => {110 let our = self.evaluate_this(k, real_this)?;111 if k.add {112 s.get_raw(key, real_this)?113 .map_or(Ok(Some(our.clone())), |v| {114 Ok(Some(evaluate_add_op(&v, &our)?))115 })116 } else {117 Ok(Some(our))118 }119 }120 (None, Some(s)) => s.get_raw(key, real_this),121 (None, None) => Ok(None),122 }123 }124 fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {125 Ok(v.invoke126 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?127 .evaluate()?)128 }129}130impl PartialEq for ObjValue {131 fn eq(&self, other: &Self) -> bool {132 Rc::ptr_eq(&self.0, &other.0)133 }134}crates/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);