-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Add count(where:)
and tests
#16099
Add count(where:)
and tests
#16099
Conversation
@swift-ci please test |
does it have merge conflicts? (there's no way to for me to see) |
Seems like it merges fine. @airspeedswift, can you nominate someone to code review and merge this? Thanks. |
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.
Tests look good, but could you add a benchmark as well? Maybe one on String
or Set
for some combined coverage.
public func count( | ||
where predicate: (Element) throws -> Bool | ||
) rethrows -> Int { | ||
var count = 0 |
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.
Any reason to not use reduce
here?
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.
I think using a reduce is needlessly complicated both in terms of abstraction and in terms of code readability, which is why I went with the for loop. If you feel strongly, I can switch it to a reduce.
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.
I disagree. This kind of calculation is exactly what reduce
is for. It states declaratively what the code does instead of writing an open-coded for
loop, so improves readability. It's also important that the code in the std lib encourage the practice of using map/reduce/filter where appropriate.
(it does bug me you have to write try twice with reduce, tho)
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.
Hey, I know I'm very late to this but just now came across this proposal, I just have a question and my apologies if it's a dumb one, but wouldn't the below make more sense:
@inlinable
public func count<T: Sequence>(where predicate: (Element) throws -> Bool) rethrows -> Int
where T: Sequence, T.Element == Element, T: AnyObject {
var count = 0
for element in self {
if try predicate(element) {
count += 1
}
}
return count
}
- The method is explicitly constrained to
Sequence
types to avoid ambiguity withCollection.count
, which should help reduce the type checker's workload and resolve the performance issue. - The generic constraint
T: AnyObject
is added to restrict the method usage and aid the compiler in resolving type ambiguities more efficiently, although this might need to be adjusted based on the specific requirements and context.
re the diagnostic test - you could replace with |
@natecook1000 any feedback on the doc comment? |
@swift-ci test |
Anything left to do here? |
Build failed |
Build failed |
@tkremenek Ben asked for a performance test, but I don't know how to add that yet, so I've been putting it off. |
Is there any way for me to see why the build failed? I'm getting a 404. |
Those aren’t real failures, you get them when you kick off a rerun of the tests sometimes. The tests then ran and passed. |
I got help from @natecook1000 and am working on the benchmark today and tomorrow. Hopefully will have something soon. |
Can someone kick off a benchmark run to make sure everything is working right? |
@swift-ci Please smoke benchmark |
@swift-ci Please smoke test |
@swift-ci Please smoke benchmark |
Build comment file:Performance: -O
Performance: -Osize
Performance: -Onone
How to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the regressions before you merge the PR. Noise: Sometimes the performance results (not code size!) contain false alarms. Unexpected regressions which are marked with '(?)' are probably noise. If you see regressions which you cannot explain you can try to run the benchmarks again. If regressions still show up, please consult with the performance team (@eeckstein). Hardware Overview
|
Quite a difference between optimized and unoptimized for the range! |
What should we do about the range benchmark? I'm assuming 40 minutes is too long to include in the suite |
@swift-ci Please smoke benchmark |
Build comment file:Performance: -O
Performance: -Osize
Performance: -Onone
How to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the regressions before you merge the PR. Noise: Sometimes the performance results (not code size!) contain false alarms. Unexpected regressions which are marked with '(?)' are probably noise. If you see regressions which you cannot explain you can try to run the benchmarks again. If regressions still show up, please consult with the performance team (@eeckstein). Hardware Overview
|
@swift-ci Please smoke test |
Does this PR need anything else? |
I raised https://bugs.swift.org/browse/SR-8742 for the weird Range benchmark thing. |
* add count(where:) and tests * Revise count(where:) documentation * Remove errant word in abstract * add a benchmark for ranges and strings with help from @natecook1000 * update benchmark to use Array instead of Range
This pull request is a reference implementation and tests for the
count(where:)
function onSequence
. The relevant pitch thread can be found here.Several notes:
There's currently a failing diagnostic test:
This makes sense, because
count
is now a property and a function. Not sure what the recommended approach is here: do we delete this test? Do we change the expected error to "wrong number of parameters"?I didn't include
count(of:)
whereElement
isEquatable
, but I can, depending on what the temperature of the review is. If we do addcount(of:)
, I think we probably also want to add specializations for ranges, sets, etc.count(of:)
also might need special consideration forNSCountedSet
.