Famous / engine

MIT License
1.75k stars 250 forks source link

Idea: Use transferable objects instead of constructing a new typed array in Buffer #431

Open alexanderGugel opened 9 years ago

alexanderGugel commented 9 years ago

Sending an ArrayBuffer via postMessage and marking it as transferable might be worth investigating, since it would significantly lower the memory footprint due to

  1. zero-copy transfer
  2. no need to instantiate a typed array in subData
Buffer.prototype.subData = function subData() {
    var gl = this.gl;
    var data = [];

    // to prevent against maximum call-stack issue.
    for (var i = 0, chunk = 10000; i < this.data.length; i += chunk)
        data = Array.prototype.concat.apply(data, this.data.slice(i, i + chunk));

    this.buffer = this.buffer || gl.createBuffer();
    gl.bindBuffer(this.target, this.buffer);
    gl.bufferData(this.target, new this.type(data), gl.STATIC_DRAW);
};

Also

    // to prevent against maximum call-stack issue.
    for (var i = 0, chunk = 10000; i < this.data.length; i += chunk)
        data = Array.prototype.concat.apply(data, this.data.slice(i, i + chunk));

should be removed IMO. It seems very excessive in terms of memory footprint and execution time.