Skip to content

Use top-k sorted list builder instead of full-list sorts in search indexes. #8814

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

isoos
Copy link
Collaborator

@isoos isoos commented Jun 11, 2025

@isoos isoos requested review from szakarias and sigurdm June 11, 2025 16:49
@@ -148,6 +148,50 @@ class DurationTracker extends LastNTracker<Duration> {
};
}

/// Builds a sorted list of the top-k items using the provided comparator.
///
/// The algorithm uses a binary tree insertion, resulting in O(N * log(K)) comparison.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a binary tree insertion, but insertion in a sorted list, complexity O(n*k) (the insertion step is linear in k)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, good point, I was ignoring the List insertion step, as in the comparison was dominating the sorting times much more. However, the reduced gain on high-k values may be due to the List insertion. I was certainly lazy in the insertion part, will take a look how to improve it.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could use an actual binary tree (though that comes with its own overhead)

If k is small enough, simple list insertion might be better

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if we stick with the list, consider using https://pub.dev/documentation/collection/latest/collection/binarySearch.html . Binary search is known as being notoriously hard to implement correctly

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In this case the implementation is very close to the one in package:collection, but I will certainly benchmark it, and if it about the same, we shall use it:
https://github.com/dart-lang/core/blob/main/pkgs/collection/lib/src/algorithms.dart#L47-L62

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

binarySearch in this case is not usable, as it returns -1 when the searched item is not in the collection, while this algorithm needs the index where the next item should be inserted into. Furthermore, in most of the cases there is no equality, it won't be in the collection. However, with close review, I don't think the algorithm is conceptually the same, except for this specific goal.

I've checked List.insert + List.removeLast() (if the list is over k) and overall did perform worse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we know both k and N upfront, we could probably decide which sorting is more worthwhile: this top-k list building for low k or a full list + single sort upfront otherwise. (We could also limit k, e.g. the number of text search results to 1000 or some reasonably high number).

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