Keep the slow version
SpatialForge reconstructs geometry from calibrated RGB-D frames and supplied camera poses. I kept its dense TSDF implementation deliberately simple. It gives me a readable reference for projection, signed-distance updates, weighting, and truncation before faster data structures enter the picture.
That changed the optimization target. A mesh that looked roughly the same was not enough. For the documented replay and configuration, every faster path had to serialize to the same bytes as the scalar reference.
Profile before changing the data structure
A dense volume is easy to inspect, but it visits a large regular region even when one RGB-D frame can update only a small part of it. Projection, bounds checks, depth sampling, truncation tests, and weighted updates were all repeated in Python-level loops. That work dominated the replay.
The documented workload took roughly 210 seconds. Profiling pointed to fusion, not file loading or final serialization, so I focused on visiting fewer voxels and doing less interpreter work inside the measured hot path.
- Keep the dense implementation as the correctness oracle after the first speed-up.
- Plan only the sparse blocks that the current observation can affect.
- Batch numerical work only where the defined update behaviour stays intact.
- Compare serialized artifacts after every optimization, not screenshots or aggregate mesh counts.
Sparse blocks reduce the work, but not for free
The sparse path divides the volume into blocks and materializes only those selected for the current frame. Work follows observed space instead of the full bounding volume, which improves runtime and memory use without changing the TSDF update for a voxel that is visited.
Sparsity does not guarantee speed. Block selection can become the next bottleneck, and a tiny array operation may spend more time on allocation and dispatch than arithmetic. The planner still needs measured boundaries, stable indexing, and batches large enough to make vectorization worthwhile.
My first vectorized version changed the bytes
My first vectorized version was fast and numerically close. It also failed the byte comparison. Changing grouping, traversal order, intermediate precision, or rounding can alter low-order bits because floating-point addition is not associative.
For this project, ‘close enough’ was a different contract. I adjusted the optimized path until it followed the reference update behaviour on the measured replay, then turned byte equality into a regression test. Future performance work must preserve that contract or introduce a new one explicitly.
const reference = serialize(fuseDense(recording, config))
const optimized = serialize(fuseSparse(recording, config))
expect(hash(optimized)).toEqual(hash(reference))What improved
On the documented sparse-fusion workload, runtime fell from about 210 seconds to about 2 seconds—a little over 100×—while the output stayed byte-identical to the scalar reference. The number belongs to that workload, implementation, hardware, and configuration. It does not mean every SpatialForge command or RGB-D sequence is 100× faster.
I also ran a held-out replay on TUM RGB-D freiburg1_xyz with supplied poses. It fused 99 frames and held out 98, covering 1,428,048 samples. The TSDF residual was 9.3 mm at the median, 20.4 mm RMS, and 45.6 mm at p95. Those numbers measure disagreement in the signed-distance field, not absolute surface accuracy or SLAM accuracy.
What still needs work
SpatialForge is a reconstruction and evaluation prototype built around calibrated frames and supplied poses. It does not estimate poses, close loops, relocalize a camera, or attach semantic labels. The sparse representation still lives in memory, and real-data meshing still produces non-manifold cases that fail validation.
The result is narrower than a complete mapping system: one profiled numerical kernel became much faster while the scalar path protected its behaviour. The surrounding project is still a prototype.
- Byte identity is verified for defined fixtures and configurations, not every possible platform and compiler combination.
- Held-out TSDF residuals measure field consistency under supplied poses, not ground-truth geometric accuracy.
- Sparse planning and meshing still have their own scaling and correctness limits.
- Performance measurements should be rerun when data, hardware, numerical libraries, or configuration changes.