# Meshopt decoder: increase concurrency

**URL:** <https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902>\
**Category:** Feature requests\
**Tags:** performance, workers\
**Created:** [October 11, 2025, 2:30am UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902 "2025-10-11T02:30:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [October 11, 2025, 2:30am UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902/1 "2025-10-11T02:30:27Z")

</div>

Currently in `MeshoptCompression` off-thread decoding is used, and the worker count is fixed `1`.

> <https://github.com/BabylonJS/Babylon.js/blob/c190296494a0c192edb26689045c929902476937/packages/dev/core/src/Meshes/Compression/meshoptCompression.ts#L113>

Since multithreading already supported, any intent to use [`navigator.hardwareConcurrency`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/hardwareConcurrency) or `navigator.hardwareConcurrency - 1` when available instead of `1` to get maximum decoding performance?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [October 13, 2025, 3:13pm UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902/2 "2025-10-13T15:13:44Z")

</div>

cc @bghgary and @alexchuber

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 13, 2025, 8:41pm UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902/3 "2025-10-13T20:41:29Z")

</div>

If we trace the history of this change, it came from here: [Fix memory leak in MeshoptCompression by OrigamiDev-Pete · Pull Request #14995 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/14995)

Which came from: [MeshoptDecoder Memory Leak - Bugs - Babylon.js](https://forum.babylonjs.com/t/meshoptdecoder-memory-leak/49696)

Which came from: [wasm: expose methods to dispose or recreate wasm instances · Issue #522 · zeux/meshoptimizer · GitHub](https://github.com/zeux/meshoptimizer/issues/522) which is posted by you @kzhsw 😃

Full circle, I guess. Do you have a suggestion on how to use more threads and not have a memory leak?

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [October 14, 2025, 12:40am UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902/4 "2025-10-14T00:40:23Z")

</div>

For thread count, both `navigator.hardwareConcurrency` and `Math.max(1, navigator.hardwareConcurrency - 1)`, later one keeps a thread for main content thread.  
For cleaning, make something like a queue, or a count down to wait all concurrent tasks to complete before terminating all threads.

```typescript
let ActiveTasks = 0;

    public async decodeGltfBufferAsync(source: Uint8Array, count: number, stride: number, mode: "ATTRIBUTES" | "TRIANGLES" | "INDICES", filter?: string): Promise<Uint8Array> {
        await this._decoderModulePromise!;
        if (NumberOfWorkers === 0) {
            MeshoptDecoder.useWorkers(Math.max(1, typeof navigator === "object" && Number.isFinite(navigator.hardwareConcurrency) ? navigator.hardwareConcurrency : 1));
            NumberOfWorkers = 1;
        }
        let result: Uint8Array;
        ActiveTasks++;
        try {
            result = await MeshoptDecoder.decodeGltfBufferAsync(count, stride, source, mode, filter);
        } finally {
            ActiveTasks--;
            // a simple debounce to avoid switching back and forth between workers and no workers while decoding
            if (WorkerTimeout !== null) {
                clearTimeout(WorkerTimeout);
            }
            WorkerTimeout = setTimeout(() => {
                if (ActiveTasks === 0) {
                    MeshoptDecoder.useWorkers(0);
                    NumberOfWorkers = 0;
                }// else another timeout would be set after async task completed
                WorkerTimeout = null;
            }, 1000);
        }
        return result;
    }

```

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [November 7, 2025, 5:43am UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902/5 "2025-11-07T05:43:24Z")

</div>

Any news on this?

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [November 7, 2025, 8:44pm UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902/6 "2025-11-07T20:44:04Z")

</div>

_Sorry, I saw this at the time but didn’t have a response then._

I think maybe we can use [AutoReleaseWorkerPool](https://github.com/BabylonJS/Babylon.js/blob/caeae1c5debede447b1d153f97fc7b754865565b/packages/dev/core/src/Misc/workerPool.ts#L95) also to avoid the memory issue.

I would be careful using `navigator.hardwareConcurrency` directly. We have done this in the past and caused out-of-memory issues because this value is very high on some systems. We can probably factor out [this function](https://github.com/BabylonJS/Babylon.js/blob/caeae1c5debede447b1d153f97fc7b754865565b/packages/dev/core/src/Meshes/Compression/dracoCodec.ts#L52-L59) to get a reasonable value.

Do you want to give this a shot?

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [November 8, 2025, 12:23am UTC](https://forum.babylonjs.com/t/meshopt-decoder-increase-concurrency/60902/7 "2025-11-08T00:23:23Z")

</div>

Well, the thing is, meshopt manages its own worker pool, and worker creation and disposion is not exposed.
