Skip to content

Commit 059e36d

Browse files
committed
work around a splatting penalty in twiceprecision (#29060)
* work around a splatting penalty in twiceprecision * add allocation test (cherry picked from commit 88d536a)
1 parent f7bbec5 commit 059e36d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

base/twiceprecision.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,14 @@ const F_or_FF = Union{AbstractFloat, Tuple{AbstractFloat,AbstractFloat}}
332332
asF64(x::AbstractFloat) = Float64(x)
333333
asF64(x::Tuple{AbstractFloat,AbstractFloat}) = Float64(x[1]) + Float64(x[2])
334334

335+
# Defined to prevent splatting in the function below which here has a performance impact
336+
_TP(x) = TwicePrecision{Float64}(x)
337+
_TP(x::Tuple{Any, Any}) = TwicePrecision{Float64}(x[1], x[2])
335338
function steprangelen_hp(::Type{Float64}, ref::F_or_FF,
336339
step::F_or_FF, nb::Integer,
337340
len::Integer, offset::Integer)
338-
StepRangeLen(TwicePrecision{Float64}(ref...),
339-
twiceprecision(TwicePrecision{Float64}(step...), nb), Int(len), offset)
341+
StepRangeLen(_TP(ref),
342+
twiceprecision(_TP(step), nb), Int(len), offset)
340343
end
341344

342345
function steprangelen_hp(::Type{T}, ref::F_or_FF,

test/ranges.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,10 @@ end # module NonStandardIntegerRangeTest
13711371
end
13721372
end
13731373
end
1374+
1375+
@testset "allocation of TwicePrecision call" begin
1376+
0:286.493442:360
1377+
0:286:360
1378+
@test @allocated(0:286.493442:360) == 0
1379+
@test @allocated(0:286:360) == 0
1380+
end

0 commit comments

Comments
 (0)