mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
perf(ext/http): recover memory for serve and optimize AbortController (#23559)
Max rps without a signal is unchanged, however we can drastically reduce memory usage by not creating the signal until needed, and we can optimize the rps in the case where the signal is created. With a quick memory benchmark, it looks like this helps pretty drastically with # of GCs when benchmarking w/wrk: - 1.42.4: 1763 - canary: 1093 - this patch: 874 This branch: ``` Running 10s test @ http://localhost:8080/ 2 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev Latency 87.33us 439.95us 20.68ms 99.67% Req/Sec 66.70k 6.39k 74.11k 83.66% 1340255 requests in 10.10s, 191.73MB read Requests/sec: 132696.90 Transfer/sec: 18.98MB cpu: Apple M2 Pro runtime: deno 1.43.0 (aarch64-apple-darwin) file:///Users/matt/Documents/scripts/bench_request.js benchmark time (avg) iter/s (min … max) p75 p99 p995 ----------------------------------------------------------------------------------------------- ----------------------------- newRequest 986.5 ns/iter 1,013,682.6 (878.2 ns … 1.18 µs) 1.01 µs 1.18 µs 1.18 µs newAbortController 18 ns/iter 55,541,104.1 (15.6 ns … 42.62 ns) 17.71 ns 25.05 ns 26.27 ns newAbortControllerSignal 18.66 ns/iter 53,578,966.7 (16.49 ns … 32.16 ns) 18.71 ns 25.67 ns 26.39 ns newAbortControllerSignalOnAbort 106.49 ns/iter 9,390,164.9 (97.87 ns … 120.61 ns) 108.6 ns 114.24 ns 115.89 ns newAbortControllerSignalAddEventListener 86.92 ns/iter 11,504,880.2 (81.88 ns … 103.15 ns) 90 ns 98.28 ns 99.55 ns newAbortControllerSignalOnAbortNoListener 3.01 µs/iter 331,964.4 (2.97 µs … 3.1 µs) 3.06 µs 3.1 µs 3.1 µs newAbortControllerSignalOnAbortAbort 3.26 µs/iter 306,662.6 (3.22 µs … 3.36 µs) 3.27 µs 3.36 µs 3.36 µs ``` Latest canary: ``` Running 10s test @ http://localhost:8080/ 2 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev Latency 72.86us 71.23us 4.47ms 99.05% Req/Sec 64.66k 5.54k 72.48k 82.18% 1299015 requests in 10.10s, 185.83MB read Requests/sec: 128616.02 Transfer/sec: 18.40MB cpu: Apple M2 Pro runtime: deno 1.43.0+bc4aa5f (aarch64-apple-darwin) file:///Users/matt/Documents/scripts/bench_request.js benchmark time (avg) iter/s (min … max) p75 p99 p995 ----------------------------------------------------------------------------------------------- ----------------------------- newRequest 1.25 µs/iter 800,005.2 (1.01 µs … 4.18 µs) 1.16 µs 4.18 µs 4.18 µs newAbortController 18.56 ns/iter 53,868,204.3 (16.04 ns … 38.73 ns) 18.38 ns 26.1 ns 26.63 ns newAbortControllerSignal 18.72 ns/iter 53,430,746.1 (16.13 ns … 36.71 ns) 18.71 ns 26.19 ns 26.98 ns newAbortControllerSignalOnAbort 193.91 ns/iter 5,156,992.4 (184.25 ns … 211.41 ns) 194.96 ns 207.87 ns 209.4 ns newAbortControllerSignalAddEventListener 171.45 ns/iter 5,832,569.2 (153 ns … 182.03 ns) 176.17 ns 180.75 ns 181.05 ns newAbortControllerSignalOnAbortNoListener 3.07 µs/iter 326,263.3 (2.98 µs … 3.17 µs) 3.08 µs 3.17 µs 3.17 µs newAbortControllerSignalOnAbortAbort 3.32 µs/iter 301,344.6 (3.29 µs … 3.4 µs) 3.33 µs 3.4 µs 3.4 µs ```
This commit is contained in:
parent
bc4aa5f901
commit
084eafe508
5 changed files with 79 additions and 62 deletions
|
@ -14,6 +14,7 @@ const {
|
||||||
ArrayPrototypeMap,
|
ArrayPrototypeMap,
|
||||||
ArrayPrototypeSlice,
|
ArrayPrototypeSlice,
|
||||||
ArrayPrototypeSplice,
|
ArrayPrototypeSplice,
|
||||||
|
ObjectFreeze,
|
||||||
ObjectKeys,
|
ObjectKeys,
|
||||||
ObjectPrototypeIsPrototypeOf,
|
ObjectPrototypeIsPrototypeOf,
|
||||||
RegExpPrototypeExec,
|
RegExpPrototypeExec,
|
||||||
|
@ -24,7 +25,6 @@ const {
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
||||||
import { assert } from "ext:deno_web/00_infra.js";
|
|
||||||
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
||||||
import {
|
import {
|
||||||
byteUpperCase,
|
byteUpperCase,
|
||||||
|
@ -43,8 +43,12 @@ import {
|
||||||
headersFromHeaderList,
|
headersFromHeaderList,
|
||||||
} from "ext:deno_fetch/20_headers.js";
|
} from "ext:deno_fetch/20_headers.js";
|
||||||
import { HttpClientPrototype } from "ext:deno_fetch/22_http_client.js";
|
import { HttpClientPrototype } from "ext:deno_fetch/22_http_client.js";
|
||||||
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
|
import {
|
||||||
|
createDependentAbortSignal,
|
||||||
|
newSignal,
|
||||||
|
signalAbort,
|
||||||
|
} from "ext:deno_web/03_abort_signal.js";
|
||||||
|
import { DOMException } from "ext:deno_web/01_dom_exception.js";
|
||||||
const { internalRidSymbol } = core;
|
const { internalRidSymbol } = core;
|
||||||
|
|
||||||
const _request = Symbol("request");
|
const _request = Symbol("request");
|
||||||
|
@ -52,6 +56,7 @@ const _headers = Symbol("headers");
|
||||||
const _getHeaders = Symbol("get headers");
|
const _getHeaders = Symbol("get headers");
|
||||||
const _headersCache = Symbol("headers cache");
|
const _headersCache = Symbol("headers cache");
|
||||||
const _signal = Symbol("signal");
|
const _signal = Symbol("signal");
|
||||||
|
const _signalCache = Symbol("signalCache");
|
||||||
const _mimeType = Symbol("mime type");
|
const _mimeType = Symbol("mime type");
|
||||||
const _body = Symbol("body");
|
const _body = Symbol("body");
|
||||||
const _url = Symbol("url");
|
const _url = Symbol("url");
|
||||||
|
@ -262,7 +267,13 @@ class Request {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {AbortSignal} */
|
/** @type {AbortSignal} */
|
||||||
[_signal];
|
get [_signal]() {
|
||||||
|
const signal = this[_signalCache];
|
||||||
|
if (signal !== undefined) {
|
||||||
|
return signal;
|
||||||
|
}
|
||||||
|
return (this[_signalCache] = newSignal());
|
||||||
|
}
|
||||||
get [_mimeType]() {
|
get [_mimeType]() {
|
||||||
const values = getDecodeSplitHeader(
|
const values = getDecodeSplitHeader(
|
||||||
headerListFromHeaders(this[_headers]),
|
headerListFromHeaders(this[_headers]),
|
||||||
|
@ -363,11 +374,10 @@ class Request {
|
||||||
// 28.
|
// 28.
|
||||||
this[_request] = request;
|
this[_request] = request;
|
||||||
|
|
||||||
// 29.
|
// 29 & 30.
|
||||||
const signals = signal !== null ? [signal] : [];
|
if (signal !== null) {
|
||||||
|
this[_signalCache] = createDependentAbortSignal([signal], prefix);
|
||||||
// 30.
|
}
|
||||||
this[_signal] = abortSignal.createDependentAbortSignal(signals, prefix);
|
|
||||||
|
|
||||||
// 31.
|
// 31.
|
||||||
this[_headers] = headersFromHeaderList(request.headerList, "request");
|
this[_headers] = headersFromHeaderList(request.headerList, "request");
|
||||||
|
@ -473,17 +483,21 @@ class Request {
|
||||||
}
|
}
|
||||||
const clonedReq = cloneInnerRequest(this[_request]);
|
const clonedReq = cloneInnerRequest(this[_request]);
|
||||||
|
|
||||||
assert(this[_signal] !== null);
|
const materializedSignal = this[_signal];
|
||||||
const clonedSignal = abortSignal.createDependentAbortSignal(
|
const clonedSignal = createDependentAbortSignal(
|
||||||
[this[_signal]],
|
[materializedSignal],
|
||||||
prefix,
|
prefix,
|
||||||
);
|
);
|
||||||
|
|
||||||
return fromInnerRequest(
|
const request = new Request(_brand);
|
||||||
clonedReq,
|
request[_request] = clonedReq;
|
||||||
clonedSignal,
|
request[_signalCache] = clonedSignal;
|
||||||
|
request[_getHeaders] = () =>
|
||||||
|
headersFromHeaderList(
|
||||||
|
clonedReq.headerList,
|
||||||
guardFromHeaders(this[_headers]),
|
guardFromHeaders(this[_headers]),
|
||||||
);
|
);
|
||||||
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
||||||
|
@ -562,19 +576,30 @@ function toInnerRequest(request) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {InnerRequest} inner
|
* @param {InnerRequest} inner
|
||||||
* @param {AbortSignal} signal
|
|
||||||
* @param {"request" | "immutable" | "request-no-cors" | "response" | "none"} guard
|
* @param {"request" | "immutable" | "request-no-cors" | "response" | "none"} guard
|
||||||
* @returns {Request}
|
* @returns {Request}
|
||||||
*/
|
*/
|
||||||
function fromInnerRequest(inner, signal, guard) {
|
function fromInnerRequest(inner, guard) {
|
||||||
const request = new Request(_brand);
|
const request = new Request(_brand);
|
||||||
request[_request] = inner;
|
request[_request] = inner;
|
||||||
request[_signal] = signal;
|
|
||||||
request[_getHeaders] = () => headersFromHeaderList(inner.headerList, guard);
|
request[_getHeaders] = () => headersFromHeaderList(inner.headerList, guard);
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const signalAbortError = new DOMException(
|
||||||
|
"The request has been cancelled.",
|
||||||
|
"AbortError",
|
||||||
|
);
|
||||||
|
ObjectFreeze(signalAbortError);
|
||||||
|
|
||||||
|
function abortRequest(request) {
|
||||||
|
if (request[_signal]) {
|
||||||
|
request[_signal][signalAbort](signalAbortError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
abortRequest,
|
||||||
fromInnerRequest,
|
fromInnerRequest,
|
||||||
newInnerRequest,
|
newInnerRequest,
|
||||||
processUrlList,
|
processUrlList,
|
||||||
|
|
1
ext/fetch/internal.d.ts
vendored
1
ext/fetch/internal.d.ts
vendored
|
@ -70,7 +70,6 @@ declare module "ext:deno_fetch/26_fetch.js" {
|
||||||
function toInnerRequest(request: Request): InnerRequest;
|
function toInnerRequest(request: Request): InnerRequest;
|
||||||
function fromInnerRequest(
|
function fromInnerRequest(
|
||||||
inner: InnerRequest,
|
inner: InnerRequest,
|
||||||
signal: AbortSignal | null,
|
|
||||||
guard:
|
guard:
|
||||||
| "request"
|
| "request"
|
||||||
| "immutable"
|
| "immutable"
|
||||||
|
|
|
@ -49,7 +49,11 @@ import {
|
||||||
ResponsePrototype,
|
ResponsePrototype,
|
||||||
toInnerResponse,
|
toInnerResponse,
|
||||||
} from "ext:deno_fetch/23_response.js";
|
} from "ext:deno_fetch/23_response.js";
|
||||||
import { fromInnerRequest, toInnerRequest } from "ext:deno_fetch/23_request.js";
|
import {
|
||||||
|
abortRequest,
|
||||||
|
fromInnerRequest,
|
||||||
|
toInnerRequest,
|
||||||
|
} from "ext:deno_fetch/23_request.js";
|
||||||
import { AbortController } from "ext:deno_web/03_abort_signal.js";
|
import { AbortController } from "ext:deno_web/03_abort_signal.js";
|
||||||
import {
|
import {
|
||||||
_eventLoop,
|
_eventLoop,
|
||||||
|
@ -126,8 +130,6 @@ function addTrailers(resp, headerList) {
|
||||||
op_http_set_response_trailers(inner.external, headerList);
|
op_http_set_response_trailers(inner.external, headerList);
|
||||||
}
|
}
|
||||||
|
|
||||||
let signalAbortError;
|
|
||||||
|
|
||||||
class InnerRequest {
|
class InnerRequest {
|
||||||
#external;
|
#external;
|
||||||
#context;
|
#context;
|
||||||
|
@ -137,14 +139,13 @@ class InnerRequest {
|
||||||
#upgraded;
|
#upgraded;
|
||||||
#urlValue;
|
#urlValue;
|
||||||
#completed;
|
#completed;
|
||||||
#abortController;
|
request;
|
||||||
|
|
||||||
constructor(external, context, abortController) {
|
constructor(external, context) {
|
||||||
this.#external = external;
|
this.#external = external;
|
||||||
this.#context = context;
|
this.#context = context;
|
||||||
this.#upgraded = false;
|
this.#upgraded = false;
|
||||||
this.#completed = undefined;
|
this.#completed = undefined;
|
||||||
this.#abortController = abortController;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(success = true) {
|
close(success = true) {
|
||||||
|
@ -158,15 +159,7 @@ class InnerRequest {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!signalAbortError) {
|
abortRequest(this.request);
|
||||||
signalAbortError = new DOMException(
|
|
||||||
"The request has been cancelled.",
|
|
||||||
"AbortError",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Unconditionally abort the request signal. Note that we don't use
|
|
||||||
// an error here.
|
|
||||||
this.#abortController.abort(signalAbortError);
|
|
||||||
this.#external = null;
|
this.#external = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -492,17 +485,16 @@ function fastSyncResponseOrStream(
|
||||||
*/
|
*/
|
||||||
function mapToCallback(context, callback, onError) {
|
function mapToCallback(context, callback, onError) {
|
||||||
return async function (req) {
|
return async function (req) {
|
||||||
const abortController = new AbortController();
|
|
||||||
const signal = abortController.signal;
|
|
||||||
|
|
||||||
// Get the response from the user-provided callback. If that fails, use onError. If that fails, return a fallback
|
// Get the response from the user-provided callback. If that fails, use onError. If that fails, return a fallback
|
||||||
// 500 error.
|
// 500 error.
|
||||||
let innerRequest;
|
let innerRequest;
|
||||||
let response;
|
let response;
|
||||||
try {
|
try {
|
||||||
innerRequest = new InnerRequest(req, context, abortController);
|
innerRequest = new InnerRequest(req, context);
|
||||||
|
const request = fromInnerRequest(innerRequest, "immutable");
|
||||||
|
innerRequest.request = request;
|
||||||
response = await callback(
|
response = await callback(
|
||||||
fromInnerRequest(innerRequest, signal, "immutable"),
|
request,
|
||||||
new ServeHandlerInfo(innerRequest),
|
new ServeHandlerInfo(innerRequest),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -38,10 +38,10 @@ import {
|
||||||
toInnerResponse,
|
toInnerResponse,
|
||||||
} from "ext:deno_fetch/23_response.js";
|
} from "ext:deno_fetch/23_response.js";
|
||||||
import {
|
import {
|
||||||
|
abortRequest,
|
||||||
fromInnerRequest,
|
fromInnerRequest,
|
||||||
newInnerRequest,
|
newInnerRequest,
|
||||||
} from "ext:deno_fetch/23_request.js";
|
} from "ext:deno_fetch/23_request.js";
|
||||||
import { AbortController } from "ext:deno_web/03_abort_signal.js";
|
|
||||||
import {
|
import {
|
||||||
_eventLoop,
|
_eventLoop,
|
||||||
_idleTimeoutDuration,
|
_idleTimeoutDuration,
|
||||||
|
@ -147,19 +147,17 @@ class HttpConn {
|
||||||
body !== null ? new InnerBody(body) : null,
|
body !== null ? new InnerBody(body) : null,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
const abortController = new AbortController();
|
|
||||||
const request = fromInnerRequest(
|
const request = fromInnerRequest(
|
||||||
innerRequest,
|
innerRequest,
|
||||||
abortController.signal,
|
|
||||||
"immutable",
|
"immutable",
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
const respondWith = createRespondWith(
|
const respondWith = createRespondWith(
|
||||||
this,
|
this,
|
||||||
|
request,
|
||||||
readStreamRid,
|
readStreamRid,
|
||||||
writeStreamRid,
|
writeStreamRid,
|
||||||
abortController,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return { request, respondWith };
|
return { request, respondWith };
|
||||||
|
@ -200,9 +198,9 @@ class HttpConn {
|
||||||
|
|
||||||
function createRespondWith(
|
function createRespondWith(
|
||||||
httpConn,
|
httpConn,
|
||||||
|
request,
|
||||||
readStreamRid,
|
readStreamRid,
|
||||||
writeStreamRid,
|
writeStreamRid,
|
||||||
abortController,
|
|
||||||
) {
|
) {
|
||||||
return async function respondWith(resp) {
|
return async function respondWith(resp) {
|
||||||
try {
|
try {
|
||||||
|
@ -384,7 +382,7 @@ function createRespondWith(
|
||||||
ws[_serverHandleIdleTimeout]();
|
ws[_serverHandleIdleTimeout]();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
abortController.abort(error);
|
abortRequest(request);
|
||||||
throw error;
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
if (deleteManagedResource(httpConn, readStreamRid)) {
|
if (deleteManagedResource(httpConn, readStreamRid)) {
|
||||||
|
|
|
@ -7,8 +7,8 @@ import { primordials } from "ext:core/mod.js";
|
||||||
const {
|
const {
|
||||||
ArrayPrototypeEvery,
|
ArrayPrototypeEvery,
|
||||||
ArrayPrototypePush,
|
ArrayPrototypePush,
|
||||||
|
FunctionPrototypeApply,
|
||||||
ObjectPrototypeIsPrototypeOf,
|
ObjectPrototypeIsPrototypeOf,
|
||||||
SafeArrayIterator,
|
|
||||||
SafeSet,
|
SafeSet,
|
||||||
SafeSetIterator,
|
SafeSetIterator,
|
||||||
SafeWeakRef,
|
SafeWeakRef,
|
||||||
|
@ -82,6 +82,14 @@ const timerId = Symbol("[[timerId]]");
|
||||||
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
||||||
|
|
||||||
class AbortSignal extends EventTarget {
|
class AbortSignal extends EventTarget {
|
||||||
|
[abortReason] = undefined;
|
||||||
|
[abortAlgos] = null;
|
||||||
|
[dependent] = false;
|
||||||
|
[sourceSignals] = null;
|
||||||
|
[dependentSignals] = null;
|
||||||
|
[timerId] = null;
|
||||||
|
[webidl.brand] = webidl.brand;
|
||||||
|
|
||||||
static any(signals) {
|
static any(signals) {
|
||||||
const prefix = "Failed to execute 'AbortSignal.any'";
|
const prefix = "Failed to execute 'AbortSignal.any'";
|
||||||
webidl.requiredArguments(arguments.length, 1, prefix);
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
||||||
|
@ -141,9 +149,11 @@ class AbortSignal extends EventTarget {
|
||||||
const algos = this[abortAlgos];
|
const algos = this[abortAlgos];
|
||||||
this[abortAlgos] = null;
|
this[abortAlgos] = null;
|
||||||
|
|
||||||
|
if (listenerCount(this, "abort") > 0) {
|
||||||
const event = new Event("abort");
|
const event = new Event("abort");
|
||||||
setIsTrusted(event, true);
|
setIsTrusted(event, true);
|
||||||
super.dispatchEvent(event);
|
super.dispatchEvent(event);
|
||||||
|
}
|
||||||
if (algos !== null) {
|
if (algos !== null) {
|
||||||
for (const algorithm of new SafeSetIterator(algos)) {
|
for (const algorithm of new SafeSetIterator(algos)) {
|
||||||
algorithm();
|
algorithm();
|
||||||
|
@ -168,13 +178,6 @@ class AbortSignal extends EventTarget {
|
||||||
throw new TypeError("Illegal constructor.");
|
throw new TypeError("Illegal constructor.");
|
||||||
}
|
}
|
||||||
super();
|
super();
|
||||||
this[abortReason] = undefined;
|
|
||||||
this[abortAlgos] = null;
|
|
||||||
this[dependent] = false;
|
|
||||||
this[sourceSignals] = null;
|
|
||||||
this[dependentSignals] = null;
|
|
||||||
this[timerId] = null;
|
|
||||||
this[webidl.brand] = webidl.brand;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get aborted() {
|
get aborted() {
|
||||||
|
@ -199,8 +202,8 @@ class AbortSignal extends EventTarget {
|
||||||
// `[add]` and `[remove]` don't ref and unref the timer because they can
|
// `[add]` and `[remove]` don't ref and unref the timer because they can
|
||||||
// only be used by Deno internals, which use it to essentially cancel async
|
// only be used by Deno internals, which use it to essentially cancel async
|
||||||
// ops which would block the event loop.
|
// ops which would block the event loop.
|
||||||
addEventListener(...args) {
|
addEventListener() {
|
||||||
super.addEventListener(...new SafeArrayIterator(args));
|
FunctionPrototypeApply(super.addEventListener, this, arguments);
|
||||||
if (listenerCount(this, "abort") > 0) {
|
if (listenerCount(this, "abort") > 0) {
|
||||||
if (this[timerId] !== null) {
|
if (this[timerId] !== null) {
|
||||||
refTimer(this[timerId]);
|
refTimer(this[timerId]);
|
||||||
|
@ -216,8 +219,8 @@ class AbortSignal extends EventTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeEventListener(...args) {
|
removeEventListener() {
|
||||||
super.removeEventListener(...new SafeArrayIterator(args));
|
FunctionPrototypeApply(super.removeEventListener, this, arguments);
|
||||||
if (listenerCount(this, "abort") === 0) {
|
if (listenerCount(this, "abort") === 0) {
|
||||||
if (this[timerId] !== null) {
|
if (this[timerId] !== null) {
|
||||||
unrefTimer(this[timerId]);
|
unrefTimer(this[timerId]);
|
||||||
|
|
Loading…
Reference in a new issue