git.delta.rocks / jrsonnet / refs/commits / 64f3674f18c4

difftreelog

perf specialize super.field gets

Yaroslav Bolyukin2022-11-20parent: #30b370e.patch.diff
in: master

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
139}139}
140140
141fn main_real(s: &State, opts: Opts) -> Result<(), Error> {141fn main_real(s: &State, opts: Opts) -> Result<(), Error> {
142 let (_stack_guard, tla, _gc_guard) = opts.general.configure(s)?;142 let (tla, _gc_guard) = opts.general.configure(s)?;
143 let manifest_format = opts.manifest.configure(s)?;143 let manifest_format = opts.manifest.configure(s)?;
144144
145 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;145 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
6use std::{env, marker::PhantomData, path::PathBuf};6use std::{env, marker::PhantomData, path::PathBuf};
77
8use clap::Parser;8use clap::Parser;
9use jrsonnet_evaluator::{9use jrsonnet_evaluator::{error::Result, stack::set_stack_depth_limit, FileImportResolver, State};
10 error::Result, stack::StackDepthLimitOverrideGuard, FileImportResolver, State,
11};
12use jrsonnet_gcmodule::with_thread_object_space;10use jrsonnet_gcmodule::with_thread_object_space;
13pub use manifest::*;11pub use manifest::*;
48 jpath: Vec<PathBuf>,46 jpath: Vec<PathBuf>,
49}47}
50impl ConfigureState for MiscOpts {48impl ConfigureState for MiscOpts {
51 type Guards = StackDepthLimitOverrideGuard;49 type Guards = ();
52 fn configure(&self, s: &State) -> Result<Self::Guards> {50 fn configure(&self, s: &State) -> Result<Self::Guards> {
53 let mut library_paths = self.jpath.clone();51 let mut library_paths = self.jpath.clone();
54 library_paths.reverse();52 library_paths.reverse();
5856
59 s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));57 s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
6058
61 let _depth_limit = jrsonnet_evaluator::stack::limit_stack_depth(self.max_stack);59 set_stack_depth_limit(self.max_stack);
62 Ok(_depth_limit)60 Ok(())
63 }61 }
64}62}
6563
8179
82impl ConfigureState for GeneralOpts {80impl ConfigureState for GeneralOpts {
83 type Guards = (81 type Guards = (
84 <MiscOpts as ConfigureState>::Guards,
85 <TlaOpts as ConfigureState>::Guards,82 <TlaOpts as ConfigureState>::Guards,
86 <GcOpts as ConfigureState>::Guards,83 <GcOpts as ConfigureState>::Guards,
87 );84 );
88 fn configure(&self, s: &State) -> Result<Self::Guards> {85 fn configure(&self, s: &State) -> Result<Self::Guards> {
89 // Configure trace first, because tla-code/ext-code can throw86 // Configure trace first, because tla-code/ext-code can throw
90 let misc_guards = self.misc.configure(s)?;87 self.misc.configure(s)?;
91 let tla_guards = self.tla.configure(s)?;88 let tla_guards = self.tla.configure(s)?;
92 self.std.configure(s)?;89 self.std.configure(s)?;
93 let gc_guards = self.gc.configure(s)?;90 let gc_guards = self.gc.configure(s)?;
94 Ok((misc_guards, tla_guards, gc_guards))91 Ok((tla_guards, gc_guards))
95 }92 }
96}93}
9794
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
389 ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(389 ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
390 ctx.this()390 ctx.this()
391 .clone()391 .clone()
392 .expect("if super exists - then this should to"),392 .expect("if super exists - then this should too"),
393 ),393 ),
394 ),394 ),
395 Literal(LiteralType::Dollar) => {395 Literal(LiteralType::Dollar) => {
408 || format!("variable <{name}> access"),408 || format!("variable <{name}> access"),
409 || ctx.binding(name.clone())?.evaluate(),409 || ctx.binding(name.clone())?.evaluate(),
410 )?,410 )?,
411 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
412 let name = evaluate(ctx.clone(), index)?;
413 let Val::Str(name) = name else {
414 throw!(ValueIndexMustBeTypeGot(
415 ValType::Obj,
416 ValType::Str,
417 name.value_type(),
418 ))
419 };
420 ctx.super_obj()
421 .clone()
422 .expect("no super found")
423 .get_for(name, ctx.this().clone().expect("no this found"))?
424 .expect("value not found")
425 }
411 Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {426 Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {
412 (Val::Obj(v), Val::Str(key)) => State::push(427 (Val::Obj(v), Val::Str(key)) => State::push(
413 CallLocation::new(loc),428 CallLocation::new(loc),
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
128 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,128 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,
129 assertions_ran: RefCell<GcHashSet<ObjValue>>,129 assertions_ran: RefCell<GcHashSet<ObjValue>>,
130 this_entries: Cc<GcHashMap<IStr, ObjMember>>,130 this_entries: Cc<GcHashMap<IStr, ObjMember>>,
131 value_cache: RefCell<GcHashMap<IStr, CacheValue>>,131 value_cache: RefCell<GcHashMap<(IStr, Option<WeakObjValue>), CacheValue>>,
132}132}
133133
134#[derive(Clone, Trace)]134#[derive(Clone, Trace)]
387387
388 pub fn get(&self, key: IStr) -> Result<Option<Val>> {388 pub fn get(&self, key: IStr) -> Result<Option<Val>> {
389 self.run_assertions()?;389 self.run_assertions()?;
390 let cache_key = (key.clone(), None);
390 if let Some(v) = self.0.value_cache.borrow().get(&key) {391 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
391 return Ok(match v {392 return Ok(match v {
392 CacheValue::Cached(v) => Some(v.clone()),393 CacheValue::Cached(v) => Some(v.clone()),
393 CacheValue::NotFound => None,394 CacheValue::NotFound => None,
398 self.0399 self.0
399 .value_cache400 .value_cache
400 .borrow_mut()401 .borrow_mut()
401 .insert(key.clone(), CacheValue::Pending);402 .insert(cache_key.clone(), CacheValue::Pending);
402 let value = self403 let value = self
403 .get_raw(404 .get_raw(key, self.0.this.clone().unwrap_or_else(|| self.clone()))
404 key.clone(),
405 self.0.this.clone().unwrap_or_else(|| self.clone()),
406 )
407 .map_err(|e| {405 .map_err(|e| {
408 self.0406 self.0
409 .value_cache407 .value_cache
410 .borrow_mut()408 .borrow_mut()
411 .insert(key.clone(), CacheValue::Errored(e.clone()));409 .insert(cache_key.clone(), CacheValue::Errored(e.clone()));
412 e410 e
413 })?;411 })?;
414 self.0.value_cache.borrow_mut().insert(412 self.0.value_cache.borrow_mut().insert(
415 key,413 cache_key,
416 value414 value
417 .as_ref()415 .as_ref()
418 .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),416 .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),
419 );417 );
420 Ok(value)418 Ok(value)
421 }419 }
420 pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {
421 self.run_assertions()?;
422 let cache_key = (key.clone(), Some(this.clone().downgrade()));
423 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
424 return Ok(match v {
425 CacheValue::Cached(v) => Some(v.clone()),
426 CacheValue::NotFound => None,
427 CacheValue::Pending => throw!(InfiniteRecursionDetected),
428 CacheValue::Errored(e) => return Err(e.clone()),
429 });
430 }
431 self.0
432 .value_cache
433 .borrow_mut()
434 .insert(cache_key.clone(), CacheValue::Pending);
435 let value = self.get_raw(key, this).map_err(|e| {
436 self.0
437 .value_cache
438 .borrow_mut()
439 .insert(cache_key.clone(), CacheValue::Errored(e.clone()));
440 e
441 })?;
442 self.0.value_cache.borrow_mut().insert(
443 cache_key,
444 value
445 .as_ref()
446 .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),
447 );
448 Ok(value)
449 }
422450
423 fn get_raw(&self, key: IStr, real_this: Self) -> Result<Option<Val>> {451 fn get_raw(&self, key: IStr, real_this: Self) -> Result<Option<Val>> {
424 match (self.0.this_entries.get(&key), &self.0.sup) {452 match (self.0.this_entries.get(&key), &self.0.sup) {