Skip to content

Commit

Permalink
Optimizing performance of _BinaryWriter._resizeBuffer
Browse files Browse the repository at this point in the history
In class _BinaryWriter of the gltf exporter, a loop is used to copy contents in old buffer to new buffer, which is benchmarked to be slow.
TypedArray.prototype.set() can be used for faster memory copying while having the same Browser compatibility of TypedArray.

Forum link:
https://forum.babylonjs.com/t/optimizing-performance-of-binarywriter-resizebuffer/37516
  • Loading branch information
myfreeer committed Jan 20, 2023
1 parent aceaefa commit 5ad54c3
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/dev/serializers/src/glTF/2.0/glTFExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2344,11 +2344,12 @@ export class _BinaryWriter {
*/
private _resizeBuffer(byteLength: number): ArrayBuffer {
const newBuffer = new ArrayBuffer(byteLength);
const oldUint8Array = new Uint8Array(this._arrayBuffer);
let oldUint8Array = new Uint8Array(this._arrayBuffer);
const newUint8Array = new Uint8Array(newBuffer);
for (let i = 0, length = newUint8Array.byteLength; i < length; ++i) {
newUint8Array[i] = oldUint8Array[i];
if (oldUint8Array.length > newUint8Array.length) {
oldUint8Array = oldUint8Array.subarray(0, newUint8Array.length);
}
newUint8Array.set(oldUint8Array, 0);
this._arrayBuffer = newBuffer;
this._dataView = new DataView(this._arrayBuffer);

Expand Down

0 comments on commit 5ad54c3

Please sign in to comment.