Skip to content

net: improve performance of isIPv4 and isIPv6 - #49568

Merged
nodejs-github-bot merged 5 commits into
nodejs:mainfrom
Uzlopak:patch-1
Sep 13, 2023
Merged

net: improve performance of isIPv4 and isIPv6#49568
nodejs-github-bot merged 5 commits into
nodejs:mainfrom
Uzlopak:patch-1

Conversation

@Uzlopak

@UzlopakUzlopak commented Sep 8, 2023

Copy link
Copy Markdown
Contributor

Basically making the a capturing group to be non capturing.

I dont know how to bench the performance on nodejs

This is my benchmark:

'use strict'const{isIPv4: isIPv4builtIn}=require('net')constbenchmark=require('benchmark')constsuite=newbenchmark.Suite()constipMax='255.255.255.255'constipMin='0.0.0.0'constinvalid='0.0.0.0.0'constisIPv4RE=/^(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/suite.add('RE',function(){isIPv4RE.test(ipMin)isIPv4RE.test(ipMax)isIPv4RE.test(invalid)})suite.add('builtIn',function(){isIPv4builtIn(ipMin)isIPv4builtIn(ipMax)isIPv4builtIn(invalid)})suite.on('cycle',functiononCycle(event){console.log(String(event.target))})suite.run({async: false})

result:

aras@aras-Lenovo-Legion-5-17ARH05H:~/workspace/proxy-addr$ node benchmark/is-ipv4.js
RE x 12,055,141 ops/sec ±1.49% (93 runs sampled)
builtIn x 8,672,332 ops/sec ±0.70% (94 runs sampled)

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. labels Sep 8, 2023
@Uzlopak

Copy link
Copy Markdown
ContributorAuthor

@anonriganonrig added performance Issues and PRs related to the performance of Node.js. request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Sep 8, 2023
@anonrig

Copy link
Copy Markdown
Member

Hey @Uzlopak, can you make sure the commit message conforms to the commit message guideline?

@Uzlopak

Copy link
Copy Markdown
ContributorAuthor

Just a remark: when i use following regex, i get even better perf. I think it reduces the backtracking. but i am not sure why. Do we have some Regex Expert?

/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/

node benchmark/is-ipv4.js
RE x 17,225,828 ops/sec ±0.74% (92 runs sampled)
builtIn x 9,251,894 ops/sec ±0.65% (91 runs sampled)

@Uzlopak

Copy link
Copy Markdown
ContributorAuthor

@anonrig

how can i edit the commit message? I would probably just undo last commit and force push. But what should be the commit message to conform?

@anonrig

Copy link
Copy Markdown
Member

You should amend and update your commit and force push using this guideline: https://github.com/nodejs/node/blob/main/doc/contributing/pull-requests.md#commit-message-guidelines

@Uzlopak

Uzlopak commented Sep 8, 2023

Copy link
Copy Markdown
ContributorAuthor

@anonrig

Also modified the IPv6 regex.

here is the benchmark:

'use strict'const{isIPv6: isIPv6builtIn}=require('net')constbenchmark=require('benchmark')constsuite=newbenchmark.Suite()constipLocal='::1'constisIPv6OrigRE=/^((?:(?:[0-9a-fA-F]{1,4}):){7}(?:(?:[0-9a-fA-F]{1,4})|:)|(?:(?:[0-9a-fA-F]{1,4}):){6}(?:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(?:[0-9a-fA-F]{1,4})|:)|(?:(?:[0-9a-fA-F]{1,4}):){5}(?::((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,2}|:)|(?:(?:[0-9a-fA-F]{1,4}):){4}(?:(:(?:[0-9a-fA-F]{1,4})){0,1}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,3}|:)|(?:(?:[0-9a-fA-F]{1,4}):){3}(?:(:(?:[0-9a-fA-F]{1,4})){0,2}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,4}|:)|(?:(?:[0-9a-fA-F]{1,4}):){2}(?:(:(?:[0-9a-fA-F]{1,4})){0,3}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,5}|:)|(?:(?:[0-9a-fA-F]{1,4}):){1}(?:(:(?:[0-9a-fA-F]{1,4})){0,4}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,6}|:)|(?::((?::(?:[0-9a-fA-F]{1,4})){0,5}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})?$/constisIPv6RE=/^(?:(?:(?:[0-9a-fA-F]{1,4}):){7}(?:(?:[0-9a-fA-F]{1,4})|:)|(?:(?:[0-9a-fA-F]{1,4}):){6}(?:(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(?:[0-9a-fA-F]{1,4})|:)|(?:(?:[0-9a-fA-F]{1,4}):){5}(?::(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,2}|:)|(?:(?:[0-9a-fA-F]{1,4}):){4}(?:(?::(?:[0-9a-fA-F]{1,4})){0,1}:(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,3}|:)|(?:(?:[0-9a-fA-F]{1,4}):){3}(?:(?::(?:[0-9a-fA-F]{1,4})){0,2}:(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,4}|:)|(?:(?:[0-9a-fA-F]{1,4}):){2}(?:(?::(?:[0-9a-fA-F]{1,4})){0,3}:(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,5}|:)|(?:(?:[0-9a-fA-F]{1,4}):){1}(?:(?::(?:[0-9a-fA-F]{1,4})){0,4}:(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,6}|:)|(?::(?:(?::(?:[0-9a-fA-F]{1,4})){0,5}:(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,7}|:)))(?:%[0-9a-zA-Z-.:]{1,})?$/suite.add('RE (modified)',function(){isIPv6RE.test(ipLocal)})suite.add('RE (original)',function(){isIPv6OrigRE.test(ipLocal)})suite.add('builtIn',function(){isIPv6builtIn(ipLocal)})suite.on('cycle',functiononCycle(event){console.log(String(event.target))})suite.run({async: false})

node benchmark/is-ipv6.js
RE (modified) x 14,264,155 ops/sec ±0.20% (94 runs sampled)
RE (original) x 9,326,170 ops/sec ±0.89% (85 runs sampled)
builtIn x 8,595,158 ops/sec ±0.33% (94 runs sampled)

@UzlopakUzlopak changed the title improve isIPv4 perfnet: improve performance of isIPv4 and isIPv6Sep 8, 2023
@anonriganonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 8, 2023
Comment threadlib/internal/net.js Outdated
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 9, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@Uzlopak

Copy link
Copy Markdown
ContributorAuthor

@anonrig
can you please retrigger?

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina

Copy link
Copy Markdown
Member

@Uzlopak could youn please add your benchmark using Node.js infrastructure in https://github.com/nodejs/node/tree/main/benchmark/net?

@Uzlopak

Uzlopak commented Sep 9, 2023

Copy link
Copy Markdown
ContributorAuthor

@mcollina

Something like this?

'use strict';constcommon=require('../common.js');const{ isIPv4 }=require('net');constminIPv4='0.0.0.0';constmaxIPv4='255.255.255.255';constinvalid='0.0.0.0.0';constbench=common.createBenchmark(main,{n: [1e8],});functionmain({ n }){bench.start();for(leti=0;i<n;++i){isIPv4(minIPv4);isIPv4(maxIPv4);isIPv4(invalid);}bench.end(n);}

@mcollina

Copy link
Copy Markdown
Member

You can run it as well locally to verify if it's working. https://github.com/nodejs/node/blob/main/doc/contributing/writing-and-running-benchmarks.md

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@Uzlopak

Copy link
Copy Markdown
ContributorAuthor

Does this need some kind of backporting? Or do we have to wait till release of node 22? :D

@ShogunPandaShogunPanda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollina

mcollina commented Sep 12, 2023

Copy link
Copy Markdown
Member

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

 confidence improvement accuracy (*) (**) (***)
net/net-is-ip-v4.js n=10000000 *** 51.74 % ±1.02% ±1.37% ±1.81%
net/net-is-ip-v6.js n=10000000 *** 31.71 % ±0.37% ±0.50% ±0.64%

@lpincalpinca added the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 13, 2023
@nodejs-github-botnodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Sep 13, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator
Commit Queue failed
- Loading data for nodejs/node/pull/49568
✔ Done loading data for nodejs/node/pull/49568
----------------------------------- PR info ------------------------------------
Title net: improve performance of isIPv4 and isIPv6 (#49568)
⚠ Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch Uzlopak:patch-1 -> nodejs:main
Labels net, performance, author ready, needs-ci
Commits 5
- net: improve performance of isIPv4 and isIPv6
- Update lib/internal/net.js
- add benchmarks
- improve benchmarks as requested
- add trailing comma
Committers 2
- uzlopak - GitHub PR-URL: https://github.com/nodejs/node/pull/49568
Reviewed-By: Matteo Collina Reviewed-By: Yagiz Nizipli Reviewed-By: Paolo Insogna Reviewed-By: Luigi Pinca ------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/49568
Reviewed-By: Matteo Collina Reviewed-By: Yagiz Nizipli Reviewed-By: Paolo Insogna Reviewed-By: Luigi Pinca --------------------------------------------------------------------------------
ℹ This PR was created on Fri, 08 Sep 2023 22:58:07 GMT
✔ Approvals: 4
✔ - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/49568#pullrequestreview-1618630253
✔ - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/49568#pullrequestreview-1618702262
✔ - Paolo Insogna (@ShogunPanda): https://github.com/nodejs/node/pull/49568#pullrequestreview-1619385610
✔ - Luigi Pinca (@lpinca): https://github.com/nodejs/node/pull/49568#pullrequestreview-1625294083
✔ Last GitHub CI successful
ℹ Last Full PR CI on 2023-09-12T01:06:42Z: https://ci.nodejs.org/job/node-test-pull-request/53901/
ℹ Last Benchmark CI on 2023-09-12T07:17:37Z: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1381/
- Querying data for job/node-test-pull-request/1381/
✔ Last Jenkins CI successful
--------------------------------------------------------------------------------
✔ No git cherry-pick in progress
✔ No git am in progress
✔ No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/main up to date...
From https://github.com/nodejs/node
* branch main -> FETCH_HEAD
✔ origin/main is now up-to-date
- Downloading patch for 49568
From https://github.com/nodejs/node
* branch refs/pull/49568/merge -> FETCH_HEAD
✔ Fetched commits as f5bb2c7d205c..d4675cf5801e
--------------------------------------------------------------------------------
[main b0432b4569] net: improve performance of isIPv4 and isIPv6
Author: uzlopak Date: Sat Sep 9 01:42:49 2023 +0200
1 file changed, 10 insertions(+), 10 deletions(-)
[main 6cd44bb65e] Update lib/internal/net.js
Author: Uzlopak Date: Sat Sep 9 02:05:58 2023 +0200
1 file changed, 1 insertion(+), 1 deletion(-)
[main 6dfa053218] add benchmarks
Author: uzlopak Date: Sat Sep 9 15:30:49 2023 +0200
2 files changed, 44 insertions(+)
create mode 100644 benchmark/net/net-is-ip-v4.js
create mode 100644 benchmark/net/net-is-ip-v6.js
[main 9b7d63212d] improve benchmarks as requested
Author: uzlopak Date: Sun Sep 10 21:00:18 2023 +0200
2 files changed, 16 insertions(+), 12 deletions(-)
[main c118049402] add trailing comma
Author: uzlopak Date: Mon Sep 11 01:41:36 2023 +0200
1 file changed, 1 insertion(+), 1 deletion(-)
✔ Patches applied
There are 5 commits in the PR. Attempting autorebase.
Rebasing (2/10)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
net: improve performance of isIPv4 and isIPv6

PR-URL: #49568
Reviewed-By: Matteo Collina matteo.collina@gmail.com
Reviewed-By: Yagiz Nizipli yagiz@nizipli.com
Reviewed-By: Paolo Insogna paolo@cowtech.it
Reviewed-By: Luigi Pinca luigipinca@gmail.com

[detached HEAD 6e375ecf08] net: improve performance of isIPv4 and isIPv6
Author: uzlopak aras.abbasi@googlemail.com
Date: Sat Sep 9 01:42:49 2023 +0200
1 file changed, 10 insertions(+), 10 deletions(-)
Rebasing (3/10)
Rebasing (4/10)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
Update lib/internal/net.js

PR-URL: #49568
Reviewed-By: Matteo Collina matteo.collina@gmail.com
Reviewed-By: Yagiz Nizipli yagiz@nizipli.com
Reviewed-By: Paolo Insogna paolo@cowtech.it
Reviewed-By: Luigi Pinca luigipinca@gmail.com

[detached HEAD 9ca9a4a386] Update lib/internal/net.js
Author: Uzlopak aras.abbasi@googlemail.com
Date: Sat Sep 9 02:05:58 2023 +0200
1 file changed, 1 insertion(+), 1 deletion(-)
Rebasing (5/10)
Rebasing (6/10)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
add benchmarks

PR-URL: #49568
Reviewed-By: Matteo Collina matteo.collina@gmail.com
Reviewed-By: Yagiz Nizipli yagiz@nizipli.com
Reviewed-By: Paolo Insogna paolo@cowtech.it
Reviewed-By: Luigi Pinca luigipinca@gmail.com

[detached HEAD a36d52abdf] add benchmarks
Author: uzlopak aras.abbasi@googlemail.com
Date: Sat Sep 9 15:30:49 2023 +0200
2 files changed, 44 insertions(+)
create mode 100644 benchmark/net/net-is-ip-v4.js
create mode 100644 benchmark/net/net-is-ip-v6.js
Rebasing (7/10)
Rebasing (8/10)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
improve benchmarks as requested

PR-URL: #49568
Reviewed-By: Matteo Collina matteo.collina@gmail.com
Reviewed-By: Yagiz Nizipli yagiz@nizipli.com
Reviewed-By: Paolo Insogna paolo@cowtech.it
Reviewed-By: Luigi Pinca luigipinca@gmail.com

[detached HEAD fb927bbc6a] improve benchmarks as requested
Author: uzlopak aras.abbasi@googlemail.com
Date: Sun Sep 10 21:00:18 2023 +0200
2 files changed, 16 insertions(+), 12 deletions(-)
Rebasing (9/10)
Rebasing (10/10)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
add trailing comma

PR-URL: #49568
Reviewed-By: Matteo Collina matteo.collina@gmail.com
Reviewed-By: Yagiz Nizipli yagiz@nizipli.com
Reviewed-By: Paolo Insogna paolo@cowtech.it
Reviewed-By: Luigi Pinca luigipinca@gmail.com

[detached HEAD 6869ed06d7] add trailing comma
Author: uzlopak aras.abbasi@googlemail.com
Date: Mon Sep 11 01:41:36 2023 +0200
1 file changed, 1 insertion(+), 1 deletion(-)

Successfully rebased and updated refs/heads/main.

ℹ Add commit-queue-squash label to land the PR as one commit, or commit-queue-rebase to land as separate commits.

https://github.com/nodejs/node/actions/runs/6177090293

@lpincalpinca added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Sep 13, 2023
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 13, 2023
@nodejs-github-bot
nodejs-github-bot merged commit 4e01842 into nodejs:mainSep 13, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 4e01842

@Uzlopak
Uzlopak deleted the patch-1 branch September 13, 2023 20:02
@bricssbricss mentioned this pull request Sep 13, 2023
UlisesGascon pushed a commit that referenced this pull request Sep 13, 2023
PR-URL: #49568
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
maxmilton added a commit to maxmilton/bun that referenced this pull request Jan 19, 2024
Copy over the `isIPv4`/`isIPv6` performance improvements from
<nodejs/node#49568>.
Jarred-Sumner pushed a commit to oven-sh/bun that referenced this pull request Jan 20, 2024
Copy over the `isIPv4`/`isIPv6` performance improvements from
<nodejs/node#49568>.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.commit-queue-squashAdd this label to instruct the Commit Queue to squash all the PR commits into the first one.needs-ciPRs that need a full CI run.netIssues and PRs related to the net subsystem.performanceIssues and PRs related to the performance of Node.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@Uzlopak@nodejs-github-bot@anonrig@mcollina@mscdex@ShogunPanda@lpinca@UlisesGascon