-
Notifications
You must be signed in to change notification settings - Fork 8
Incremental topK with fractional index #72
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
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
041c2d0
WIP incremental topKWithFractionalIndex
kevin-dp c5589a6
Incremental topKWithFractionalIndex
kevin-dp 9fc3a2c
Fix tests to not assume particular fractional indices as those are im…
kevin-dp 4fef948
Introduce a TopK data structure
kevin-dp de145fd
B+ tree variant of topKWithFractionalIndex
kevin-dp 9b33a52
Extend unit tests to test all insertion and deletion cases
kevin-dp 5d41351
Formatting
kevin-dp 71d0dcb
Unit test for duplicate values
kevin-dp c605244
Expose useTree option also on sortBy operator
kevin-dp 98bc39e
Split array and B+ tree variants in separate operators
kevin-dp 1e402df
Add missing imports
kevin-dp d1473b2
Add a orderBy operator that uses topK with B+ tree variant
kevin-dp bffd28b
Formatting
kevin-dp 2640de6
Trigger CI
kevin-dp 69551ea
Remove useTree option
kevin-dp edad4be
Changeset
kevin-dp 4e22638
Dynamically import B+ tree library
kevin-dp 845bbe6
Also run orderByWithFractionalIndex tests for both the array and B+ t…
kevin-dp 37eb83f
Split tree version of orderBy into its own file to enable tree shakin…
kevin-dp 5fc79d9
Improved changeset
kevin-dp 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,5 @@ | ||
| --- | ||
| '@electric-sql/d2mini': patch | ||
| --- | ||
|
|
||
| Introduce topKWithFractionalIndexBTree and orderByWithFractionalIndexBTree operators. These variants use a B+ tree which is more efficient on big collections as its time complexity is logarithmic. |
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,19 @@ | ||
| import { KeyValue } from '../types.js' | ||
| import { OrderByOptions, orderByWithFractionalIndexBase } from './orderBy.js' | ||
| import { topKWithFractionalIndexBTree } from './topKWithFractionalIndexBTree.js' | ||
|
|
||
| export function orderByWithFractionalIndexBTree< | ||
| T extends KeyValue<unknown, unknown>, | ||
| Ve = unknown, | ||
| >( | ||
| valueExtractor: ( | ||
| value: T extends KeyValue<unknown, infer V> ? V : never, | ||
| ) => Ve, | ||
| options?: OrderByOptions<Ve>, | ||
| ) { | ||
| return orderByWithFractionalIndexBase( | ||
| topKWithFractionalIndexBTree, | ||
| valueExtractor, | ||
| options, | ||
| ) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5.8kb gzipped https://bundlephobia.com/package/sorted-btree@1.8.1
This is enough extra code weight (~24% increase to tanstack/db) that depending on where the crossover point ends up being, this could be an opt-in thing. I.e. only use if you have 50k+ items in a a collection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's the idea. We want to do some initial benchmarking to see when the turnover point is between using the array version or the tree version. We could automatically switch between them based on the size of the collection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok perfect, yeah that'd be easy with an async import 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews problem with using an async import is that it propagates to our API but d2mini is sync, i don't think we want to make it async. Not sure how to get around this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kevin-dp we can treat it like a JIT optimization perhaps? If the first sync run is too slow/big, we load sorted-btree in the background and start using it when it's loaded.
I agree we shouldn't make this async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews yes something we could do later if need be. For now, i introduced an async
loadBTreefunction that must be called before using the tree variant of the operator. That way, we can keep the operator sync.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just-in-time data structures in the wild 😃