← Back to Pull requests

speed up `first` and `only` for various types #52296

Closed
unknown closed this pull request without merging on Dec 04, 2023 14:58 UTC
View on GitHub
unknown unknown commented on Nov 25, 2023 03:23 UTC
Author

This PR is the result of this Discourse thread. It speeds up first for Array and String and only for Dict, IdDict and Set. For only the speed up comes from using length and first instead of calling iterate twice (once to get the first element, once to check that there are no more). I have also deleted dedicated only methods for Tuple, Int and the like because they are not faster than the standard method.

Here are some benchmarks. They were generated by the function

function runtest(v)
    y = only(v[1])
    @show eltype(v)
    @btime count(x -> first(x) == $y, $v)
    @btime count(x -> @inbounds(first(x)) == $y, $v)
    @btime count(x -> only(x) == $y, $v)
    @btime count(x -> @inbounds(only(x)) == $y, $v)
end

applied to a Vector v with n = 1000 randomly generated containers of the given type having one element each. Below I only show the relevant output.

first

| type | master | new | @inbounds master | @inbounds new |
|---|---|---|---|---|
| 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 | @inbounds master | @inbounds new |
|---|---|---|---|---|
| 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 as only(x, Val(:iterate)), but only when using @inbounds. However, if one adds @inbounds, then one can also switch to first instead of only to get a larger speed-up.

Note that only for IdDict is 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.

Diff: 294 lines (+220 / -74) across 3 files Reviewer: matthias314 Review duration: 13654m
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
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
56%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 56.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 39.9% (MEDIUM) · Higher = larger blast radius
39.9%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 56.8% · Lower = rushed or shallow review
56.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 17.3% · Safe to merge
17.3%