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

src: add fast path to TextEncoder.encodeInto #45701

Closed
wants to merge 3 commits into from

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Dec 1, 2022

Original commit message:

[fastcall] Implement support for onebyte string arguments

Refs: v8/v8@bc831f8

Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1259/

                                                                                 confidence improvement accuracy (*)    (**)   (***)
util/text-encoder.js op='encodeInto' type='v8-one-byte-string' n=10000 len=0                     1.99 %       ±4.94%  ±6.58%  ±8.57%
util/text-encoder.js op='encodeInto' type='v8-one-byte-string' n=10000 len=1024         ***     13.53 %       ±6.84%  ±9.10% ±11.86%
util/text-encoder.js op='encodeInto' type='v8-one-byte-string' n=10000 len=256                   5.36 %       ±6.97%  ±9.29% ±12.12%
util/text-encoder.js op='encodeInto' type='v8-one-byte-string' n=10000 len=32768        ***     15.07 %       ±7.99% ±10.63% ±13.84%
util/text-encoder.js op='encodeInto' type='v8-two-byte-string' n=10000 len=0                    -5.38 %       ±6.23%  ±8.31% ±10.85%
util/text-encoder.js op='encodeInto' type='v8-two-byte-string' n=10000 len=1024                  1.45 %       ±6.77%  ±9.01% ±11.74%
util/text-encoder.js op='encodeInto' type='v8-two-byte-string' n=10000 len=256                  -2.36 %       ±7.55% ±10.05% ±13.08%
util/text-encoder.js op='encodeInto' type='v8-two-byte-string' n=10000 len=32768                -0.49 %       ±2.34%  ±3.11%  ±4.06%
util/text-encoder.js op='encode' type='v8-one-byte-string' n=10000 len=0                         1.62 %       ±6.95%  ±9.25% ±12.07%
util/text-encoder.js op='encode' type='v8-one-byte-string' n=10000 len=1024                     -2.32 %       ±5.28%  ±7.03%  ±9.17%
util/text-encoder.js op='encode' type='v8-one-byte-string' n=10000 len=256                      -4.58 %       ±6.07%  ±8.08% ±10.54%
util/text-encoder.js op='encode' type='v8-one-byte-string' n=10000 len=32768                     4.25 %      ±11.64% ±15.49% ±20.16%
util/text-encoder.js op='encode' type='v8-two-byte-string' n=10000 len=0                        -6.16 %       ±6.84%  ±9.11% ±11.86%
util/text-encoder.js op='encode' type='v8-two-byte-string' n=10000 len=1024                     -6.52 %       ±7.40%  ±9.85% ±12.83%
util/text-encoder.js op='encode' type='v8-two-byte-string' n=10000 len=256                      -6.35 %       ±6.74%  ±8.97% ±11.68%
util/text-encoder.js op='encode' type='v8-two-byte-string' n=10000 len=32768                    -5.08 %       ±7.21%  ±9.60% ±12.50%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 16 comparisons, you can thus
expect the following amount of false-positive results:
  0.80 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.16 false positives, when considering a   1% risk acceptance (**, ***),
  0.02 false positives, when considering a 0.1% risk acceptance (***)

