Repository navigation
(Interleaved)BufferAttribute: Optimize normalized data access. - #34992
Merged
Merged
Conversation
Every call to getX/setX in BufferAttribute/InterleavedBufferAttribute that use an integer data type and normalized=true would go through normalize/denormalize function in MathUtils. These functions are comparatively slow because they do a switch dispatch based on the array constructor every time. The normalization and denormalization math, however, is almost the same between all variants: normalization is always Math.round(v * factor), and denormalization is either v / factor for unsigned values, or Math.max(-1, v / factor) for signed values. The latter formula can also work for signed values, because v / factor is never negative there. We use this to significantly optimize accessors by caching the normalization factor based on the array data type. If the array is not provided at construction time, the factor is left undefined, and the code still works; this keeps code like this: let buf = BufferAttribute(); buf.array = array; buf.normalized = true; ... working, if slowly. This makes access to Int8 normalized buffer attributes up to 2x faster when using v8 and up to 3x faster when using SpiderMonkey.
๐ฆ Bundle sizeFull ESM build including core, minified and gzipped.
๐ณ Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
Mugen87
reviewed
Oct 14, 2026
Instead of per-type formula we can use getNormalizationScale to retrieve the scale factor and a type-independent formula that matches the optimized variants in BufferAttribute et al.
Mugen87
reviewed
Oct 14, 2026
Mugen87
approved these changes
Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every call to
getX/setXinBufferAttribute/InterleavedBufferAttributethat use an integer data type andnormalized=truewould go throughnormalize/denormalizefunction inMathUtils.js. These functions are comparatively slow because they do a switch dispatch based on the array constructor every time.The normalization and denormalization math, however, is almost the same between all variants: normalization is always
Math.round(v * factor), and denormalization is eitherv / factorfor unsigned values, orMath.max(-1, v / factor)for signed values. The latter formula can also work for unsigned values, becausev / factoris never negative there.We use this to significantly optimize accessors by caching the normalization factor based on the array data type. If the array is not provided at construction time, the factor is left undefined, and the code still works; this keeps code like this:
... working, if slowly.
This makes access to Int8 normalized buffer attributes up to 2x faster when using v8 and up to 3x faster when using SpiderMonkey. Note that the results are exact as before; while you could cache the reciprocal factor and multiply by it, it results in slightly different outputs and that didn't seem warranted here.
I've hit this in a few different cases before, but what prompted me to fix this was working with some large glTF files using the Gaussian Splat loader with SHn encoded using int8; here's Firefox profiles before/after:
Sample counts here correspond to milliseconds; so the whole
loadMeshcall got ~2x faster (473 ms vs 956 ms), andcreateGLTFSphericalHarmonicsAttributesthat specifically heavily relied on getX/Y/Z/W got almost ~3x faster (228 ms vs 673 ms).