From c1a6c6ac4824d47790334fce0e25644aeaecd548 Mon Sep 17 00:00:00 2001 From: Лач Date: Fri, 26 Jun 2020 09:33:15 +0000 Subject: [PATCH] perf: Rc file name --- --- a/crates/jsonnet-evaluator/build.rs +++ b/crates/jsonnet-evaluator/build.rs @@ -15,7 +15,7 @@ let parsed = parse( STDLIB_STR, &ParserSettings { - file_name: PathBuf::from("std.jsonnet"), + file_name: Rc::new(PathBuf::from("std.jsonnet")), loc_data: true, }, ) --- a/crates/jsonnet-evaluator/src/error.rs +++ b/crates/jsonnet-evaluator/src/error.rs @@ -38,7 +38,7 @@ } #[derive(Clone, Debug)] -pub struct StackTraceElement(pub Rc, pub String); +pub struct StackTraceElement(pub ExprLocation, pub String); #[derive(Debug, Clone)] pub struct StackTrace(pub Vec); --- a/crates/jsonnet-evaluator/src/lib.rs +++ b/crates/jsonnet-evaluator/src/lib.rs @@ -117,7 +117,7 @@ parse( &code, &ParserSettings { - file_name: name, + file_name: Rc::new(name), loc_data: true, }, )?, @@ -198,7 +198,7 @@ let parsed = parse( &code, &ParserSettings { - file_name: PathBuf::from("raw.jsonnet"), + file_name: Rc::new(PathBuf::from("raw.jsonnet")), loc_data: true, }, ) @@ -320,11 +320,11 @@ let state = EvaluationState::default(); state .push( - Rc::new(ExprLocation(PathBuf::from("test1.jsonnet"), 10, 20)), + ExprLocation(Rc::new(PathBuf::from("test1.jsonnet")), 10, 20), "outer".to_owned(), || { state.push( - Rc::new(ExprLocation(PathBuf::from("test2.jsonnet"), 30, 40)), + ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40), "inner".to_owned(), || { state.print_stack_trace(); --- a/crates/jsonnet-parser/src/expr.rs +++ b/crates/jsonnet-parser/src/expr.rs @@ -207,7 +207,7 @@ /// file, begin offset, end offset #[derive(Clone, PartialEq, Serialize, Deserialize)] -pub struct ExprLocation(pub PathBuf, pub usize, pub usize); +pub struct ExprLocation(pub Rc, pub usize, pub usize); impl Debug for ExprLocation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2) @@ -216,7 +216,7 @@ /// Holds AST expression and its location in source file+ #[derive(Clone, PartialEq, Serialize, Deserialize)] -pub struct LocExpr(pub Rc, pub Option>); +pub struct LocExpr(pub Rc, pub Option); impl Debug for LocExpr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?} from {:?}", self.0, self.1) @@ -230,11 +230,7 @@ LocExpr( std::rc::Rc::new($expr), if $need_loc { - Some(std::rc::Rc::new(ExprLocation( - $name.to_owned(), - $start, - $end, - ))) + Some(ExprLocation($name, $start, $end)) } else { None }, --- a/crates/jsonnet-parser/src/lib.rs +++ b/crates/jsonnet-parser/src/lib.rs @@ -11,7 +11,7 @@ pub struct ParserSettings { pub loc_data: bool, - pub file_name: PathBuf, + pub file_name: Rc, } parser! { @@ -289,7 +289,7 @@ } end:position!() { let LocExpr(e, _) = a; LocExpr(e, if s.loc_data { - Some(Rc::new(ExprLocation(s.file_name.to_owned(), start, end))) + Some(ExprLocation(s.file_name.clone(), start, end)) } else { None }) @@ -317,6 +317,7 @@ use super::{expr::*, parse}; use crate::ParserSettings; use std::path::PathBuf; + use std::rc::Rc; macro_rules! parse { ($s:expr) => { @@ -324,7 +325,7 @@ $s, &ParserSettings { loc_data: false, - file_name: PathBuf::from("/test.jsonnet"), + file_name: Rc::new(PathBuf::from("/test.jsonnet")), }, ) .unwrap() -- gitstuff