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

Avoid creating multiple Minimatch instances for the same negative glob #33

Merged
merged 1 commit into from
Jan 2, 2015

Conversation

UltCombo
Copy link
Contributor

@UltCombo UltCombo commented Jan 1, 2015

This optimizes use cases such as ['a', 'b', '!b'], where a new Minimatch instance for '!b' would be created for each previous positive glob.

Now the Minimatch instances for negative globs are created beforehand, thus positive globs can use the same negative matcher instances. That is, for ['a', 'b', '!b'] both 'a' and 'b' globs will use the same Minimatch instance for '!b' instead of instantiating their own.

Also, as far as I can see, gs.createStream is internal (no docs or tests), so I took the liberty of adapting it a bit as I needed the complete opt object to pass to the Minimatch constructor in gs.create.

Should also note that this change, technically, would instantiate Minimatch instances that would never be used when considering leading negative globs (e.g. ['!b', 'b']), but that is a complete misuse and IIRC Minimatch produces their underlying regex lazily, so this shouldn't be a problem.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) when pulling 5571d41 on UltCombo:mm-optimize into 1f26389 on wearefractal:master.

@UltCombo
Copy link
Contributor Author

UltCombo commented Jan 1, 2015

(improved a comment & squashed)

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) when pulling e7f66fe on UltCombo:mm-optimize into 1f26389 on wearefractal:master.

@UltCombo UltCombo changed the title Create Minimatch instances for each negative glob only once Avoid creating multiple Minimatch instances for the same negative glob Jan 1, 2015
@UltCombo
Copy link
Contributor Author

UltCombo commented Jan 1, 2015

Changed commit message.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) when pulling 0f6ba0a on UltCombo:mm-optimize into 1f26389 on wearefractal:master.

UltCombo added a commit to sindresorhus/globby that referenced this pull request Jan 1, 2015
This optimizes use cases such as `['a', 'b', '!b']`, where a new Minimatch instance for `'!b'` would be created for each previous positive glob.

Now the Minimatch instances for negative globs are created beforehand, thus positive globs can use the same negative matcher instances. That is, for `['a', 'b', '!b']` both `'a'` and `'b'` globs will use the same Minimatch instance for `'!b'` instead of instantiating their own.

Should also note that this change, technically, would instantiate Minimatch instances that would never be used when considering leading negative globs (e.g. `['!b', 'b']`), but that is a complete misuse and IIRC Minimatch produces their underlying regex lazily, so this shouldn't be a problem.

(imported from gulpjs/glob-stream#33)
yocontra pushed a commit that referenced this pull request Jan 2, 2015
Avoid creating multiple Minimatch instances for the same negative glob
@yocontra yocontra merged commit 2768041 into gulpjs:master Jan 2, 2015
@yocontra
Copy link
Member

yocontra commented Jan 2, 2015

Nice catch - thanks!

@UltCombo
Copy link
Contributor Author

UltCombo commented Jan 2, 2015

No problem, though, I believe it lost a bit of readability, seeing as negatives' glob property now holds either a RegExp or Minimatch instance, which is a little counterintuitive. Perhaps it would be cleaner if this patch was more similar to the one I submitted to globby (sindresorhus/globby@e402d9f), that is, iterating over the negatives and creating a matcher property.

@UltCombo UltCombo deleted the mm-optimize branch January 2, 2015 08:58
UltCombo added a commit to UltCombo/glob-stream that referenced this pull request Jan 2, 2015
- Better naming for negative matchers ( gulpjs#33 (comment) )
- Throw on invalid glob argument ( gulpjs#34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Applied glob type validation in the `isNegative` function, as all globs go through it before stream creation and that function is already testing their types.
UltCombo added a commit to UltCombo/glob-stream that referenced this pull request Jan 2, 2015
- Better naming for negative matchers ( gulpjs#33 (comment) )
- Throw on invalid glob argument ( gulpjs#34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
UltCombo added a commit to UltCombo/glob-stream that referenced this pull request Jan 2, 2015
- Better naming for negative matchers ( gulpjs#33 (comment) )
- Throw on invalid glob argument ( gulpjs#34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
UltCombo added a commit to UltCombo/glob-stream that referenced this pull request Jan 2, 2015
- Better naming for negative matchers ( gulpjs#33 (comment) )
- Throw on invalid glob argument ( gulpjs#34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 16, 2017
Avoid creating multiple Minimatch instances for the same negative glob
phated pushed a commit that referenced this pull request Feb 16, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 16, 2017
Avoid creating multiple Minimatch instances for the same negative glob
phated pushed a commit that referenced this pull request Feb 16, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 16, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 16, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 20, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 21, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 21, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
phated pushed a commit that referenced this pull request Feb 21, 2017
- Better naming for negative matchers ( #33 (comment) )
- Throw on invalid glob argument ( #34 (comment) )
- Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
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.

3 participants