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

Improve performance of isPathIgnored() utility #6728

Merged
merged 3 commits into from Mar 29, 2023

Conversation

ybiquitous
Copy link
Member

@ybiquitous ybiquitous commented Mar 22, 2023

Which issue, if any, is this issue related to?

None.

Is there anything in the PR that needs further explanation?

This change delays the getFileIgnorer() call until needed. getFileIgnorer() internally uses fs.readFileSync() which is a blocking I/O and possibly slow.

const ignoreText = fs.readFileSync(absoluteIgnoreFilePath, 'utf8');

Benchmark

script

// bench.js
const Benchmark = require('benchmark');
const createStylelint = require('./lib/createStylelint');
const isPathIgnored = require('./lib/isPathIgnored');

const config = {
	ignoreFiles: ['**/*.css', '!**/invalid-hex.css'],
	rules: { 'block-no-empty': true },
};
const stylelint = createStylelint({ config });

const suite = new Benchmark.Suite();

suite
	.add('isPathIgnored', function () {
		isPathIgnored(stylelint, 'a.css');
	})
	.on('complete', function (event) {
		console.log(String(event.target));
	})
	.run({ async: true });

Commit Result
0bc53af (main branch) isPathIgnored x 42,235 ops/sec ±2.42% (70 runs sampled)
b6cea75 (this branch) isPathIgnored x 2,966,362 ops/sec ±1.39% (77 runs sampled)

Environment:

  • OS: macOS 13.2.1 (Apple M1 Pro)
  • Node.js: 19.8.1

This change delays the `getFileIgnorer()` call until needed.
`getFileIgnorer()` internally uses `fs.readFileSync()` which is a blocking I/O and possible slow.
@changeset-bot
Copy link

changeset-bot bot commented Mar 22, 2023

🦋 Changeset detected

Latest commit: c055ba8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
stylelint Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ybiquitous ybiquitous marked this pull request as ready for review March 22, 2023 14:15
* @param {{ cwd: string, ignorePath?: string | string[], ignorePattern?: string[] }} options
* @typedef {import('stylelint').LinterOptions} LinterOptions
*
* @param {Pick<LinterOptions, 'ignorePath' | 'ignorePattern'> & { cwd: string }} options
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] Clarify using LinterOptions.

@@ -37,7 +39,7 @@ module.exports = function getFileIgnorer(options) {
}
}

ignorer.add(options.ignorePattern || []);
if (ignorePattern) ignorer.add(ignorePattern);
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] Reduce the ignorer.add() call.

Copy link
Member

@mattxwang mattxwang left a comment

Choose a reason for hiding this comment

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

LGTM, and thank you for the incremental improvements elsewhere in getFileIgnorer.

I'm surprised that this is in the ballpark of a 75x speedup!

@mattxwang mattxwang mentioned this pull request Mar 29, 2023
@ybiquitous
Copy link
Member Author

Thanks for the review!

@ybiquitous ybiquitous merged commit fb69e01 into main Mar 29, 2023
14 checks passed
@ybiquitous ybiquitous deleted the improve-performance-of-isPathIgnored branch March 29, 2023 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants