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

libs: fix some RegExp nits #13536

Closed
wants to merge 3 commits into from
Closed

libs: fix some RegExp nits #13536

wants to merge 3 commits into from

Conversation

vsemozhetbyt
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt commented Jun 7, 2017

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

cluster, dns, repl, tls, util

  1. Take RegExp creation out of cycles.

    I am not sure if v8 fully optimizes this now (= makes some RegExps an identical one in cycles), so I've addressed this to be on the safe side. As long as the RegExp does not use g flag and match indices, we are safe here.

  2. Use test(), not match() in boolean context.

    match() returns a complicated object, unneeded in a boolean context.

  3. Remove redundant RegExp parts.

    If I get this correctly, a RegExp part with a quantifier '0 or more' is redundant if:

    • it is used at the very beginning or the very end of the RegExp;
    • it is used in a boolean context (test());
    • or it is not captured in match()/exec() results (full or partial) or does not affect match indices.

    However, these parts may improve readability and comprehensibility. If this is the case for the changed fragments, feel free to reject the change.

These are rather trivial changes to cause any regressions, and I've tried not to cause any accidental deopts (being consistent with var / const environment style in creating variables). If anybody believes some benchmarks are needed, please, tell me which of them I should run.

It is easier to review this PR commit by commit, as the changes are interwoven and this may be confusing.

@nodejs-github-bot nodejs-github-bot added dns Issues and PRs related to the dns subsystem. repl Issues and PRs related to the REPL subsystem. tls Issues and PRs related to the tls subsystem. util Issues and PRs related to the built-in util module. labels Jun 7, 2017
@vsemozhetbyt vsemozhetbyt added the cluster Issues and PRs related to the cluster subsystem. label Jun 7, 2017

servers.forEach((serv) => {
var ipVersion = isIP(serv);
if (ipVersion !== 0)
return newSet.push([ipVersion, serv]);

const match = serv.match(/\[(.*)\](?::\d+)?/);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this was meant to be anchored, i.e. ^…$, otherwise the addition doesn’t make much sense? Anyway, your change seems correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this was for some more clarity, to show the wider pattern. But this may impact performance a bit.

@@ -772,7 +773,7 @@ function complete(line, callback) {
name = files[f];
ext = path.extname(name);
base = name.slice(0, -ext.length);
if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') {
if (versionedFileNamesRe.test(base) || name === '.npm') {
Copy link
Member

Choose a reason for hiding this comment

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

same here, but I really don’t know what this code is supposed to do. :/

Copy link
Contributor Author

@vsemozhetbyt vsemozhetbyt Jun 7, 2017

Choose a reason for hiding this comment

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

Ditto about clarity/performance )

Copy link
Member

Choose a reason for hiding this comment

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

I looked into this a bit further and this all about the npm cache itself. It is meant to prevent listing all the version folders found in the cache folder (e.g. 1.0.0 in root/.npm/foo/1.0.0/package). As far as I see it this does not check the cache folder anymore though and the windows cache folder (npm-cache instead of .npm) is not excluded either. So I think this part could be safely removed and it's a legacy remnant. I did not try it out though.

vsemozhetbyt added a commit that referenced this pull request Jun 10, 2017
* Take RegExp creation out of cycles.
* Use test(), not match() in boolean context.
* Remove redundant RegExp parts.

PR-URL: #13536
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@vsemozhetbyt
Copy link
Contributor Author

Landed in 390fa03

@vsemozhetbyt vsemozhetbyt deleted the regexp-nits branch June 10, 2017 10:40
addaleax pushed a commit that referenced this pull request Jun 10, 2017
* Take RegExp creation out of cycles.
* Use test(), not match() in boolean context.
* Remove redundant RegExp parts.

PR-URL: #13536
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@addaleax addaleax mentioned this pull request Jun 10, 2017
@gibfahn gibfahn mentioned this pull request Jun 15, 2017
3 tasks
@MylesBorins
Copy link
Contributor

This does not land cleanly in LTS. Please feel free to manually backport. Please also feel free to replace the backport request label with do-not-land if it shouldn't land

@vsemozhetbyt
Copy link
Contributor Author

@MylesBorins Backport: #14348

MylesBorins pushed a commit that referenced this pull request Jul 18, 2017
* Take RegExp creation out of cycles.
* Use test(), not match() in boolean context.
* Remove redundant RegExp parts.

Backport-PR-URL: #14348
PR-URL: #13536
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@MylesBorins MylesBorins mentioned this pull request Jul 18, 2017
MylesBorins pushed a commit that referenced this pull request Jul 21, 2017
* Take RegExp creation out of cycles.
* Use test(), not match() in boolean context.
* Remove redundant RegExp parts.

Backport-PR-URL: #14348
PR-URL: #13536
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cluster Issues and PRs related to the cluster subsystem. dns Issues and PRs related to the dns subsystem. repl Issues and PRs related to the REPL subsystem. tls Issues and PRs related to the tls subsystem. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants