A variety of techniques have been used.

Deferring calculations until needed

Each face had a normal vector and area stored in the nx,ny,nz,orig_area members of the FACE struct.

mris_fix_topology has a hot loop

_: _: a computeDefectContext is constructed here

_:_: then it loops over a set of patches, or iterates on one patch. For each patch it calls

_:_:_: mrisDefectPatchFitness, which calls

_:_:_: _: mrisComputeDefectLogLikelihood, which calls

_:_:_: _: _: mrisComputeDefectMRILogUnlikelihood, which

_:_:_: _: _:_: does an expensive computation all the face normals for ALL the faces

_:_:_: _: _:_: does two other expensive steps, which only use a few of the face normals