speed up `first` and `only` for various types #52296
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.40) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 294 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.340
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #52296"
general
weight: 0.35
matthias314
This PR is the result of this Discourse thread. It speeds up
firstforArrayandStringandonlyforDict,IdDictandSet. Foronlythe speed up comes from usinglengthandfirstinstead of callingiteratetwice (once to get the first element, once to check that there are no more). I have also deleted dedicatedonlymethods forTuple,Intand the like because they are not faster than the standard method.Here are some benchmarks. They were generated by the function
applied to a
Vectorvwithn = 1000randomly generated containers of the given type having one element each. Below I only show the relevant output.first| type | master | new |
@inboundsmaster |@inboundsnew ||---|---|---|---|---|
|
String| 3.145 μs | 1.241 μs | 3.147 μs | 1.368 μs ||
Vector{Char}| 985.571 ns | 982.688 ns | 961.227 ns | 748.953 ns ||
Matrix{Char}| 1.275 μs | 1.276 μs | 1.277 μs | 866.526 ns ||
Array{Char, 3}| 1.273 μs | 1.268 μs | 1.302 μs | 872.368 ns |only| type | master | new |
@inboundsmaster |@inboundsnew ||---|---|---|---|---|
|
Dict{Char, Int64}| 16.931 μs | 5.940 μs | 6.167 μs | 5.830 μs ||
IdDict{Char, Int64}| 28.419 μs | 18.514 μs | 22.029 μs |15.923 μs ||
Set{Char}| 14.810 μs | 3.742 μs | 5.113 μs | 3.561 μs ||
Tuple{Int64}| 71.966 ns | 70.243 ns | 71.962 ns | 70.251 ns ||
Int64| 71.974 ns | 71.031 ns | 71.961 ns | 71.061 ns ||
Char| 102.729 ns | 102.744 ns | 100.216 ns | 100.071 ns |There are a couple of types where the new method
only(x, Val(:first))would be faster the current one (now accessible asonly(x, Val(:iterate)), but only when using@inbounds. However, if one adds@inbounds, then one can also switch tofirstinstead ofonlyto get a larger speed-up.Note that
onlyforIdDictis defined in iterators.jl since it is included later than iddict.jl into Base.jl. Maybe there is a more elegant way of doing it.