提交 213885a9 编写于 作者: R Ryan Dahl

Optimization: Reuse ArrayBuffer during serialization.

上级 4e2e185d
......@@ -79,6 +79,7 @@ ts_sources = [
"js/fetch.ts",
"js/file_info.ts",
"js/files.ts",
"js/flatbuffers.ts",
"js/global_eval.ts",
"js/globals.ts",
"js/io.ts",
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
/** Copies the contents of a file to another by name synchronously.
......@@ -36,7 +36,7 @@ function req(
from: string,
to: string
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const from_ = builder.createString(from);
const to_ = builder.createString(to);
msg.CopyFile.startCopyFile(builder);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { assert } from "./util";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { sendSync } from "./dispatch";
/**
......@@ -12,7 +12,7 @@ import { sendSync } from "./dispatch";
* throws NotFound exception if directory not available
*/
export function cwd(): string {
const builder = new flatbuffers.Builder(0);
const builder = flatbuffers.createBuilder();
msg.Cwd.startCwd(builder);
const inner = msg.Cwd.endCwd(builder);
const baseRes = sendSync(builder, msg.Any.Cwd, inner);
......@@ -28,7 +28,7 @@ export function cwd(): string {
* throws NotFound exception if directory not available
*/
export function chdir(directory: string): void {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const directory_ = builder.createString(directory);
msg.Chdir.startChdir(builder);
msg.Chdir.addDirectory(builder, directory_);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { libdeno } from "./libdeno";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as msg from "gen/msg_generated";
import * as errors from "./errors";
import * as util from "./util";
......@@ -86,6 +86,7 @@ function sendInternal(
msg.Base.addSync(builder, sync);
msg.Base.addCmdId(builder, cmdId);
builder.finish(msg.Base.endBase(builder));
return [cmdId, libdeno.send(builder.asUint8Array(), data)];
const res = libdeno.send(builder.asUint8Array(), data);
builder.inUse = false;
return [cmdId, res];
}
......@@ -7,7 +7,7 @@ import {
typedArrayToArrayBuffer,
notImplemented
} from "./util";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { sendAsync } from "./dispatch";
import * as msg from "gen/msg_generated";
import * as domTypes from "./dom_types";
......@@ -187,7 +187,7 @@ export async function fetch(
log("dispatch FETCH_REQ", url);
// Send FetchReq message
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const url_ = builder.createString(url);
msg.FetchReq.startFetchReq(builder);
msg.FetchReq.addUrl(builder, url_);
......
......@@ -4,7 +4,7 @@ import { Reader, Writer, Closer, ReadResult } from "./io";
import * as dispatch from "./dispatch";
import * as msg from "gen/msg_generated";
import { assert } from "./util";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
/** The Deno abstraction for reading and writing files. */
export class File implements Reader, Writer, Closer {
......@@ -51,7 +51,7 @@ export async function open(
filename: string,
mode: OpenMode = "r"
): Promise<File> {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const filename_ = builder.createString(filename);
msg.Open.startOpen(builder);
msg.Open.addFilename(builder, filename_);
......@@ -73,7 +73,7 @@ export async function read(
rid: number,
p: ArrayBufferView
): Promise<ReadResult> {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Read.startRead(builder);
msg.Read.addRid(builder, rid);
const inner = msg.Read.endRead(builder);
......@@ -90,7 +90,7 @@ export async function read(
* Resolves with the number of bytes written.
*/
export async function write(rid: number, p: ArrayBufferView): Promise<number> {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Write.startWrite(builder);
msg.Write.addRid(builder, rid);
const inner = msg.Write.endWrite(builder);
......@@ -104,7 +104,7 @@ export async function write(rid: number, p: ArrayBufferView): Promise<number> {
/** Close the file ID. */
export function close(rid: number): void {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Close.startClose(builder);
msg.Close.addRid(builder, rid);
const inner = msg.Close.endClose(builder);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { flatbuffers } from "flatbuffers";
import * as util from "./util";
// Re-export some types.
export type Offset = flatbuffers.Offset;
export class ByteBuffer extends flatbuffers.ByteBuffer {}
export interface Builder extends flatbuffers.Builder {
inUse: boolean;
}
const globalBuilder = new flatbuffers.Builder() as Builder;
globalBuilder.inUse = false;
// This is a wrapper around the real Builder .
// The purpose is to reuse a single ArrayBuffer for every message.
// We can do this because the "control" messages sent to the privileged
// side are guarenteed to be used during the call to libdeno.send().
export function createBuilder(): Builder {
// tslint:disable-next-line:no-any
const gb = globalBuilder as any;
util.assert(!gb.inUse);
let bb = globalBuilder.dataBuffer();
// Only create a new backing ArrayBuffer if the previous one had grown very
// large in capacity. This should only happen rarely.
if (bb.capacity() > 1024) {
util.log(`realloc flatbuffer ArrayBuffer because it was ${bb.capacity()}`);
bb = ByteBuffer.allocate(1024);
}
gb.bb = bb;
// Remaining space in the ByteBuffer.
gb.space = globalBuilder.dataBuffer().capacity();
// Minimum alignment encountered so far.
gb.minalign = 1;
// The vtable for the current table.
gb.vtable = null;
// The amount of fields we're actually using.
gb.vtable_in_use = 0;
// Whether we are currently serializing a table.
gb.isNested = false;
// Starting offset of the current struct/table.
gb.object_start = 0;
// List of offsets of all vtables.
gb.vtables = [];
// For the current vector being built.
gb.vector_num_elems = 0;
// False omits default values from the serialized data
gb.force_defaults = false;
gb.inUse = true;
return gb as Builder;
}
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as msg from "gen/msg_generated";
import { assert, log, setLogDebug } from "./util";
import * as os from "./os";
......@@ -11,7 +11,7 @@ import { promiseErrorExaminer, promiseRejectHandler } from "./promise_util";
import { version } from "typescript";
function sendStart(): msg.StartRes {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Start.startStart(builder);
const startOffset = msg.Start.endStart(builder);
const baseRes = sendSync(builder, msg.Any.Start, startOffset);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
import { assert } from "./util";
......@@ -43,7 +43,7 @@ function req({
prefix,
suffix
}: MakeTempDirOptions): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const fbDir = dir == null ? -1 : builder.createString(dir);
const fbPrefix = prefix == null ? -1 : builder.createString(prefix);
const fbSuffix = suffix == null ? -1 : builder.createString(suffix);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { assert } from "./util";
import * as dispatch from "./dispatch";
......@@ -18,7 +18,7 @@ export function metrics(): Metrics {
}
function req(): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Metrics.startMetrics(builder);
const inner = msg.Metrics.endMetrics(builder);
return [builder, msg.Any.Metrics, inner];
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
/** Creates a new directory with the specified path and permission
......@@ -26,7 +26,7 @@ function req(
path: string,
mode: number
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const path_ = builder.createString(path);
msg.Mkdir.startMkdir(builder);
msg.Mkdir.addPath(builder, path_);
......
......@@ -4,7 +4,7 @@ import { ReadResult, Reader, Writer, Closer } from "./io";
import * as msg from "gen/msg_generated";
import { assert, notImplemented } from "./util";
import * as dispatch from "./dispatch";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { read, write, close } from "./files";
export type Network = "tcp";
......@@ -32,7 +32,7 @@ class ListenerImpl implements Listener {
constructor(readonly rid: number) {}
async accept(): Promise<Conn> {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Accept.startAccept(builder);
msg.Accept.addRid(builder, this.rid);
const inner = msg.Accept.endAccept(builder);
......@@ -111,7 +111,7 @@ enum ShutdownMode {
}
function shutdown(rid: number, how: ShutdownMode) {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Shutdown.startShutdown(builder);
msg.Shutdown.addRid(builder, rid);
msg.Shutdown.addHow(builder, how);
......@@ -136,7 +136,7 @@ function shutdown(rid: number, how: ShutdownMode) {
* See `dial()` for a description of the network and address parameters.
*/
export function listen(network: Network, address: string): Listener {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const network_ = builder.createString(network);
const address_ = builder.createString(address);
msg.Listen.startListen(builder);
......@@ -179,7 +179,7 @@ export function listen(network: Network, address: string): Listener {
* dial("tcp", ":80")
*/
export async function dial(network: Network, address: string): Promise<Conn> {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const network_ = builder.createString(network);
const address_ = builder.createString(address);
msg.Dial.startDial(builder);
......
......@@ -3,12 +3,12 @@ import { ModuleInfo } from "./types";
import * as msg from "gen/msg_generated";
import { assert } from "./util";
import * as util from "./util";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { sendSync } from "./dispatch";
/** Exit the Deno process with optional exit code. */
export function exit(exitCode = 0): never {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Exit.startExit(builder);
msg.Exit.addCode(builder, exitCode);
const inner = msg.Exit.endExit(builder);
......@@ -23,7 +23,7 @@ export function codeFetch(
): ModuleInfo {
util.log("os.ts codeFetch", moduleSpecifier, containingFile);
// Send CodeFetch message
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const moduleSpecifier_ = builder.createString(moduleSpecifier);
const containingFile_ = builder.createString(containingFile);
msg.CodeFetch.startCodeFetch(builder);
......@@ -53,7 +53,7 @@ export function codeCache(
outputCode: string
): void {
util.log("os.ts codeCache", filename, sourceCode, outputCode);
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const filename_ = builder.createString(filename);
const sourceCode_ = builder.createString(sourceCode);
const outputCode_ = builder.createString(outputCode);
......@@ -84,7 +84,7 @@ function createEnv(_inner: msg.EnvironRes): { [index: string]: string } {
}
function setEnv(key: string, value: string): void {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const _key = builder.createString(key);
const _value = builder.createString(value);
msg.SetEnv.startSetEnv(builder);
......@@ -113,7 +113,7 @@ export function env(): { [index: string]: string } {
command: msg.Command.ENV,
});
*/
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.Environ.startEnviron(builder);
const inner = msg.Environ.endEnviron(builder);
const baseRes = sendSync(builder, msg.Any.Environ, inner)!;
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
import { FileInfo, FileInfoImpl } from "./file_info";
import { assert } from "./util";
......@@ -25,7 +25,7 @@ export async function readDir(path: string): Promise<FileInfo[]> {
}
function req(path: string): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const path_ = builder.createString(path);
msg.ReadDir.startReadDir(builder);
msg.ReadDir.addPath(builder, path_);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { assert } from "./util";
import * as dispatch from "./dispatch";
......@@ -29,7 +29,7 @@ export async function readFile(filename: string): Promise<Uint8Array> {
function req(
filename: string
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const filename_ = builder.createString(filename);
msg.ReadFile.startReadFile(builder);
msg.ReadFile.addFilename(builder, filename_);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { assert } from "./util";
import * as dispatch from "./dispatch";
......@@ -23,7 +23,7 @@ export async function readlink(name: string): Promise<string> {
}
function req(name: string): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const name_ = builder.createString(name);
msg.Readlink.startReadlink(builder);
msg.Readlink.addName(builder, name_);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
/** Removes the named file or (empty) directory synchronously. Would throw
......@@ -47,7 +47,7 @@ function req(
path: string,
recursive: boolean
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const path_ = builder.createString(path);
msg.Remove.startRemove(builder);
msg.Remove.addPath(builder, path_);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
/** Synchronously renames (moves) `oldpath` to `newpath`. If `newpath` already
......@@ -30,7 +30,7 @@ function req(
oldpath: string,
newpath: string
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const oldpath_ = builder.createString(oldpath);
const newpath_ = builder.createString(newpath);
msg.Rename.startRename(builder);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
import { assert } from "./util";
import { FileInfo, FileInfoImpl } from "./file_info";
......@@ -54,7 +54,7 @@ function req(
filename: string,
lstat: boolean
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const filename_ = builder.createString(filename);
msg.Stat.startStat(builder);
msg.Stat.addFilename(builder, filename_);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
import * as util from "./util";
......@@ -43,7 +43,7 @@ function req(
if (type) {
return util.notImplemented();
}
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const oldname_ = builder.createString(oldname);
const newname_ = builder.createString(newname);
msg.Symlink.startSymlink(builder);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { assert } from "./util";
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import { sendSync, setFireTimersCallback } from "./dispatch";
// Tell the dispatcher which function it should call to fire timers that are
......@@ -51,7 +51,7 @@ function setGlobalTimeout(due: number | null, now: number) {
assert(timeout >= 0);
}
// Send message to the backend.
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
msg.SetTimeout.startSetTimeout(builder);
msg.SetTimeout.addTimeout(builder, timeout);
const inner = msg.SetTimeout.endSetTimeout(builder);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
/** Truncates or extends the specified file synchronously, updating the size of
......@@ -30,7 +30,7 @@ function req(
name: string,
len?: number
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const name_ = builder.createString(name);
len = len && len > 0 ? Math.floor(len) : 0;
msg.Truncate.startTruncate(builder);
......
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
/** Write a new file, with given filename and data synchronously.
......@@ -40,7 +40,7 @@ function req(
data: Uint8Array,
perm: number
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset, Uint8Array] {
const builder = new flatbuffers.Builder();
const builder = flatbuffers.createBuilder();
const filename_ = builder.createString(filename);
msg.WriteFile.startWriteFile(builder);
msg.WriteFile.addFilename(builder, filename_);
......
......@@ -816,8 +816,8 @@ fn op_copy_file(
// Once the issue is reolved, we should remove this workaround.
if cfg!(unix) && !from.is_file() {
return Err(errors::new(
ErrorKind::NotFound,
"File not found".to_string(),
ErrorKind::NotFound,
"File not found".to_string(),
));
}
......
......@@ -57,6 +57,7 @@ def spawn():
if __name__ == '__main__':
try:
spawn()
while True: sleep(100)
while True:
sleep(100)
except KeyboardInterrupt:
sys.exit()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册