Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve encoding #1768

Closed
wants to merge 2 commits into from
Closed

perf: improve encoding #1768

wants to merge 2 commits into from

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Nov 9, 2022

Before:

[bench:run] │ Tests               │ Samples │          Result │ Tolerance │ Difference with slowest │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - fetch      │       1 │ 1312.32 req/sec │  ± 0.00 % │                       - │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ http - no keepalive │       1 │ 2640.12 req/sec │  ± 0.00 % │              + 101.18 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ http - keepalive    │       1 │ 3609.65 req/sec │  ± 0.00 % │              + 175.06 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - request    │       1 │ 4525.88 req/sec │  ± 0.00 % │              + 244.87 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - pipeline   │       1 │ 5737.63 req/sec │  ± 0.00 % │              + 337.21 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - dispatch   │       1 │ 5812.50 req/sec │  ± 0.00 % │              + 342.92 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - stream     │       1 │ 7318.62 req/sec │  ± 0.00 % │              + 457.68 % │

After:

[bench:run] │ Tests               │ Samples │          Result │ Tolerance │ Difference with slowest │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - fetch      │       1 │ 1326.74 req/sec │  ± 0.00 % │                       - │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ http - no keepalive │       1 │ 2635.04 req/sec │  ± 0.00 % │               + 98.61 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ http - keepalive    │       1 │ 3757.30 req/sec │  ± 0.00 % │              + 183.20 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - request    │       1 │ 5283.23 req/sec │  ± 0.00 % │              + 298.21 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - dispatch   │       1 │ 5651.02 req/sec │  ± 0.00 % │              + 325.93 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - pipeline   │       1 │ 6096.91 req/sec │  ± 0.00 % │              + 359.54 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - stream     │       1 │ 7483.29 req/sec │  ± 0.00 % │              + 464.04 % │

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just the smallest change, lgtm otherwise

lib/fetch/body.js Outdated Show resolved Hide resolved
@ronag
Copy link
Member

ronag commented Nov 10, 2022

@mcollina isn't there security implication to this? i.e. what happens if a partial symbol stays buffered in the encoder between requests?

lib/fetch/util.js Outdated Show resolved Hide resolved
@anonrig anonrig requested review from KhafraDev and mcollina and removed request for KhafraDev and mcollina November 10, 2022 13:25
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there still a perf improvement?

@anonrig
Copy link
Member Author

anonrig commented Nov 10, 2022

Yes I updated the description of the pull request.

@mcollina
Copy link
Member

it does not seem to add much.

@anonrig
Copy link
Member Author

anonrig commented Nov 10, 2022

Mainly two reasons:

  1. The benchmark does not include headers
  2. https://www.youtube.com/watch?v=jrDikOz8YSU

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is worth it.

@KhafraDev
Copy link
Member

KhafraDev commented Nov 10, 2022

TextEncoder's constructor only sets a single symbol1, and TextDecoder seems to call into C++ land once2. With those benchmarks, I don't think there's really any performance gain considering creating a new TextEncoder shouldn't be slow.

Footnotes

  1. https://github.com/nodejs/node/blob/8cae0a117ecb0dd2c9f9bbb05fdd53f114906990/lib/internal/encoding.js#L327

  2. https://github.com/nodejs/node/blob/8cae0a117ecb0dd2c9f9bbb05fdd53f114906990/lib/internal/encoding.js#L401

@anonrig
Copy link
Member Author

anonrig commented Nov 10, 2022

I'm working on adding a fast path to TextDecoder for UTF8. I'll close this pull request.

@anonrig
Copy link
Member Author

anonrig commented Nov 10, 2022

With the addition of the fast path utf8 pull request, here's the new result for the benchmark:

[bench:run] │ Tests               │ Samples │          Result │ Tolerance │ Difference with slowest │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - fetch      │       1 │ 1668.14 req/sec │  ± 0.00 % │                       - │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ http - no keepalive │       1 │ 2820.24 req/sec │  ± 0.00 % │               + 69.07 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ http - keepalive    │       1 │ 4053.76 req/sec │  ± 0.00 % │              + 143.01 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - request    │       1 │ 4929.85 req/sec │  ± 0.00 % │              + 195.53 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - pipeline   │       1 │ 5840.36 req/sec │  ± 0.00 % │              + 250.11 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - stream     │       1 │ 7391.04 req/sec │  ± 0.00 % │              + 343.07 % │
[bench:run] |─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
[bench:run] │ undici - dispatch   │       1 │ 8433.93 req/sec │  ± 0.00 % │              + 405.59 % │

@mcollina
Copy link
Member

I like the improvement on all the methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants