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

[6.0] Use an AtomicInt32 to count pendingUnitCount instead of using AsyncQueue #1744

Merged
merged 1 commit into from
Oct 9, 2024

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 9, 2024

  • Explanation: Adding an item to AsyncQueue<Serial> is linear in the number of pending queue items, thus adding n items to an AsyncQueue before any can execute is in O(n^2). This decision was made intentionally because the primary use case for AsyncQueue was to track pending LSP requests, of which we don’t expect to have too many pending requests at any given time.
    SourceKitIndexDelegate was also using AsyncQueue to track the number of pending units to be processed and eg. after indexing SourceKit-LSP, I have seen this grow up to ~20,000. With the quadratic behavior, this explodes time-wise and also uses a bunch of memory for those tasks.
    Turns out that we don’t actually need to use a queue here at all, an atomic is sufficient and much faster.
  • Scope: Background functionality for SourceKit-LSP
  • Risk: Low, no functional change, just changing a queue to an atomic
  • Testing: Verified that the atomic implementation does not expose the quadratic behavior
  • Issue: High memory usage #1541 / rdar://130844901
  • Reviewer: @hamishknight on Use an AtomicInt32 to count pendingUnitCount instead of using AsyncQueue #1720

…yncQueue`

Adding an item to `AsyncQueue<Serial>` is linear in the number of pending queue items, thus adding n items to an `AsyncQueue` before any can execute is in O(n^2). This decision was made intentionally because the primary use case for `AsyncQueue` was to track pending LSP requests, of which we don’t expect to have too many pending requests at any given time.

`SourceKitIndexDelegate` was also using `AsyncQueue` to track the number of pending units to be processed and eg. after indexing SourceKit-LSP, I have seen this grow up to ~20,000. With the quadratic behavior, this explodes time-wise.

Turns out that we don’t actually need to use a queue here at all, an atomic is sufficient and much faster.

Independently, we should consider mitigating the quadratic behavior of `AsyncQueue<Serial>` or `AsyncQueue` in general.

Fixes swiftlang#1541
rdar://130844901
@ahoppen ahoppen requested a review from bnbarham October 9, 2024 00:39
@ahoppen
Copy link
Member Author

ahoppen commented Oct 9, 2024

@swift-ci Please test

@ahoppen ahoppen merged commit 8f9aaed into swiftlang:release/6.0 Oct 9, 2024
3 checks passed
@ahoppen ahoppen deleted the 6.0/atomic-instead-of-queue branch October 9, 2024 15:58
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.

2 participants