Skip to content

(Interleaved)BufferAttribute: Optimize normalized data access. - #34992

Merged
Mugen87 merged 2 commits into
mrdoob:devfrom
zeux:normalize-perf
Oct 14, 2026
Merged

Mugen87 merged 2 commits into
mrdoob:devfrom
zeux:normalize-perf

Conversation

@zeux

@zeux zeux commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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.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 either v / factor for unsigned values, or Math.max(-1, v / factor) for signed values. The latter formula can also work for unsigned 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 = new 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. 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:

image image

Sample counts here correspond to milliseconds; so the whole loadMesh call got ~2x faster (473 ms vs 956 ms), and createGLTFSphericalHarmonicsAttributes that specifically heavily relied on getX/Y/Z/W got almost ~3x faster (228 ms vs 673 ms).

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.
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

๐Ÿ“ฆ Bundle size

Full ESM build including core, minified and gzipped.

Before After Diff
Core 390.84
102.52
391.28
102.64
+438 B
+119 B
WebGL 777.27
194.10
777.71
194.23
+438 B
+128 B
WebGPU 1098.56
295.85
1099.00
295.97
+438 B
+122 B
WebGPU Nodes 1096.51
295.48
1096.94
295.61
+438 B
+130 B

๐ŸŒณ Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 518.46
124.59
518.57
124.62
+116 B
+36 B
WebGPU 776.84
208.03
777.28
208.11
+438 B
+82 B
WebGPU Nodes 723.96
194.78
724.40
194.91
+438 B
+130 B

Comment thread src/math/MathUtils.js Resolved
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.
Comment thread src/core/BufferAttribute.js Resolved
@Mugen87 Mugen87 added this to the r187 milestone Oct 14, 2026
@Mugen87 Mugen87 changed the title Optimize normalized data access in (Interleaved)BufferAttribute (Interleaved)BufferAttribute: Optimize normalized data access. Oct 14, 2026
@Mugen87
Mugen87 merged commit 877acc8 into mrdoob:dev Oct 14, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants