andywer / andywer/threads.js

What is the correct way to ensure zero copy of an ArrayBuffer and the ability to transfer back to the main thread (without reassignment)

Đang mở
#385 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
question
Ngôn ngữ chính
TypeScript
Star
3.5k
Fork
173
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

I have a worker function:

```
mutate (what: ArrayBuffer): TransferDescriptor {
const inView = new Uint8Array(what);
inView[0] = 88;
return Transfer(what);
},
```

When I `Transfer` an `ArrayBuffer` into this method, the original `ArrayBuffer` becomes detached.

```
const what = await workerManager.call(
async w => {
const o = await w.mutate(Transfer(ab));
return o;
}
);
```

```
ab => ArrayBuffer { (detached), byteLength: 0 }
what => ArrayBuffer { [Uint8Contents]: <58 62 63>, byteLength: 3 }
```

Note that the `call` method just does this:

```
public async call(
f: (worker: ModuleThread) => Promise,
): Promise {
if (!this.pool) {
throw new workersErrors.EncryptedFSWorkerNotRunningError();
}
return await this.pool.queue(f);
}
```

I notice I have to use the returned array buffer. The `ab` is no longer usable.

I thought I would be able to share a buffer with a thread, make that thread make mutations, and then share that buffer back, but the above seems to mean that I have to use a new assignment, and discard the old assignment.

Furthermore I actually want to use Node buffers. But when I use node buffers with the internal array buffer, the internal array buffer gets detached even under the same usage. This is quite strange behaviour.

This is a follow up from https://github.com/andywer/threads.js/issues/348

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.