Repository navigation
Fix slow cache lookup due to unnecessary copying - #6969
Open
ephraimkunz wants to merge 4 commits into
Open
ephraimkunz wants to merge 4 commits into
ephraimkunz wants to merge 4 commits into
Conversation
Generated by 🚫 Danger |
Author
|
@SimplyDanny are you the right person to take a look at this? (I saw you landed some PRs recently). This fix is safe and drastically improves performance when linting many files. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I noticed that running SwiftLint on my company's large codebase wasn't saturating all the cores as I expected. Instead, many threads spent a long time waiting for
writeCacheLockin LinterCache. The critical section that that lock protects is taking a surprisingly long time to execute.Here's one thread spending 51% of it's running time (2.79 seconds out of 5.47 seconds) inside LinterCache.cache. Most of that time is spent in dictionary allocation and deallocation:
The code in question is
This is a hidden performance pitfall. The 2nd line copies the files cache struct stored in the writeCache into filesCache, but that's cheap because Swift struct copies are lazy.
Line 3 is the real problem. Since we are now modifying that copied struct, Swift must first actually do a deep copy (copy-on-write) since there are 2 references to the underlying struct and value semantics must be maintained. This is an expensive O(n) operation.
On line 4, Swift then deallocates the original struct and replaces it with the one we just copied or modified.
A sufficiently smart compiler would elide the copy and deallocation since it would recognize that we are really just mutating the original fileCache, but Swift's compiler does not.
The result is an O(n^2) algorithm. LinterCache.cache() is called for every file. Each time it is called, it copies all the previous file caches entries, one for each file its previously seen.
The fix is simple. On line 2, instead of copying the stored struct, just remove it and modify the removed version. Line 3 now no longer triggers COW since that's the only reference to this struct so modification can happen in-place. The function is now O(1), meaning we now have an O(n) algorithm since we call it for each file.
After making this change, we spend just 4% of the thread's time (110 ms out of 2.73 seconds) inside this method:

After making this change, time to scan 46,590 swift files in our codebase drops from 43 seconds to 11 seconds, a speedup of 3.9x.