use Object instead of Map for promise table (#4309)

This commit is contained in:
Bartek Iwańczuk 2020-03-10 03:04:49 +01:00 committed by GitHub
parent 68119e1d7e
commit dca00211ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 8 deletions

View file

@ -19,7 +19,10 @@ interface JsonResponse {
promiseId?: number; // Only present in async messages. promiseId?: number; // Only present in async messages.
} }
const promiseTable = new Map<number, util.Resolvable<JsonResponse>>(); // Using an object without a prototype because `Map` was causing GC problems.
const promiseTable: {
[key: number]: util.Resolvable<JsonResponse>;
} = Object.create(null);
let _nextPromiseId = 1; let _nextPromiseId = 1;
function nextPromiseId(): number { function nextPromiseId(): number {
@ -48,9 +51,9 @@ export function asyncMsgFromRust(resUi8: Uint8Array): void {
const res = decode(resUi8); const res = decode(resUi8);
util.assert(res.promiseId != null); util.assert(res.promiseId != null);
const promise = promiseTable.get(res.promiseId!); const promise = promiseTable[res.promiseId!];
util.assert(promise != null); util.assert(promise != null);
promiseTable.delete(res.promiseId!); delete promiseTable[res.promiseId!];
promise.resolve(res); promise.resolve(res);
} }
@ -89,7 +92,7 @@ export async function sendAsync(
promise.resolve(res); promise.resolve(res);
} else { } else {
// Async result. // Async result.
promiseTable.set(promiseId, promise); promiseTable[promiseId] = promise;
} }
const res = await promise; const res = await promise;

View file

@ -4,7 +4,11 @@ import { core } from "../core.ts";
import { TextDecoder } from "../web/text_encoding.ts"; import { TextDecoder } from "../web/text_encoding.ts";
import { ErrorKind, errors, getErrorClass } from "../errors.ts"; import { ErrorKind, errors, getErrorClass } from "../errors.ts";
const promiseTableMin = new Map<number, util.Resolvable<RecordMinimal>>(); // Using an object without a prototype because `Map` was causing GC problems.
const promiseTableMin: {
[key: number]: util.Resolvable<RecordMinimal>;
} = Object.create(null);
// Note it's important that promiseId starts at 1 instead of 0, because sync // Note it's important that promiseId starts at 1 instead of 0, because sync
// messages are indicated with promiseId 0. If we ever add wrap around logic for // messages are indicated with promiseId 0. If we ever add wrap around logic for
// overflows, this should be taken into account. // overflows, this should be taken into account.
@ -72,8 +76,8 @@ util.assert(scratchBytes.byteLength === scratch32.length * 4);
export function asyncMsgFromRust(ui8: Uint8Array): void { export function asyncMsgFromRust(ui8: Uint8Array): void {
const record = recordFromBufMinimal(ui8); const record = recordFromBufMinimal(ui8);
const { promiseId } = record; const { promiseId } = record;
const promise = promiseTableMin.get(promiseId); const promise = promiseTableMin[promiseId];
promiseTableMin.delete(promiseId); delete promiseTableMin[promiseId];
util.assert(promise); util.assert(promise);
promise.resolve(record); promise.resolve(record);
} }
@ -95,7 +99,7 @@ export async function sendAsyncMinimal(
promise.resolve(record); promise.resolve(record);
} else { } else {
// Async result. // Async result.
promiseTableMin.set(promiseId, promise); promiseTableMin[promiseId] = promise;
} }
const res = await promise; const res = await promise;