← Back to Pull requests

math: Keep `^(::Float64, ::Integer)` cheap to inline #64532

Closed
unknown closed this pull request without merging on Nov 03, 2026 08:08 UTC
View on GitHub
unknown unknown commented on Oct 31, 2026 21:29 UTC
Author

Since #53967 (1.12), ^(::Float64, ::Integer) is @inline but carries the large-exponent fallback (three pow_body(::Float64, ::Float64) calls) in its body. Every caller pays for that in its inlining cost. A ForwardDiff-style dual number with ^(a::Dual, n::Integer) = Dual(a.v^n, (n a.v^(n - 1)) . a.p) inlined on 1.11 (cost 83) but no longer does. The loop using it then calls ^ out of line, returns the dual through memory, and loses the vectorized partials. On the CI runtime benchmarks, apps/dual_gradient is 1.65× slower than on 1.10.

struct Dual{N} <: Real
    v::Float64
    p::NTuple{N,Float64}
end
Base.:^(a::Dual, n::Integer) = Dual(a.v^n, (n * a.v^(n - 1)) .* a.p)
first(code_typed(^, (Dual{4}, Int)))[1].inlining_cost  # 65535 on master, 84 with this PR

This PR makes ^ do a single out-of-line call again: pow_int checks the range, then either runs pow_body inline or tail-calls the large-exponent path. The second commit starts pow_body with xnlo = 0.0 again: since #62527 a zero err returns x*y, so the -0.0 from #55875 no longer affects any result. Results are bitwise identical to master on a 493k-case grid: signed zeros, Inf, NaN, subnormals, n in -70:70, the use_power_by_squaring edges, typemin/typemax, and Int32/UInt8/BigInt exponents.

Cycles per call on an M4 Pro (thread_selfcounts, min of 400, stable over 3 processes):

| | master | PR |
|---|---|---|
| Dual Rosenbrock gradient chunk (256 elements) | 16.8k | 10.5k |
| x^n, runtime n = 2 | 11.1 | 8.8 |
| x^n, runtime n = -7 | 16 | 12.5 |
| x^n, runtime n = 100_000 | 32.3 | 35.6 |

The last row is the extra call on the rare large-exponent path.

<details><summary>Benchmark</summary>

Base.:+(a::Dual{N}, b::Dual{N}) where {N} = Dual(a.v + b.v, a.p .+ b.p)
Base.:-(a::Dual{N}, b::Dual{N}) where {N} = Dual(a.v - b.v, a.p .- b.p)
Base.:*(a::Dual{N}, b::Dual{N}) where {N} = Dual(a.v * b.v, a.v .* b.p .+ b.v .* a.p)
Base.zero(::Type{Dual{N}}) where {N} = Dual(0.0, ntuple(_ -> 0.0, Val(N)))
Base.convert(::Type{Dual{N}}, x::Real) where {N} = Dual(Float64(x), ntuple(_ -> 0.0, Val(N)))
Base.convert(::Type{Dual{N}}, x::Dual{N}) where {N} = x
Base.promote_rule(::Type{Dual{N}}, ::Type{<:Real}) where {N} = Dual{N}
function rosenbrock(x::AbstractVector{T}) where {T}
    s = zero(T)
    for i in 1:length(x)-1
        s += 100 * (x[i+1] - x[i]^2)^2 + (1 - x[i])^2
    end
    return s
end
xd = [Dual(rand(), ntuple(k -> Float64(i == k), Val(4))) for i in 1:256]
@btime rosenbrock($xd)

</details>

🤖 Generated with Claude Code

Diff: 333 lines (+249 / -84) across 4 files Reviewer: oscardssmith Review duration: 3519m
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 333 lines diff across 4 files
0.42
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%
60%
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.348
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #64532"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% 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: 42.1% (MEDIUM) · Higher = larger blast radius
42.1%
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: 60.5% · Lower = rushed or shallow review
60.5%
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: 16.7% · Safe to merge
16.7%