-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Improve subscription performance by 10-20 times #9817
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
Closed
MrFlashAccount
wants to merge
17
commits into
TanStack:main
from
MrFlashAccount:improve-gc-performance
Closed
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4a9f064
feat(core): introduce GCManager for efficient garbage collection acroβ¦
MrFlashAccount 149e00e
feat(core): enhance garbage collection with immediate scan scheduling
MrFlashAccount 8d59d30
address issues
MrFlashAccount 1b47ce9
fix mutations behavior
MrFlashAccount f65ccf3
Fix some tests
MrFlashAccount 3605a95
Fix suspense query tests
MrFlashAccount 0e241d7
fix solid query test
MrFlashAccount 8b97639
feature(core): start GC only when we have items to collect
MrFlashAccount b0fd174
rewrite gc manager to timeout api
MrFlashAccount c514ec8
Reimlement gcManager
MrFlashAccount d74d9de
clean things up
MrFlashAccount c087439
Add tests for gcManager
MrFlashAccount cc8e8d6
clear one change
MrFlashAccount ce1c896
add extra test
MrFlashAccount b030a69
fix tests
MrFlashAccount 648cb68
Changeset
MrFlashAccount 7dd1e63
fix test
MrFlashAccount File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
1,341 changes: 1,341 additions & 0 deletions
1,341
packages/query-core/src/__tests__/gcManager.test.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { timeoutManager } from './timeoutManager' | ||
| import type { Removable } from './removable' | ||
| import type { ManagedTimerId } from './timeoutManager' | ||
|
|
||
| /** | ||
| * Configuration for the GC manager | ||
| */ | ||
| export interface GCManagerConfig { | ||
| /** | ||
| * Force disable garbage collection. | ||
| * @default false | ||
| */ | ||
| forceDisable?: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Manages periodic garbage collection across all caches. | ||
| * | ||
| * Instead of each query/mutation having its own timeout, | ||
| * the GCManager runs a single interval that scans all | ||
| * registered caches for items eligible for removal. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // Register a cache for GC | ||
| * gcManager.registerCache(queryCache) | ||
| * | ||
| * // Start scanning | ||
| * gcManager.startScanning() | ||
| * | ||
| * // Change scan interval | ||
| * gcManager.setScanInterval(60000) // 1 minute | ||
| * | ||
| * // Stop scanning | ||
| * gcManager.stopScanning() | ||
| * ``` | ||
| */ | ||
| export class GCManager { | ||
| #isScanning = false | ||
| #forceDisable = false | ||
| #eligibleItems = new Set<Removable>() | ||
| #scheduledScanTimeoutId: ManagedTimerId | null = null | ||
| #isScheduledScan = false | ||
|
|
||
| constructor(config: GCManagerConfig = {}) { | ||
| this.#forceDisable = config.forceDisable ?? false | ||
| } | ||
|
|
||
| #scheduleScan(): void { | ||
| if (this.#forceDisable || this.#isScheduledScan) { | ||
| return | ||
| } | ||
|
|
||
| this.#isScheduledScan = true | ||
|
|
||
| queueMicrotask(() => { | ||
| if (!this.#isScheduledScan) { | ||
| return | ||
| } | ||
|
|
||
| this.#isScheduledScan = false | ||
|
|
||
| let minTimeUntilGc = Infinity | ||
|
|
||
| for (const item of this.#eligibleItems) { | ||
| const timeUntilGc = getTimeUntilGc(item) | ||
|
|
||
| if (timeUntilGc < minTimeUntilGc) { | ||
| minTimeUntilGc = timeUntilGc | ||
| } | ||
| } | ||
|
|
||
| if (minTimeUntilGc === Infinity) { | ||
| return | ||
| } | ||
|
|
||
| if (this.#scheduledScanTimeoutId !== null) { | ||
| timeoutManager.clearTimeout(this.#scheduledScanTimeoutId) | ||
| } | ||
|
|
||
| this.#isScanning = true | ||
| this.#scheduledScanTimeoutId = timeoutManager.setTimeout(() => { | ||
| this.#isScanning = false | ||
| this.#scheduledScanTimeoutId = null | ||
|
|
||
| this.#performScan() | ||
|
|
||
| // If there are still eligible items, schedule the next scan | ||
| if (this.#eligibleItems.size > 0) { | ||
| this.#scheduleScan() | ||
| } | ||
| }, minTimeUntilGc) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Stop periodic scanning. Safe to call multiple times. | ||
| */ | ||
| stopScanning(): void { | ||
| this.#isScanning = false | ||
| this.#isScheduledScan = false | ||
|
|
||
| if (this.#scheduledScanTimeoutId === null) { | ||
| return | ||
| } | ||
|
|
||
| timeoutManager.clearTimeout(this.#scheduledScanTimeoutId) | ||
|
|
||
| this.#scheduledScanTimeoutId = null | ||
| } | ||
|
|
||
| /** | ||
| * Check if scanning is active | ||
| */ | ||
| isScanning(): boolean { | ||
| return this.#isScanning | ||
| } | ||
|
|
||
| /** | ||
| * Track an item that has been marked for garbage collection. | ||
| * Automatically starts scanning if not already running. | ||
| * | ||
| * @param item - The query or mutation marked for GC | ||
| */ | ||
| trackEligibleItem(item: Removable): void { | ||
| if (this.#forceDisable) { | ||
| return | ||
| } | ||
|
|
||
| if (this.#eligibleItems.has(item)) { | ||
| return | ||
| } | ||
|
|
||
| this.#eligibleItems.add(item) | ||
|
|
||
| this.#scheduleScan() | ||
| } | ||
|
|
||
| /** | ||
| * Untrack an item that is no longer eligible for garbage collection. | ||
| * Automatically stops scanning if no items remain eligible. | ||
| * | ||
| * @param item - The query or mutation no longer eligible for GC | ||
| */ | ||
| untrackEligibleItem(item: Removable): void { | ||
| if (this.#forceDisable) { | ||
| return | ||
| } | ||
|
|
||
| if (!this.#eligibleItems.has(item)) { | ||
| return | ||
| } | ||
|
|
||
| this.#eligibleItems.delete(item) | ||
|
|
||
| if (this.isScanning()) { | ||
| if (this.getEligibleItemCount() === 0) { | ||
| this.stopScanning() | ||
| } else { | ||
| this.#scheduleScan() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the number of items currently eligible for garbage collection. | ||
| */ | ||
| getEligibleItemCount(): number { | ||
| return this.#eligibleItems.size | ||
| } | ||
|
|
||
| #performScan(): void { | ||
| // Iterate through all eligible items and attempt to collect them | ||
| for (const item of this.#eligibleItems) { | ||
| try { | ||
| if (item.isEligibleForGc()) { | ||
| const wasCollected = item.optionalRemove() | ||
|
|
||
| if (wasCollected) { | ||
| this.#eligibleItems.delete(item) | ||
| } | ||
| } | ||
| } catch (error) { | ||
| // Log but don't throw - one cache error shouldn't stop others | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| console.error('[GCManager] Error during garbage collection:', error) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| clear(): void { | ||
| this.#eligibleItems.clear() | ||
| this.stopScanning() | ||
| } | ||
| } | ||
|
|
||
| function getTimeUntilGc(item: Removable): number { | ||
| const gcAt = item.getGcAtTimestamp() | ||
| if (gcAt === null) { | ||
| return Infinity | ||
| } | ||
| return Math.max(0, gcAt - Date.now()) | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.