WritingPerformance engineering

Cache the geometry, then change the values

How I accelerate CorneaForge’s geometry engine with reusable interpolation, batched surface fits, and directly evaluated derivatives.

Updated

In this article

I implemented CorneaForge’s polar-to-Cartesian interpolation so that maps sharing a sampling grid can reuse the same geometric calculation. The code constructs a triangulation, finds the triangle containing each output pixel, and stores three source indices and three interpolation weights. Applying the next map becomes a gather, multiplication, and sum.

I built this optimization into the map-generation pipeline behind OphtaFlow AI, the real-time diagnostic support platform I operate 24/7, currently in beta with physicians. Different maps often share the same rings and angles while carrying different measurements. Rebuilding their geometry each time repeats work that the changing values do not affect.

The engineering problem is deciding exactly what can be reused. Missing measurements change the point set; output resolution changes the tables; and retaining many tables consumes memory. I made those dependencies explicit in the cache design.

One triangle, two maps

Take three source positions, A = (0, 0), B = (1, 0), and C = (0, 1). We want a value at q = (0.25, 0.25).

The point has barycentric weights 0.5, 0.25, and 0.25 relative to those vertices. If the source values are 10, 20, and 40, interpolation gives:

0.5 × 10 + 0.25 × 20 + 0.25 × 40 = 20

Now change the values to 20, 30, and 50:

0.5 × 20 + 0.25 × 30 + 0.25 × 50 = 30

The weights describe the positions of the vertices and the query point. Both maps therefore use the same weights, computed before either set of values arrives.

Try changing the values below, then remove vertex C. Changing a value reuses the triangle; removing a vertex changes the geometry itself.

Change the measurements, keep the weightsSynthetic worked example
q = (¼, ¼) A (0, 0)B (1, 0)C (0, 1)

0.50 × 10 + 0.25 × 20 + 0.25 × 40 = 20.00

The weights stay fixed while measurements change. Removing a valid vertex changes the interpolation support, so this cached result can no longer be used.

Store the reusable operator

For a complete image, repeat the geometric construction for every output location. Each valid output point needs three source indices and three weights. Together, they define an operation:

y = W x

Here x contains the source values and y the interpolated outputs. W describes the geometry. For linear interpolation within a two-dimensional triangulation, each valid row refers to at most three source vertices.

I store this sparse operator as three indices and three weights per output point:

# Built once for a compatible geometry:
# indices: (n_outputs, 3), integer source indices
# weights: (n_outputs, 3), interpolation weights
# valid:   (n_outputs,), points admitted by the geometry

# Applied to each new map:
# values: (n_sources,)
result = np.full(indices.shape[0], np.nan, dtype=np.float64)
selected = values[indices[valid]]
result[valid] = (selected * weights[valid]).sum(axis=1)

This application stage uses tables built for a specific source ordering and output grid. The valid mask selects the output points covered by the triangulation; the others retain NaN.

SciPy permits passing a precomputed triangulation to LinearNDInterpolator. With fixed output locations, we can reuse more: the triangle selected for each output and its barycentric weights. SciPy exposes the required triangle lookup and coordinate transforms in its Delaunay API.

The resulting hot path is a gather, multiplication, and reduction: triangle construction, lookup, and weight calculation have moved out of repeated map processing.

Reuse changes the cost model

Let building the geometric tables cost C, and applying them to one map cost A. For K maps, rebuilding every time costs:

K(C + A)

Building once and reusing the tables costs approximately:

C + KA

This model describes a shared geometry, with cache-management overhead omitted. In a constructed example with C = 10, A = 1, and K = 10 arbitrary time units, rebuilding costs 110 and reuse costs 20.

The first map pays for construction. Each subsequent map pays for applying the operator. In CorneaForge, that is the opportunity created by processing several measurements sampled on the same device grid.

Include missing measurements in the cache key

CorneaForge uses a fixed angular grid. The number of radial rings and the target resolution determine the coordinate grids. For complete maps, those dimensions identify the reusable operator.

A missing source value changes the available point set. I build the triangulation from finite observations and include their exact validity mask in the cache key. Maps with the same missing points can share an operator; a new pattern gets its own triangulation and weights.

Removing vertex C from our example leaves no triangle spanning the query. With more observations, the remaining points can form new triangles across a gap. The interpolation follows that new geometry, including its wider spacing. This is why the validity pattern belongs in the key alongside the grid dimensions.

Source ordering is part of the operator too: each stored index must continue to identify the same point. In a system with freely moving coordinates, the key would also need to identify those coordinates and the boundary rules. CorneaForge’s fixed device grid makes the smaller key sufficient.

A cache has a memory budget

Each distinct missing-value pattern retains another set of interpolation tables. I bounded this cache to 128 entries with a least-recently-used policy: when the cache fills, the entry that has gone unused longest is replaced. That keeps recurring patterns available while limiting memory growth during long processing runs.

