git.delta.rocks / jrsonnet / refs/commits / b0a4791f1236

difftreelog

perf cache object member accessors by this

Лач2020-07-19parent: #226d3aa.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
786 .super_obj()786 .super_obj()
787 .clone()787 .clone()
788 .expect("no super found")788 .expect("no super found")
789 .get_raw(&name, &context.this().clone().expect("no this found"))?789 .get_raw(name, &context.this().clone().expect("no this found"))?
790 .expect("value not found")790 .expect("value not found")
791 }791 }
792 Index(value, index) => {792 Index(value, index) => {
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
15pub struct ObjValueInternals {15pub struct ObjValueInternals {
16 super_obj: Option<ObjValue>,16 super_obj: Option<ObjValue>,
17 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,17 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,
18 value_cache: RefCell<HashMap<Rc<str>, Val>>,18 value_cache: RefCell<HashMap<(Rc<str>, usize), Option<Val>>>,
19}19}
20#[derive(Clone)]20#[derive(Clone)]
21pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);21pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);
96 visible_fields96 visible_fields
97 }97 }
98 pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {98 pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {
99 if let Some(v) = self.0.value_cache.borrow().get(&key) {
100 return Ok(Some(v.clone()));
101 }
102 if let Some(v) = self.get_raw(&key, self)? {99 Ok(self.get_raw(key, self)?)
103 let v = v.unwrap_if_lazy()?;
104 self.0.value_cache.borrow_mut().insert(key, v.clone());
105 Ok(Some(v))
106 } else {
107 Ok(None)
108 }
109 }100 }
110 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {101 pub(crate) fn get_raw(&self, key: Rc<str>, real_this: &ObjValue) -> Result<Option<Val>> {
102 let cache_key = (key.clone(), Rc::as_ptr(&real_this.0) as usize);
103
104 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
105 return Ok(v.clone());
106 }
111 match (self.0.this_entries.get(key), &self.0.super_obj) {107 let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {
112 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),108 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),
113 (Some(k), Some(s)) => {109 (Some(k), Some(s)) => {
114 let our = self.evaluate_this(k, real_this)?;110 let our = self.evaluate_this(k, real_this)?;
123 }119 }
124 (None, Some(s)) => s.get_raw(key, real_this),120 (None, Some(s)) => s.get_raw(key, real_this),
125 (None, None) => Ok(None),121 (None, None) => Ok(None),
126 }122 }?;
123 self.0
124 .value_cache
125 .borrow_mut()
126 .insert(cache_key, value.clone());
127 Ok(value)
127 }128 }
128 fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {129 fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {
129 Ok(v.invoke130 Ok(v.invoke