Find a file
Marcos Casagrande f843a1fdff
perf(ext/headers): cache iterableHeaders for immutable Headers (#20132)
This PR caches `_iterableHeaders` for immutable `Headers` increasing the
performance of `fetch` & server if headers are iterated.

Should close #19466 

I only cached immutable headers to address this comment
https://github.com/denoland/deno/issues/19466#issuecomment-1589892373
since I didn't find any occurrence of header mutation on immutable
headers. We can discuss caching for non-immutable, but I think this is a
great first step.

## BENCHMARK

### Server
```js
const addr = Deno.args[0] ?? "127.0.0.1:4500";
const [hostname, port] = addr.split(":");
const { serve } = Deno;

serve({ hostname, port: Number(port), reusePort: true }, (req) => {
  const headers = [...req.headers]; // req.headers are immutable, cannot set/append/delete
  return new Response("ok");
});

```
Used `wrk` with 5 headers
```
wrk -d 10s --latency -H "X-Deno: true" -H "Accept: application/json" -H "X-Foo: bar" -H "User-Agent: wrk" -H "Accept-Encoding: gzip, br" http://127.0.0.1:4500
```


**This patch**
```
Running 10s test @ http://127.0.0.1:4500
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    70.18us   22.89us 679.00us   81.37%
    Req/Sec    71.55k     9.69k   82.18k    89.60%
  Latency Distribution
     50%   59.00us
     75%   89.00us
     90%   98.00us
     99%  159.00us
  1437891 requests in 10.10s, 193.35MB read
Requests/sec: 142369.83
Transfer/sec:     19.14MB
```
**main**
```
Running 10s test @ http://127.0.0.1:4500
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   112.78us   36.47us   2.09ms   77.99%
    Req/Sec    44.30k     1.65k   49.14k    74.26%
  Latency Distribution
     50%   99.00us
     75%  136.00us
     90%  162.00us
     99%  213.00us
  890588 requests in 10.10s, 118.91MB read
Requests/sec:  88176.37
Transfer/sec:     11.77MB
```
### fetch

```js
const res = await fetch('http://127.0.0.1:4500');
Deno.bench("Headers iterator", () => {
  const i = [...res.headers]; // res.headers are immutable, cannot set/append/delete
});
```

**this patch**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark             time (avg)        iter/s             (min … max)       p75       p99      p995
---------------------------------------------------------------------- -----------------------------
Headers iterator      329.5 ns/iter   3,034,909.0 (318.55 ns … 364.34 ns)  331.1 ns 355.72 ns 364.34 ns
```
**main**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark             time (avg)        iter/s             (min … max)       p75       p99      p995
---------------------------------------------------------------------- -----------------------------
Headers iterator       2.59 µs/iter     386,372.1     (2.56 µs … 2.68 µs)   2.59 µs   2.68 µs   2.68 µs
```
2023-08-12 10:42:06 -06:00
.cargo fix: reload config files on watcher restarts (#19487) 2023-06-14 22:29:19 +00:00
.devcontainer chore: update devcontainer configuration (#15751) 2022-09-03 02:21:41 +02:00
.github chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
bench_util chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
cli fix(node): implement TLSSocket._start (#20120) 2023-08-11 11:57:41 +00:00
ext perf(ext/headers): cache iterableHeaders for immutable Headers (#20132) 2023-08-12 10:42:06 -06:00
runtime chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
test_ffi chore: update to Rust 1.71 (#19822) 2023-07-13 15:16:24 -06:00
test_napi fix(node): don't print warning on process.dlopen.flags (#20124) 2023-08-10 20:19:20 +02:00
test_util chore: upgrade fastwebsockets to 0.4.4 (#19089) 2023-08-10 09:59:06 +05:30
third_party@7f1a41fee1 chore: upgrade to dprint 0.40 internally (#19998) 2023-07-31 17:10:10 +02:00
tools fix(ext/abort): trigger AbortSignal events in correct order (#20095) 2023-08-08 12:05:42 +02:00
.dlint.json chore: update dlint to v0.37.0 for GitHub Actions (#17295) 2023-01-16 17:17:18 +01:00
.dprint.json fix(fmt): do not insert expr stmt leading semi-colon in do while stmt body (#20093) 2023-08-08 09:15:19 -04:00
.editorconfig editorconfig: Don't insert final newline in .out files (#1686) 2019-02-07 11:31:49 -05:00
.gitattributes fix(test): support typechecking docs with CRLF line endings (#12748) 2021-11-15 09:58:04 -05:00
.gitignore feat(cli): Adding JUnit test reports (#19747) 2023-07-27 00:12:35 +02:00
.gitmodules chore: check node_compat config diff in CI (#19119) 2023-05-15 21:22:53 +09:00
.rustfmt.toml chore: update copyright year to 2023 (#17247) 2023-01-02 21:00:42 +00:00
Cargo.lock chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
Cargo.toml chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
LICENSE.md chore: update copyright year to 2023 (#17247) 2023-01-02 21:00:42 +00:00
README.md docs: nitpick README (#19912) 2023-07-25 14:44:50 +09:00
Releases.md chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
rust-toolchain.toml chore: update to Rust 1.71 (#19822) 2023-07-13 15:16:24 -06:00

Deno

Twitter badge Discord badge YouTube badge

the deno mascot dinosaur standing in the rain

Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

Features

Install

Shell (Mac, Linux):

curl -fsSL https://deno.land/install.sh | sh

PowerShell (Windows):

irm https://deno.land/install.ps1 | iex

Homebrew (Mac):

brew install deno

Chocolatey (Windows):

choco install deno

Scoop (Windows):

scoop install deno

Build and install from source using Cargo:

cargo install deno --locked

See deno_install and releases for other options.

Getting Started

Try running a simple program:

deno run https://deno.land/std/examples/welcome.ts

Or setup a simple HTTP server:

Deno.serve((_req) => new Response("Hello, World!"));

More Examples

Additional Resources

Contributing

We appreciate your help!

To contribute, please read our contributing instructions.