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

buffer: make compare/equals faster #52993

Merged
merged 1 commit into from
May 16, 2024

Conversation

tniessen
Copy link
Member

@tniessen tniessen commented May 14, 2024

This patch adds a V8 fast API implementation for the buffer.compare binding, which is used both by Buffer.prototype.equals and Buffer.prototype.compare. In particular, it significantly improves the performance of comparing buffers for equality.

Local benchmark:

                                                                      confidence improvement accuracy (*)    (**)   (***)
buffers/buffer-compare-instance-method.js n=1000000 args=1 size=16           ***    225.73 %      ±16.81% ±22.62% ±29.94%
buffers/buffer-compare-instance-method.js n=1000000 args=1 size=16386        ***     15.57 %       ±1.32%  ±1.76%  ±2.29%
buffers/buffer-compare-instance-method.js n=1000000 args=1 size=4096         ***     61.44 %       ±6.15%  ±8.27% ±10.93%
buffers/buffer-compare-instance-method.js n=1000000 args=1 size=512          ***    181.97 %       ±7.02%  ±9.45% ±12.52%
buffers/buffer-compare-instance-method.js n=1000000 args=2 size=16                    1.82 %       ±3.08%  ±4.11%  ±5.39%
buffers/buffer-compare-instance-method.js n=1000000 args=2 size=16386                -0.02 %       ±1.38%  ±1.84%  ±2.39%
buffers/buffer-compare-instance-method.js n=1000000 args=2 size=4096           *     -2.30 %       ±2.21%  ±2.93%  ±3.82%
buffers/buffer-compare-instance-method.js n=1000000 args=2 size=512            *     -3.92 %       ±3.02%  ±4.02%  ±5.24%
buffers/buffer-compare-instance-method.js n=1000000 args=5 size=16                   -2.86 %       ±3.69%  ±4.93%  ±6.48%
buffers/buffer-compare-instance-method.js n=1000000 args=5 size=16386                -0.17 %       ±2.94%  ±3.93%  ±5.13%
buffers/buffer-compare-instance-method.js n=1000000 args=5 size=4096                 -0.62 %       ±2.00%  ±2.67%  ±3.47%
buffers/buffer-compare-instance-method.js n=1000000 args=5 size=512                  -1.16 %       ±3.34%  ±4.45%  ±5.79%
buffers/buffer-compare-offset.js n=1000000 size=16 method='offset'                   -0.75 %       ±3.85%  ±5.13%  ±6.68%
buffers/buffer-compare-offset.js n=1000000 size=16 method='slice'            ***     55.75 %       ±3.72%  ±4.97%  ±6.51%
buffers/buffer-compare-offset.js n=1000000 size=16386 method='offset'                 0.60 %       ±2.39%  ±3.19%  ±4.15%
buffers/buffer-compare-offset.js n=1000000 size=16386 method='slice'         ***     54.36 %       ±4.39%  ±5.85%  ±7.62%
buffers/buffer-compare-offset.js n=1000000 size=4096 method='offset'                 -0.17 %       ±2.52%  ±3.37%  ±4.41%
buffers/buffer-compare-offset.js n=1000000 size=4096 method='slice'          ***     53.48 %       ±4.26%  ±5.69%  ±7.44%
buffers/buffer-compare-offset.js n=1000000 size=512 method='offset'                   1.06 %       ±1.97%  ±2.63%  ±3.45%
buffers/buffer-compare-offset.js n=1000000 size=512 method='slice'           ***     49.36 %       ±5.02%  ±6.73%  ±8.88%
buffers/buffer-compare.js n=1000000 size=16                                  ***    136.22 %       ±6.78%  ±9.07% ±11.90%
buffers/buffer-compare.js n=1000000 size=16386                                 *      4.93 %       ±4.56%  ±6.10%  ±8.01%
buffers/buffer-compare.js n=1000000 size=4096                                ***     36.27 %       ±5.07%  ±6.79%  ±8.92%
buffers/buffer-compare.js n=1000000 size=512                                 ***    113.32 %       ±6.16%  ±8.27% ±10.92%
buffers/buffer-equals.js n=1000000 difflen='false' size=0                             0.39 %       ±2.76%  ±3.69%  ±4.84%
buffers/buffer-equals.js n=1000000 difflen='false' size=16386                ***     18.69 %       ±5.35%  ±7.20%  ±9.52%
buffers/buffer-equals.js n=1000000 difflen='false' size=512                  ***    141.31 %      ±10.23% ±13.77% ±18.22%
buffers/buffer-equals.js n=1000000 difflen='true' size=0                              0.23 %       ±2.81%  ±3.74%  ±4.87%
buffers/buffer-equals.js n=1000000 difflen='true' size=16386                 ***     -4.23 %       ±1.99%  ±2.66%  ±3.48%
buffers/buffer-equals.js n=1000000 difflen='true' size=512                           -1.28 %       ±4.89%  ±6.50%  ±8.47%

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

image

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

This patch adds a V8 fast API implementation for the buffer.compare
binding, which is used both by Buffer.prototype.equals and
Buffer.prototype.compare. In particular, it significantly improves the
performance of comparing buffers for equality.
@tniessen tniessen added buffer Issues and PRs related to the buffer subsystem. performance Issues and PRs related to the performance of Node.js. labels May 14, 2024
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels May 14, 2024
Copy link
Member

@anonrig anonrig left a comment

Choose a reason for hiding this comment

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

legend

@Trott Trott added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels May 15, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 15, 2024
Copy link
Member

@VoltrexKeyva VoltrexKeyva left a comment

Choose a reason for hiding this comment

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

Amazing!

@Trott Trott added the request-ci Add this label to start a Jenkins CI on a PR. label May 16, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 16, 2024
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label May 16, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label May 16, 2024
@nodejs-github-bot nodejs-github-bot merged commit 075853e into nodejs:main May 16, 2024
75 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 075853e

targos pushed a commit that referenced this pull request May 21, 2024
This patch adds a V8 fast API implementation for the buffer.compare
binding, which is used both by Buffer.prototype.equals and
Buffer.prototype.compare. In particular, it significantly improves the
performance of comparing buffers for equality.

PR-URL: #52993
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
lukins-cz pushed a commit to lukins-cz/OS-Aplet-node that referenced this pull request Jun 1, 2024
This patch adds a V8 fast API implementation for the buffer.compare
binding, which is used both by Buffer.prototype.equals and
Buffer.prototype.compare. In particular, it significantly improves the
performance of comparing buffers for equality.

PR-URL: nodejs#52993
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. performance Issues and PRs related to the performance of Node.js.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants