Skip to content

Commit 710e782

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

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

modules/mexpreval/src/lib.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ thread_local! {
1111
static CONTEXT: RefCell<Context<'static>> = RefCell::new(Context::new());
1212
}
1313

14+
static Y: [&str; 8] = ["y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8"];
15+
static P: [&str; 8] = ["p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8"];
16+
1417
#[allow(clippy::missing_safety_doc)]
1518
#[no_mangle]
1619
pub unsafe extern "C" fn eval_f_exprs(
@@ -38,31 +41,24 @@ pub unsafe extern "C" fn eval_f_exprs(
3841
let mut ctx = ctx.borrow_mut();
3942

4043
for (i, expr) in y.iter().enumerate() {
41-
ctx.var(format!("y{}", i + 1), *expr);
44+
ctx.var(Y[i], *expr);
4245
}
4346

4447
for (i, param) in params.iter().enumerate() {
45-
ctx.var(format!("p{}", i + 1), *param);
46-
}
47-
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);
48+
ctx.var(P[i], *param);
5349
}
5450

5551
for (i, ptr) in exprs.iter().enumerate() {
56-
let index = ptr.offset_from(exprs[0]) as usize;
57-
58-
if exprs.len() < index {
52+
let index = exprs[0].offset_from(*ptr) as usize;
53+
54+
if exprs_cache.len() <= index {
5955
let c_str = CStr::from_ptr(*ptr);
6056
let s = c_str.to_str().unwrap();
6157
let expr = Expr::from_str(s).unwrap_or_else(|e| panic!("Error parsing expresion {}: {}", s, e));
6258

6359
exprs_cache.push(expr);
6460
}
65-
61+
6662
let expr = &exprs_cache[index];
6763

6864
ydot[i] = expr

0 commit comments

Comments
 (0)