mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 20:59:10 +00:00
Move buffers between V8 and native
* send()/recv() now operate on TypedArrays rather than ArrayBuffers. * Remove a copy (through ArrayBuffer.slice()) from the send path. * Remove a copy (through v8::ArrayBuffer::New()) from the return path. * After moving a buffer from JS to native, the ArrayBuffer object and it's views are made inaccessible ('neutered'). * `struct deno_buf` now holds two [ptr, length] tuples, one for the actual memory allocation, and one for the logical data contained therein. This is necessary because flatbuffers fills it's buffer bottom-up, so the serialized blob doesn't start at beginning of the buffer, but somewhere in the middle.
This commit is contained in:
parent
bbcd4c8dd3
commit
24b0e91d80
12 changed files with 438 additions and 70 deletions
13
js/main.ts
13
js/main.ts
|
@ -19,7 +19,7 @@ function assignCmdId(): number {
|
|||
return cmdId;
|
||||
}
|
||||
|
||||
function startMsg(cmdId: number): ArrayBuffer {
|
||||
function startMsg(cmdId: number): Uint8Array {
|
||||
const builder = new flatbuffers.Builder();
|
||||
const msg = fbs.Start.createStart(builder, 0);
|
||||
fbs.Base.startBase(builder);
|
||||
|
@ -27,7 +27,7 @@ function startMsg(cmdId: number): ArrayBuffer {
|
|||
fbs.Base.addMsg(builder, msg);
|
||||
fbs.Base.addMsgType(builder, fbs.Any.Start);
|
||||
builder.finish(fbs.Base.endBase(builder));
|
||||
return typedArrayToArrayBuffer(builder.asUint8Array());
|
||||
return builder.asUint8Array();
|
||||
}
|
||||
|
||||
window["denoMain"] = () => {
|
||||
|
@ -47,7 +47,7 @@ window["denoMain"] = () => {
|
|||
}
|
||||
|
||||
// Deserialize res into startResMsg.
|
||||
const bb = new flatbuffers.ByteBuffer(new Uint8Array(res));
|
||||
const bb = new flatbuffers.ByteBuffer(res);
|
||||
const base = fbs.Base.getRootAsBase(bb);
|
||||
assert(base.cmdId() === cmdId);
|
||||
assert(fbs.Any.StartRes === base.msgType());
|
||||
|
@ -69,10 +69,3 @@ window["denoMain"] = () => {
|
|||
mod.compileAndRun();
|
||||
*/
|
||||
};
|
||||
|
||||
function typedArrayToArrayBuffer(ta: Uint8Array): ArrayBuffer {
|
||||
return ta.buffer.slice(
|
||||
ta.byteOffset,
|
||||
ta.byteOffset + ta.byteLength
|
||||
) as ArrayBuffer;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue