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 pre-reading files #5973

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

OneSadCookie
Copy link

Per #5947, files are being read unnecessarily when they have been previously linted and appear in the cache.

To avoid this,

  • remove the test for file emptiness from Linter.init
  • extract the redundant check for this in Rule.lint to a helper, which simplifies Rule.lint and allows it to return non-optional
  • check the helper before calling either Rule.lint or Rule.correct.

@SwiftLintBot
Copy link

SwiftLintBot commented Jan 20, 2025

1 Warning
⚠️ If this is a user-facing change, please include a CHANGELOG entry to credit yourself!
You can find it at CHANGELOG.md.
18 Messages
📖 Building this branch resulted in a binary size of 29488.51 KiB vs 29488.71 KiB when built on main (-1% smaller).
📖 Linting Aerial with this PR took 1.77 s vs 0.95 s on main (86% slower).
📖 Linting Alamofire with this PR took 1.29 s vs 1.29 s on main (0% slower).
📖 Linting Brave with this PR took 8.68 s vs 8.72 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 5.61 s vs 5.66 s on main (0% faster).
📖 Linting Firefox with this PR took 11.67 s vs 11.77 s on main (0% faster).
📖 Linting Kickstarter with this PR took 10.49 s vs 10.63 s on main (1% faster).
📖 Linting Moya with this PR took 0.54 s vs 0.54 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 2.94 s vs 2.97 s on main (1% faster).
📖 Linting Nimble with this PR took 0.8 s vs 0.8 s on main (0% slower).
📖 Linting PocketCasts with this PR took 8.77 s vs 8.86 s on main (1% faster).
📖 Linting Quick with this PR took 0.45 s vs 0.46 s on main (2% faster).
📖 Linting Realm with this PR took 4.67 s vs 4.56 s on main (2% slower).
📖 Linting Sourcery with this PR took 2.4 s vs 2.37 s on main (1% slower).
📖 Linting Swift with this PR took 4.65 s vs 4.67 s on main (0% faster).
📖 Linting VLC with this PR took 1.25 s vs 1.27 s on main (1% faster).
📖 Linting Wire with this PR took 18.56 s vs 18.89 s on main (1% faster).
📖 Linting WordPress with this PR took 11.57 s vs 11.64 s on main (0% faster).

Here's an example of your CHANGELOG entry:

* avoid pre-reading files.  
  [OneSadCookie](https://github.com/OneSadCookie)
  [#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)

note: There are two invisible spaces after the entry's text.

Generated by 🚫 Danger

Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

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

Basically looks good to me!

Would be great to have a test for linting and correcting empty files with (mock) rules that do/don't support this.

@@ -378,7 +381,13 @@ public struct CollectedLinter {
}

var corrections = [Correction]()
for rule in rules.compactMap({ $0 as? any CorrectableRule }) {
for rule in rules.compactMap({ (rule: any Rule) -> (any CorrectableRule)? in
Copy link
Collaborator

Choose a reason for hiding this comment

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

Stylistically, I would prefer a separate if statement in the for block to check for shouldRun. Or you can make use of a where clause.

We could even check for CorrectableRule in the for block to avoid creating a new list and iterating twice.

@OneSadCookie OneSadCookie force-pushed the osc/dont-preread-files branch from 86e8f3b to bde693d Compare January 26, 2025 07:41
@OneSadCookie OneSadCookie force-pushed the osc/dont-preread-files branch from bde693d to 54bd5bc Compare January 26, 2025 07:47
@OneSadCookie
Copy link
Author

I've tried to write those tests, but pretty low confidence that that's "the right way"; pointers appreciated.

I tried to see if there was a way to write an integration test that the files' contents aren't read when the cache is populated, but short of modifying SourceKitten to report contents reads, or magical unix shenanigans (I think you can somehow get a pipe at a path so it gets opened like a regular file, maybe you could tell if data was consumed on the pipe?), I don't see how. Definitely open to ideas here, as regressions will be easy.

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