The table layout gives a direct memory budget. Three 32-bit indices and three 64-bit weights require 12 + 24 = 36 bytes per output location. Add two one-byte masks and the table storage is about 38 bytes per location. At 512 × 512, that is roughly 9.50 MiB per entry. Keeping 128 such entries retains about 1.19 GiB for those arrays alone, before object overhead and temporary working arrays.

A saved benchmark from 31 August 2026, using 128 generated missing-value patterns at 512 × 512, measured exactly 1,275,068,416 bytes, or 1.19 GiB, in cached arrays. The measurement confirms the allocation calculated from the array layout and makes the entry limit a concrete deployment choice.

Keep the interpolation rule intact

The cached operator implements the same piecewise linear calculation as its uncached counterpart. Coordinates, source ordering, validity masks and numerical precision define the comparison. A planar function gives a simple numerical check because barycentric interpolation reproduces it inside each triangle. For a curved function, the two implementations should produce the same interpolated values.

The cache design also identifies the performance measurements that matter: construction time for a new geometry, application time for a reused geometry, and the frequency of each in a sequence of maps. The linked benchmark records cold construction and retained allocation for distinct masks.

Batch the small problems

Interpolation is one part of the geometry engine I built. The optical calculations also need surface heights and slopes at thousands of ray positions. A local quadratic fit gives both: near a query point, approximate the surface as

z(dx, dy) = a + b dx + c dy + d dx² + e dx dy + f dy²

Here dx and dy are measured from the query point. At that point, the fitted height is a and the two slopes are b and c. Six coefficients describe a small patch of surface, including how it bends.

The straightforward implementation finds nearby samples and solves one least-squares problem for each query. The complication is that neighbourhoods have different sizes. A point in a dense region might have 30 neighbours; one near an edge might have 12. Putting every neighbourhood into a single rectangular array would waste space on padding.

I sort queries by their number of neighbours and process them in chunks of up to 256. Each chunk is padded only to its own largest neighbourhood. Padding contributes zeros to both the design matrix and the observed heights, so it contributes nothing to the fit. This keeps memory bounded while turning many small solves into a batch.

For each neighbourhood, the design matrix has six columns: 1, dx, dy, dx², dx dy, and dy². Stacking those matrices gives shape (batch, neighbours, 6). The main calculation becomes:

# A: (batch, neighbours, 6); z: (batch, neighbours)
# Padded rows are zero in both arrays.
gram = np.einsum("bki,bkj->bij", A, A)
rhs = np.einsum("bki,bk->bi", A, z)
gram[:, np.arange(6), np.arange(6)] += regularization
coefficients = np.linalg.solve(gram, rhs[..., None])[..., 0]

gram contains one 6 × 6 system per query. NumPy’s batched linear solver handles the stack. The small diagonal term is regularization: it stabilizes a fit when the available sample positions leave some directions poorly constrained. Centering the coordinates also keeps the local polynomial tied to the point being evaluated.

The useful change is the unit of work. Python organizes neighbourhoods; array operations carry out the repeated algebra. The same technique applies to local image models, point-cloud fitting and small regression problems with variable-sized inputs.

Choose a surface that gives its own derivatives

For the main optical path, I also exploit the structure of the input itself. The measurements arrive on a regular grid of radii and angles. That makes it possible to build one bicubic spline—a surface assembled from smooth polynomial pieces—and evaluate it wherever the rays need it.

I use SciPy’s RectBivariateSpline to obtain the height and its derivatives with respect to radius and angle. The ray tracer needs slopes in Cartesian coordinates, so I convert them with the chain rule:

∂z/∂x = (∂z/∂r) cos θ − (∂z/∂θ) sin θ / r
∂z/∂y = (∂z/∂r) sin θ + (∂z/∂θ) cos θ / r

Those two slopes give the surface normal used to refract a ray. At the centre, where the polar formula divides by zero, I evaluate small directional differences instead. Around the angular seam, I extend the samples with their neighbours from the opposite side of the grid.

The spline is built once per surface within a computation and reused across queries and analysis diameters. It replaces repeated neighbourhood searches and local fitting with evaluations of the fitted surface. Unlike caching an unchanged interpolation operator, choosing a spline changes the surface approximation. Height and slope agreement therefore belong in the numerical comparison, alongside runtime.

This is where mathematical representation and performance meet. A representation that supplies the quantities the next operation needs can remove a whole stage of repeated work. The ray-tracing article follows those derivatives through both corneal surfaces and into the optical calculation.

In CorneaForge, I use three complementary moves: cache a spatial relationship that stays fixed, batch local problems that share an algebraic structure, and choose surface representations that make repeated evaluation efficient. Each starts with the mathematics of the workload. Together, they connect the map renderer to the optical engine built on the same measurements.