-
Notifications
You must be signed in to change notification settings - Fork 249
perf(db): skip unchanged index updates #1691
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
Merged
KyleAMathews
merged 6 commits into
TanStack:main
from
K-Mistele:perf/skip-unchanged-index-updates
Jul 24, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2cc26fd
perf(db): skip unchanged index updates
K-Mistele b693fbb
test(db): cover nullish index transitions
K-Mistele 791c477
fix(db): avoid self-comparison in index equality
K-Mistele 0b91d45
perf(db): harden unchanged index updates
KyleAMathews fdf567a
Merge pull request #1 from TanStack/codex/pr-1691-improvements
K-Mistele 8a9c859
docs(db): combine index update changeset
KyleAMathews 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
There are no files selected for viewing
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,7 @@ | ||
| --- | ||
| '@tanstack/db': patch | ||
| --- | ||
|
|
||
| Skip unchanged index writes while preserving index bookkeeping after failed | ||
| removals. Cache index evaluators and avoid object normalization work for | ||
| primitive values to reduce update overhead. |
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
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
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,133 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { BasicIndex } from '../src/indexes/basic-index.js' | ||
| import { BTreeIndex } from '../src/indexes/btree-index.js' | ||
| import { PropRef } from '../src/query/ir.js' | ||
| import { normalizeValue } from '../src/utils/comparison.js' | ||
| import type { BaseIndex } from '../src/indexes/base-index.js' | ||
|
|
||
| type IndexConstructor = new ( | ||
| id: number, | ||
| expression: PropRef, | ||
| name?: string, | ||
| options?: unknown, | ||
| ) => BaseIndex<string> & { | ||
| valueMapData: Map<unknown, Set<string>> | ||
| } | ||
|
|
||
| const indexTypes: Array<[string, IndexConstructor]> = [ | ||
| [`BasicIndex`, BasicIndex as IndexConstructor], | ||
| [`BTreeIndex`, BTreeIndex as IndexConstructor], | ||
| ] | ||
|
|
||
| describe.each(indexTypes)(`%s update`, (_indexName, IndexType) => { | ||
| function createIndex(options?: unknown) { | ||
| return new IndexType(1, new PropRef([`value`]), `test_index`, options) | ||
| } | ||
|
|
||
| it(`keeps the existing bucket when the indexed value does not change`, () => { | ||
| const index = createIndex() | ||
| index.add(`a`, { value: 1, version: 1 }) | ||
| const bucket = index.valueMapData.get(1) | ||
| const lastUpdated = index.getStats().lastUpdated | ||
|
|
||
| index.update(`a`, { value: 1, version: 1 }, { value: 1, version: 2 }) | ||
|
|
||
| expect(index.valueMapData.get(1)).toBe(bucket) | ||
| expect(index.getStats().lastUpdated).toBe(lastUpdated) | ||
| expect(index.lookup(`eq`, 1)).toEqual(new Set([`a`])) | ||
| }) | ||
|
|
||
| it.each([ | ||
| [`undefined`, undefined, undefined], | ||
| [`NaN`, Number.NaN, Number.NaN], | ||
| [`signed zero`, 0, -0], | ||
| [`equal dates`, new Date(1000), new Date(1000)], | ||
| [`equal byte arrays`, new Uint8Array([1, 2]), new Uint8Array([1, 2])], | ||
| ])(`keeps the existing bucket for %s`, (_caseName, oldValue, newValue) => { | ||
| const index = createIndex() | ||
| index.add(`a`, { value: oldValue }) | ||
| const bucket = index.valueMapData.get(normalizeValue(oldValue)) | ||
| expect(bucket).toBeDefined() | ||
|
|
||
| index.update(`a`, { value: oldValue }, { value: newValue }) | ||
|
|
||
| expect(index.valueMapData.get(normalizeValue(newValue))).toBe(bucket) | ||
| }) | ||
|
|
||
| it(`moves the key when the indexed value changes`, () => { | ||
| const index = createIndex() | ||
| index.add(`a`, { value: 1 }) | ||
|
|
||
| index.update(`a`, { value: 1 }, { value: 2 }) | ||
|
|
||
| expect(index.lookup(`eq`, 1)).toEqual(new Set()) | ||
| expect(index.lookup(`eq`, 2)).toEqual(new Set([`a`])) | ||
| }) | ||
|
|
||
| it(`does not conflate undefined and null`, () => { | ||
| const index = createIndex() | ||
| index.add(`a`, { value: undefined }) | ||
|
|
||
| index.update(`a`, { value: undefined }, { value: null }) | ||
|
|
||
| expect(index.lookup(`eq`, undefined)).toEqual(new Set()) | ||
| expect(index.lookup(`eq`, null)).toEqual(new Set([`a`])) | ||
| }) | ||
|
|
||
| it(`does not use comparator equality to skip an update`, () => { | ||
| const index = createIndex({ | ||
| compareFn: (a: string, b: string) => | ||
| a.toLowerCase().localeCompare(b.toLowerCase()), | ||
| }) | ||
| index.add(`a`, { value: `A` }) | ||
|
|
||
| index.update(`a`, { value: `A` }, { value: `a` }) | ||
|
|
||
| expect(index.lookup(`eq`, `A`)).toEqual(new Set()) | ||
| expect(index.lookup(`eq`, `a`)).toEqual(new Set([`a`])) | ||
| }) | ||
|
|
||
| it(`preserves the previous error behavior when evaluation fails`, () => { | ||
| const index = createIndex() | ||
| index.add(`a`, { value: 1 }) | ||
| const newItem = Object.defineProperty({}, `value`, { | ||
| get() { | ||
| throw new Error(`evaluation failed`) | ||
| }, | ||
| }) | ||
|
|
||
| expect(() => index.update(`a`, { value: 1 }, newItem)).toThrow( | ||
| `evaluation failed`, | ||
| ) | ||
|
|
||
| expect(index.lookup(`eq`, 1)).toEqual(new Set()) | ||
| expect(index.keyCount).toBe(0) | ||
| }) | ||
| }) | ||
|
|
||
| describe(`BasicIndex update bookkeeping`, () => { | ||
| it(`repairs indexed key membership after a failed removal`, () => { | ||
| const index = new BasicIndex<string>(1, new PropRef([`value`])) | ||
| index.add(`a`, { value: 1 }) | ||
| const itemWithThrowingValue = Object.defineProperty({}, `value`, { | ||
| get() { | ||
| throw new Error(`evaluation failed`) | ||
| }, | ||
| }) | ||
| const warn = vi.spyOn(console, `warn`).mockImplementation(() => {}) | ||
|
|
||
| try { | ||
| index.remove(`a`, itemWithThrowingValue) | ||
| } finally { | ||
| warn.mockRestore() | ||
| } | ||
|
|
||
| expect(index.lookup(`eq`, 1)).toEqual(new Set([`a`])) | ||
| expect(index.keyCount).toBe(0) | ||
|
|
||
| index.update(`a`, { value: 1 }, { value: 1 }) | ||
|
|
||
| expect(index.lookup(`eq`, 1)).toEqual(new Set([`a`])) | ||
| expect(index.keyCount).toBe(1) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.