0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/core/examples/http_bench_json_ops.js
Aapo Alasuutari 2164f6b1eb
perf(ops): Monomorphic sync op calls (#15337)
Welcome to better optimised op calls! Currently opSync is called with parameters of every type and count. This most definitely makes the call megamorphic. Additionally, it seems that spread params leads to V8 not being able to optimise the calls quite as well (apparently Fast Calls cannot be used with spread params).

Monomorphising op calls should lead to some improved performance. Now that unwrapping of sync ops results is done on Rust side, this is pretty simple:

```
opSync("op_foo", param1, param2);
// -> turns to
ops.op_foo(param1, param2);
```

This means sync op calls are now just directly calling the native binding function. When V8 Fast API Calls are enabled, this will enable those to be called on the optimised path.

Monomorphising async ops likely requires using callbacks and is left as an exercise to the reader.
2022-08-11 15:56:56 +02:00

49 lines
1.2 KiB
JavaScript

// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This is not a real HTTP server. We read blindly one time into 'requestBuf',
// then write this fixed 'responseBuf'. The point of this benchmark is to
// exercise the event loop in a simple yet semi-realistic way.
const requestBuf = new Uint8Array(64 * 1024);
const responseBuf = new Uint8Array(
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"
.split("")
.map((c) => c.charCodeAt(0)),
);
/** Listens on 0.0.0.0:4500, returns rid. */
function listen() {
return Deno.core.ops.op_listen();
}
/** Accepts a connection, returns rid. */
function accept(serverRid) {
return Deno.core.opAsync("op_accept", serverRid);
}
async function serve(rid) {
try {
while (true) {
await Deno.core.read(rid, requestBuf);
await Deno.core.write(rid, responseBuf);
}
} catch (e) {
if (
!e.message.includes("Broken pipe") &&
!e.message.includes("Connection reset by peer")
) {
throw e;
}
}
Deno.core.close(rid);
}
async function main() {
const listenerRid = listen();
Deno.core.print(`http_bench_ops listening on http://127.0.0.1:4544/\n`);
while (true) {
const rid = await accept(listenerRid);
serve(rid);
}
}
main();