-
Notifications
You must be signed in to change notification settings - Fork 472
solve for accidental quadratic performance from min max methods #267
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
vanvoorden
wants to merge
1
commit into
apple:main
Choose a base branch
from
vanvoorden:min-max-performance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5
−5
Open
Changes from all commits
Commits
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -270,9 +270,9 @@ extension Collection { | |||||
| return [] | ||||||
| } | ||||||
|
|
||||||
| // If we're attempting to prefix more than 10% of the collection, it's | ||||||
| // If we're attempting to prefix more than log n of the collection, it's | ||||||
| // faster to sort everything. | ||||||
| guard prefixCount < (self.count / 10) else { | ||||||
| guard prefixCount <= self.count._binaryLogarithm() else { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need underscored API for this:
Suggested change
|
||||||
| return Array(try sorted(by: areInIncreasingOrder).prefix(prefixCount)) | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -326,9 +326,9 @@ extension Collection { | |||||
| return [] | ||||||
| } | ||||||
|
|
||||||
| // If we're attempting to prefix more than 10% of the collection, it's | ||||||
| // If we're attempting to suffix more than log n of the collection, it's | ||||||
| // faster to sort everything. | ||||||
| guard suffixCount < (self.count / 10) else { | ||||||
| guard suffixCount <= self.count._binaryLogarithm() else { | ||||||
| return Array(try sorted(by: areInIncreasingOrder).suffix(suffixCount)) | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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.
Nit: Your implementation considers anything more than log2(n) to be “large” (not ln or log10); log2 isn’t one of the bases abbreviated “log”.
Uh oh!
There was an error while loading. Please reload this page.
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.
@xwu Ahh… that's a good point!
TBH… I'm also open to hearing better ideas for how to compute this new threshold. Taking the binary logarithm guarantees our
O(n log n)worst-case complexity but is also maybe more conservative than necessary. For anArrayof one million elements this only gives us the first 19 elements before we sort the wholeArray.Adding one to the result of the binary logarithm gives us approximately log1.41 for the first 20 elements. We can keep going down that road and adding more… but I don't have a great answer how to analytically solve for what the optimal threshold should be that guarantees our
O(n log n)worst-case complexity but keeps working on the "fast path" as much as possible.