Skip to content

Commit

Permalink
Remove an expensive heap allocation (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Tollerud <[email protected]>
  • Loading branch information
Cadair and eteq authored Jun 17, 2024
1 parent f2a9798 commit 9d08a28
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ float_eq = "1.0.0"
[lints.clippy]
needless_return = "allow"
implicit_return = "warn"

[profile.release]
codegen-units = 1
2 changes: 1 addition & 1 deletion benchmarks/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
grid_spacing = [1, 2, 3]
grid = VectorGrid(field, grid_spacing)

seedlist = 2 ** np.arange(12)
seedlist = 2 ** np.arange(17)
times = []
for nseeds in seedlist:
dts = []
Expand Down
13 changes: 7 additions & 6 deletions src/interp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Helper functions for interpolation.

use numpy::ndarray::{Array, Array1, ArrayBase, Data, Ix3};
use numpy::ndarray::{Array1, ArrayBase, Data, Ix3};

/// Trilinear-interpolation of a scalar defined on
/// the eight corners of a cuboid.
Expand All @@ -18,20 +18,21 @@ where
}
let m_x = 1. - x;

let mut c = Array::zeros((2, 2));
let mut c: [f64; 4] = [0.0; 4];
// Interpolate over x
for iy in 0..2 {
for iz in 0..2 {
c[[iy, iz]] = values[[0, iy, iz]] * m_x[[0]] + values[[1, iy, iz]] * x[[0]];
let iix = (2 * iy) + iz;
c[iix] = values[[0, iy, iz]] * m_x[[0]] + values[[1, iy, iz]] * x[[0]];
}
}

// Interpolate over y
let mut c1 = Array::zeros(2);
let mut c1: [f64; 2] = [0.0; 2];
for iz in 0..2 {
c1[[iz]] = c[[0, iz]] * m_x[[1]] + c[[1, iz]] * x[[1]];
c1[iz] = c[iz] * m_x[[1]] + c[iz + 2] * x[[1]];
}

// Interpolate over z
return c1[[0]] * m_x[[2]] + c1[[1]] * x[[2]];
return c1[0] * m_x[[2]] + c1[1] * x[[2]];
}

0 comments on commit 9d08a28

Please sign in to comment.