Skip to content

Commit d878ca2

Browse files
committed
trying to index using the pointers offset to win speed
1 parent 19a1020 commit d878ca2

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

modules/mexpreval/src/lib.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::str::FromStr;
77
use std::{ffi::CStr, os::raw::c_char, slice};
88

99
thread_local! {
10-
static EXPRS: RefCell<Vec<(*const c_char, Expr)>> = RefCell::new(Vec::new());
10+
static EXPRS: RefCell<Vec<Expr>> = RefCell::new(Vec::new());
1111
static CONTEXT: RefCell<Context<'static>> = RefCell::new(Context::new());
1212
}
1313

@@ -45,21 +45,25 @@ pub unsafe extern "C" fn eval_f_exprs(
4545
ctx.var(format!("p{}", i + 1), *param);
4646
}
4747

48-
for (i, ptr) in exprs.iter().enumerate() {
49-
let expr = exprs_cache.iter().find(|e| e.0 == *ptr);
50-
51-
let expr = match expr {
52-
Some(a) => &a.1,
53-
None => {
54-
let c_str = CStr::from_ptr(*ptr);
55-
let s = c_str.to_str().unwrap();
56-
let expr = Expr::from_str(s).unwrap_or_else(|e| panic!("Error parsing expresion {}: {}", s, e));
48+
if exprs_cache.is_empty() {
49+
let c_str = CStr::from_ptr(exprs[0]);
50+
let s = c_str.to_str().unwrap();
51+
let expr = Expr::from_str(s).unwrap_or_else(|e| panic!("Error parsing expresion {}: {}", s, e));
52+
exprs_cache.push(expr);
53+
}
5754

58-
exprs_cache.push((*ptr, expr));
59-
&exprs_cache.get(exprs_cache.len() - 1).unwrap().1
60-
}
61-
};
55+
for (i, ptr) in exprs.iter().enumerate() {
56+
let index = ptr.offset_from(exprs[0]) as usize;
57+
58+
if exprs.len() < index {
59+
let c_str = CStr::from_ptr(*ptr);
60+
let s = c_str.to_str().unwrap();
61+
let expr = Expr::from_str(s).unwrap_or_else(|e| panic!("Error parsing expresion {}: {}", s, e));
6262

63+
exprs_cache.push(expr);
64+
}
65+
66+
let expr = &exprs_cache[index];
6367

6468
ydot[i] = expr
6569
.eval_with_context(&*ctx)

0 commit comments

Comments
 (0)