Original commit message:

    [fastcall] Implement support for onebyte string arguments

    This CL adds one byte string specialization support for fast API call arguments.
    It introduces a kOneByteString variant to CTypeInfo.

    We see a ~6x improvement in Deno's TextEncoder#encode microbenchmark.
    Rendered results: https://divy-v8-patches.deno.dev/

    Bug: chromium:1052746
    Change-Id: I47c3a9e101cd18ddc6ad58f627db3a34231b60f7
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4036884
    Reviewed-by: Toon Verwaest <verwaest@chromium.org>
    Reviewed-by: Maya Lekova <mslekova@chromium.org>
    Commit-Queue: Maya Lekova <mslekova@chromium.org>
    Cr-Commit-Position: refs/heads/main@{#84552}

Refs: v8/v8@bc831f8
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/gyp
  • @nodejs/v8-update

@nodejs-github-bot nodejs-github-bot added build Issues and PRs related to build files or the CI. needs-ci PRs that need a full CI run. v8 engine Issues and PRs related to the V8 dependency. labels Dec 1, 2022
@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 1, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 1, 2022
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

targos
targos previously requested changes Dec 1, 2022
Copy link
Member

@targos targos left a comment

Choose a reason for hiding this comment

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

I would be more inclined to accept this if the new capabilities were used by a subsequent commit. Otherwise the change doesn't bring anything to Node.js.

@anonrig
Copy link
Member Author

anonrig commented Dec 1, 2022

I would be more inclined to accept this if the new capabilities were used by a subsequent commit. Otherwise the change doesn't bring anything to Node.js.

@targos Should I include my changes depending on this to this particular pull request, and add commit-queue-rebase?

@targos
Copy link
Member

targos commented Dec 1, 2022

That would be fine, yes.

@ShogunPanda
Copy link
Contributor

These changes look fine to me. I'm approving, but I agree on what @targos said.

@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig changed the title deps: V8: cherry-pick bc831f8ba33b src: add fast path to TextEncoder Dec 2, 2022
@targos targos dismissed their stale review December 2, 2022 16:10

resolved

@anonrig anonrig added the review wanted PRs that need reviews. label Dec 4, 2022
src/node_encoding.cc Outdated Show resolved Hide resolved
lib/internal/encoding.js Outdated Show resolved Hide resolved
@devsnek devsnek self-requested a review December 4, 2022 01:25
@anonrig anonrig requested a review from mscdex December 4, 2022 01:27
@anonrig anonrig changed the title src: add fast path to TextEncoder [WIP] src: add fast path to TextEncoder Dec 4, 2022
@@ -498,6 +498,7 @@
'src/node_contextify.cc',
'src/node_credentials.cc',
'src/node_dir.cc',
'src/node_encoding.cc',
Copy link
Member

Choose a reason for hiding this comment

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

Nit: we already have so many files dealing with encodings, does this really need to be a new one (instead of, e.g., staying in node_buffer or maybe string_bytes)? And if so, is this the encoding file?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don’t have any specific thoughts on this. I’ll try to move the encode utf8 to the node_encoding once this is implementation improves the existing benchmark.

Copy link
Member

Choose a reason for hiding this comment

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

I'm still not sure why this is a new file when it only covers a tiny part of all encoding-related routines.

src/node_encoding.cc Outdated Show resolved Hide resolved
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@anonrig
Copy link
Member Author

anonrig commented Dec 6, 2022

@tniessen Can you review this, since in your last review, you requested changes?

@anonrig anonrig added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 6, 2022
lib/internal/encoding.js Outdated Show resolved Hide resolved
lib/internal/encoding.js Outdated Show resolved Hide resolved
src/node_encoding.cc Outdated Show resolved Hide resolved
src/node_encoding.cc Outdated Show resolved Hide resolved
anonrig and others added 2 commits December 7, 2022 13:29
@anonrig
Copy link
Member Author

anonrig commented Dec 8, 2022

@targos If it's ok, I recommend creating a pull request for merging the v8 cherry-pick, and continue this performance experiment in this pull request. There are several new areas (wasm for example) that can leverage this cherry-pick.

Comment on lines +14 to +15
// For loop is required to trigger the fast path for encodeInto
// Since v8 fast path is only triggered when v8 optimization starts.
Copy link
Member

Choose a reason for hiding this comment

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

There is no guarantee that V8 would take the fast path.

Suggested change
// For loop is required to trigger the fast path for encodeInto
// Since v8 fast path is only triggered when v8 optimization starts.
// Using a loop increases the chances of triggering the fast path for encodeInto
// because V8 heuristically optimizes based on information gathered at runtime.

@@ -498,6 +498,7 @@
'src/node_contextify.cc',
'src/node_credentials.cc',
'src/node_dir.cc',
'src/node_encoding.cc',
Copy link
Member

Choose a reason for hiding this comment

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

I'm still not sure why this is a new file when it only covers a tiny part of all encoding-related routines.

@aduh95 aduh95 removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 10, 2022
@aduh95
Copy link
Contributor

aduh95 commented Dec 10, 2022

There are git conflicts, no Jenkins CI run on the last commit, and two collaborators blocking the PR, removing author ready PRs that have at least one approval, no pending requests for changes, and a CI started. .

@anonrig
Copy link
Member Author

anonrig commented Dec 10, 2022

Thanks, @aduh95. Since other pull requests are merged, I’ll update this pull request only focusing on encodeInto.

@anonrig anonrig removed the review wanted PRs that need reviews. label Dec 14, 2022
@targos targos removed their request for review March 10, 2023 15:44
@anonrig anonrig closed this Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Issues and PRs related to build files or the CI. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. needs-ci PRs that need a full CI run. v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants