1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

perf(ext/http): optimize away code based on callback length (#18849)

hello world on macOS:

```
divy@mini ~> wrk -d 10s --latency http://127.0.0.1:4500
Running 10s test @ http://127.0.0.1:4500
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    80.82us   42.95us   2.91ms   96.40%
    Req/Sec    56.91k     1.94k   60.77k    95.54%
  Latency Distribution
     50%   77.00us
     75%   89.00us
     90%  105.00us
     99%  146.00us
  1143455 requests in 10.10s, 138.49MB read
Requests/sec: 113212.38
Transfer/sec:     13.71MB

divy@mini ~> wrk -d 10s --latency http://127.0.0.1:4500
Running 10s test @ http://127.0.0.1:4500
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    88.63us   78.77us   2.55ms   98.72%
    Req/Sec    54.84k     2.16k   57.35k    98.51%
  Latency Distribution
     50%   80.00us
     75%   93.00us
     90%  109.00us
     99%  249.00us
  1102313 requests in 10.10s, 133.51MB read
Requests/sec: 109136.61
Transfer/sec:     13.22MB
```

Expected to have a larger impact on Linux
This commit is contained in:
Divy Srivastava 2023-04-26 22:11:54 +05:30 committed by GitHub
parent 17d1c7e444
commit 5f7db93d0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -435,16 +435,26 @@ async function asyncResponse(responseBodies, req, status, stream) {
*/
function mapToCallback(responseBodies, context, signal, callback, onError) {
return async function (req) {
const innerRequest = new InnerRequest(req, context);
const request = fromInnerRequest(innerRequest, signal, "immutable");
// Get the response from the user-provided callback. If that fails, use onError. If that fails, return a fallback
// 500 error.
let innerRequest;
let response;
try {
response = await callback(request, {
remoteAddr: innerRequest.remoteAddr,
});
if (callback.length > 0) {
innerRequest = new InnerRequest(req, context);
const request = fromInnerRequest(innerRequest, signal, "immutable");
if (callback.length === 1) {
response = await callback(request);
} else {
response = await callback(request, {
get remoteAddr() {
return innerRequest.remoteAddr;
},
});
}
} else {
response = await callback();
}
} catch (error) {
try {
response = await onError(error);
@ -455,19 +465,19 @@ function mapToCallback(responseBodies, context, signal, callback, onError) {
}
const inner = toInnerResponse(response);
if (innerRequest[_upgraded]) {
if (innerRequest?.[_upgraded]) {
// We're done here as the connection has been upgraded during the callback and no longer requires servicing.
if (response !== UPGRADE_RESPONSE_SENTINEL) {
console.error("Upgrade response was not returned from callback");
context.close();
}
innerRequest[_upgraded]();
innerRequest?.[_upgraded]();
return;
}
// Did everything shut down while we were waiting?
if (context.closed) {
innerRequest.close();
innerRequest?.close();
return;
}
@ -490,7 +500,7 @@ function mapToCallback(responseBodies, context, signal, callback, onError) {
core.ops.op_set_promise_complete(req, status);
}
innerRequest.close();
innerRequest?.close();
};
}