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

perf(query-core): Improve mutationCache implementation performance #8451

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 56 additions & 31 deletions packages/query-core/src/mutationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
// CLASS

export class MutationCache extends Subscribable<MutationCacheListener> {
#mutations: Map<string, Array<Mutation<any, any, any, any>>>
#mutations: Set<Mutation<any, any, any, any>>
#scopes: Map<string, Array<Mutation<any, any, any, any>>>
#mutationId: number

constructor(public config: MutationCacheConfig = {}) {
super()
this.#mutations = new Map()
this.#mutationId = Date.now()
this.#mutations = new Set()
this.#scopes = new Map()
this.#mutationId = 0
}

build<TData, TError, TVariables, TContext>(
Expand All @@ -109,59 +111,82 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
}

add(mutation: Mutation<any, any, any, any>): void {
const scope = scopeFor(mutation)
const mutations = this.#mutations.get(scope) ?? []
mutations.push(mutation)
this.#mutations.set(scope, mutations)
this.#mutations.add(mutation);
const scope = scopeFor(mutation);
if (typeof scope === 'string') {
const scopedMutations = this.#scopes.get(scope);
if (scopedMutations) {
scopedMutations.push(mutation);;
Copy link

Choose a reason for hiding this comment

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

Suggested change
scopedMutations.push(mutation);;
scopedMutations.push(mutation);

} else {
this.#scopes.set(scope, [mutation]);
}
}
this.notify({ type: 'added', mutation })
}

remove(mutation: Mutation<any, any, any, any>): void {
const scope = scopeFor(mutation)
if (this.#mutations.has(scope)) {
const mutations = this.#mutations
.get(scope)
?.filter((x) => x !== mutation)
if (mutations) {
if (mutations.length === 0) {
this.#mutations.delete(scope)
} else {
this.#mutations.set(scope, mutations)
if (this.#mutations.delete(mutation)) {
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const scopedMutations = this.#scopes.get(scope);
if (scopedMutations) {
if (scopedMutations.length > 1) {
const index = scopedMutations.indexOf(mutation);
if (index !== -1) {
scopedMutations.splice(index, 1);
}
} else if (scopedMutations[0] === mutation) {
this.#scopes.delete(scope);
}
}
}
}

// Currently we notify the removal even if the mutation was already removed.
// Consider making this an error or not notifying of the removal depending on the desired semantics.
this.notify({ type: 'removed', mutation })
}

canRun(mutation: Mutation<any, any, any, any>): boolean {
const firstPendingMutation = this.#mutations
.get(scopeFor(mutation))
?.find((m) => m.state.status === 'pending')

// we can run if there is no current pending mutation (start use-case)
// or if WE are the first pending mutation (continue use-case)
return !firstPendingMutation || firstPendingMutation === mutation
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const mutationsWithSameScope = this.#scopes.get(scope)
const firstPendingMutation = mutationsWithSameScope?.find(m => m.state.status === 'pending')
// we can run if there is no current pending mutation (start use-case)
// or if WE are the first pending mutation (continue use-case)
return !firstPendingMutation || firstPendingMutation === mutation
} else {
// For unscoped mutations there are never any pending mutations in front of the
// current mutation
return true
}
}

runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {
const foundMutation = this.#mutations
.get(scopeFor(mutation))
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const foundMutation = this.#scopes
.get(scope)
?.find((m) => m !== mutation && m.state.isPaused)

return foundMutation?.continue() ?? Promise.resolve()
return foundMutation?.continue() ?? Promise.resolve()
} else {
return Promise.resolve()
}
}

clear(): void {
notifyManager.batch(() => {
this.getAll().forEach((mutation) => {
this.remove(mutation)
this.#mutations.forEach((mutation) => {
this.notify({ type: 'removed', mutation })
})
this.#mutations.clear()
this.#scopes.clear()
})
}

getAll(): Array<Mutation> {
return [...this.#mutations.values()].flat()
return Array.from(this.#mutations)
}

find<
Expand Down Expand Up @@ -203,5 +228,5 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
}

function scopeFor(mutation: Mutation<any, any, any, any>) {
return mutation.options.scope?.id ?? String(mutation.mutationId)
return mutation.options.scope?.id
}
Loading