Skip to content

Commit 59960ef

Browse files
committed
Adds caching
1 parent a89da55 commit 59960ef

File tree

3 files changed

+236
-5
lines changed

3 files changed

+236
-5
lines changed

Cargo.lock

Lines changed: 227 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ lalrpop-util = "0.19.7"
1818
regex = "1"
1919
num-complex = "0.4.3"
2020
physical_constants = "0.5.0"
21-
errorfunctions = "0.2.0"
21+
errorfunctions = "0.2.0"
22+
cached = "0.43.0"

src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::ast::Expr;
22
use crate::ast::ExprParams;
33
use ast::EvaluateResult;
4+
use cached::proc_macro::cached;
45
use lalrpop_util::lexer::Token;
56
use lalrpop_util::ParseError;
67
use num_complex::Complex64;
@@ -83,7 +84,10 @@ fn basic_execution_test() {
8384
assert!(formula_parser::FormulaParser::new().parse("(22)").is_err());
8485
}
8586

86-
fn parse_ast<'a>(formula: &'a str) -> Result<Box<Expr>, ParseError<usize, Token<'a>, &'a str>> {
87+
#[cached]
88+
fn parse_ast(
89+
formula: &'static str,
90+
) -> Result<Box<Expr<'static>>, ParseError<usize, Token<'static>, &'static str>> {
8791
formula_parser::FormulaParser::new().parse(formula)
8892
}
8993

@@ -94,7 +98,7 @@ fn parse<'a>(
9498
single_params: &'a HashMap<&str, f64>,
9599
rep_params: &'a HashMap<&str, Vec<f64>>,
96100
) -> Result<Array1<Complex64>, Box<dyn error::Error + 'a>> {
97-
let ast = parse_ast(formula)?;
101+
let ast = parse_ast(Box::leak(formula.to_string().into_boxed_str()))?;
98102
match ast.evaluate(&mut ExprParams {
99103
x_axis_name: &x_axis_name,
100104
x_axis_values: &x_axis_values,
@@ -114,7 +118,7 @@ fn formula_dispersion(_py: Python, m: &PyModule) -> PyResult<()> {
114118
#[pyfn(m)]
115119
#[pyo3(name = "get_representation")]
116120
fn get_representation_py<'py>(formula: &str) -> PyResult<&'py str> {
117-
let ast = match parse_ast(formula) {
121+
let ast = match parse_ast(Box::leak(formula.to_string().into_boxed_str())) {
118122
Ok(ast) => ast,
119123
Err(err) => return Err(PyErr::new::<PyTypeError, _>(err.to_string())),
120124
};

0 commit comments

Comments
 (0)