Skip to content

perf: persistent buffer with colored pens#16

Merged
william-silversmith merged 10 commits into
mainfrom
wms_persistent_shape
Jul 22, 2025
Merged

perf: persistent buffer with colored pens#16
william-silversmith merged 10 commits into
mainfrom
wms_persistent_shape

Conversation

@william-silversmith

@william-silversmith william-silversmith commented Jul 22, 2025

Copy link
Copy Markdown
Contributor

Allocating and zeroing the visit buffer can be a significant cost. Even though most of the algorithm doesn't touch most of the volume, zeroing requires touching every voxel, meaning each query on a given shape is O(V * K) where V is the number of voxels and K is the number of queries. Since we were using a std::vector that reduced the cost 8x, and when we implemented 2x2x2, that reduced the cost 64x, but we can do better.

Instead of zeroing the buffer each time, let's use std::vector<uint8_t>, zero it to start, and then draw in a different color for each visit. Then we only need to zero it once every 255 times.

The memory grows 8x vs std::vector, but the amortized cost of zeroing is now 31.9 times less (255 / 8).

This behavior will be opt in as you need to intentionally allocate and deallocate the buffer. If you forget, the buffer will automatically grow as needed, but you'll have a memory leak.

I also tried experimenting with pre-computing all the "cubes", but since most of the volume is usually untouched, and most planes along a skeleton don't intersect much, there is a negative impact to doing so.

@william-silversmith william-silversmith added the enhancement New feature or request label Jul 22, 2025
@william-silversmith william-silversmith self-assigned this Jul 22, 2025
@william-silversmith william-silversmith merged commit 2c68115 into main Jul 22, 2025
5 checks passed
@william-silversmith william-silversmith deleted the wms_persistent_shape branch July 22, 2025 00:22
@william-silversmith william-silversmith added the performance Make it faster or lower memory. label Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request performance Make it faster or lower memory.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant