mikolalysenko / mudb

Low latency state replication for the web

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should MuBuffer constructor also take Uint8Array as argument?

hediyi opened this issue · comments

could reduce some allocations in some situation like in the MuReadStream constructor

constructor (data:Uint8Array) {
    this.buffer = new MuBuffer(data.buffer);
}

No. Because buffers are all pooled this could cause a memory leak.

We don't pool readstream buffers. And right now new MuBuffer() always create a new Uint8Array any way. I'm thinking something like

constructor (buffer:ArrayBuffer|Uint8Array) {
  const uint8 = buffer instance of Uint8Array ? buffer : new Uint8Array(buf)
  this.buffer = uint8.buffer
  this.uint8 = uint8
  ...
}

Maybe i miss something here, how is this gonna cause a leak?

It may make stuff more complicated. One thing to consider is that we may need to track byteOffset and byteLength from the uint8array.

fair point! yea, it's gonna be complicated.. closing