question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating a lot of garbage by using BufferAttribute.updateRange

See original GitHub issue

updateRange parameter of a BufferAttribute is intended to improve performance. I use it in my particle engine. I have been doing some performance testing and found following picture to be pretty standard across my game: image

As you can see, about 26% of the garbage alone comes from here: https://github.com/mrdoob/three.js/blob/3a3d2b1e591ced02b5742c6f4dd5d62f457a760c/src/renderers/webgl/WebGLAttributes.js#L66-L88

Why does this happen? Because WebGL1.0 does not allow you to specify offset and length of the source array that you you supply.

Here’s a relevant piece from MDN:

// WebGL1: 
void gl.bufferSubData(target, offset, ArrayBuffer srcData); 
void gl.bufferSubData(target, offset, ArrayBufferView srcData); 

// WebGL2: 
void gl.bufferSubData(target, dstByteOffset, ArrayBufferView srcData, srcOffset, length);

Well, I’m in luck, I do use WebGL2. I’m not sure how to make three.js understand that though, so that we can avoid that needless .subarray call that generates all the garbage currently.

Any thoughts?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Mugen87commented, Jan 15, 2020

Would it be acceptable to query the gl context gl.VERSION at WebGLAttributes construction time and set a flag to select the WebGL API form of the call when using bufferSubData?

You may want to use the existing isWebGL2 flag of WebGLCapabilities.

0reactions
aardgoosecommented, Jan 15, 2020

Looking at the stats more closely it does look like an overhead of about 90 bytes per array buffer update, so this is probably just the overhead of creating a new ArrayBuffer via subarray() rather than a performance issue with subarray (the issue occurs in FF and Chrome anyway). Which was presumably one of the drivers for the change in the WebGL2 API. There doesn’t appear to be any other way of avoiding the problem.

Would it be acceptable to query the gl context gl.VERSION at WebGLAttributes construction time and set a flag to select the WebGL API form of the call when using bufferSubData?

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found