Skip to content

Commit df63db6

Browse files
authored
Merge pull request #22210 from JuliaLang/teh/inlining_cost_model
Decide inline-worthiness based on a more nuanced cost model
2 parents d6a0bbd + 7de02a3 commit df63db6

9 files changed

+283
-211
lines changed

base/abstractarray.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ _getindex(::IndexStyle, A::AbstractArray, I...) =
927927
## IndexLinear Scalar indexing: canonical method is one Int
928928
_getindex(::IndexLinear, A::AbstractArray, i::Int) = (@_propagate_inbounds_meta; getindex(A, i))
929929
_getindex(::IndexLinear, A::AbstractArray) = (@_propagate_inbounds_meta; getindex(A, _to_linear_index(A)))
930-
function _getindex(::IndexLinear, A::AbstractArray, I::Int...)
930+
function _getindex(::IndexLinear, A::AbstractArray, I::Vararg{Int,M}) where M
931931
@_inline_meta
932932
@boundscheck checkbounds(A, I...) # generally _to_linear_index requires bounds checking
933933
@inbounds r = getindex(A, _to_linear_index(A, I...))
@@ -944,7 +944,7 @@ function _getindex(::IndexCartesian, A::AbstractArray)
944944
@_propagate_inbounds_meta
945945
getindex(A, _to_subscript_indices(A)...)
946946
end
947-
function _getindex(::IndexCartesian, A::AbstractArray, I::Int...)
947+
function _getindex(::IndexCartesian, A::AbstractArray, I::Vararg{Int,M}) where M
948948
@_inline_meta
949949
@boundscheck checkbounds(A, I...) # generally _to_subscript_indices requires bounds checking
950950
@inbounds r = getindex(A, _to_subscript_indices(A, I...)...)
@@ -1011,7 +1011,7 @@ _setindex!(::IndexStyle, A::AbstractArray, v, I...) =
10111011
## IndexLinear Scalar indexing
10121012
_setindex!(::IndexLinear, A::AbstractArray, v, i::Int) = (@_propagate_inbounds_meta; setindex!(A, v, i))
10131013
_setindex!(::IndexLinear, A::AbstractArray, v) = (@_propagate_inbounds_meta; setindex!(A, v, _to_linear_index(A)))
1014-
function _setindex!(::IndexLinear, A::AbstractArray, v, I::Int...)
1014+
function _setindex!(::IndexLinear, A::AbstractArray, v, I::Vararg{Int,M}) where M
10151015
@_inline_meta
10161016
@boundscheck checkbounds(A, I...)
10171017
@inbounds r = setindex!(A, v, _to_linear_index(A, I...))
@@ -1027,7 +1027,7 @@ function _setindex!(::IndexCartesian, A::AbstractArray, v)
10271027
@_propagate_inbounds_meta
10281028
setindex!(A, v, _to_subscript_indices(A)...)
10291029
end
1030-
function _setindex!(::IndexCartesian, A::AbstractArray, v, I::Int...)
1030+
function _setindex!(::IndexCartesian, A::AbstractArray, v, I::Vararg{Int,M}) where M
10311031
@_inline_meta
10321032
@boundscheck checkbounds(A, I...)
10331033
@inbounds r = setindex!(A, v, _to_subscript_indices(A, I...)...)

base/abstractarraymath.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ _rshps(shp, shp_i, sz, i, ::Tuple{}) =
397397
_reperr(s, n, N) = throw(ArgumentError("number of " * s * " repetitions " *
398398
"($n) cannot be less than number of dimensions of input ($N)"))
399399

400-
@propagate_inbounds function _repeat(A::AbstractArray, inner, outer)
400+
@noinline function _repeat(A::AbstractArray, inner, outer)
401401
shape, inner_shape = rep_shapes(A, inner, outer)
402402

403403
R = similar(A, shape)

base/array.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,23 @@ function reinterpret(::Type{T}, a::Array{S}) where T where S
170170
end
171171

172172
function reinterpret(::Type{T}, a::Array{S}, dims::NTuple{N,Int}) where T where S where N
173-
if !isbits(T)
174-
throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(T) is not a bits type"))
175-
end
176-
if !isbits(S)
177-
throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(S) is not a bits type"))
173+
function throwbits(::Type{S}, ::Type{T}, ::Type{U}) where {S,T,U}
174+
@_noinline_meta
175+
throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(U) is not a bits type"))
178176
end
177+
isbits(T) || throwbits(S, T, T)
178+
isbits(S) || throwbits(S, T, S)
179179
nel = div(length(a)*sizeof(S),sizeof(T))
180180
if prod(dims) != nel
181-
throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(nel)"))
181+
_throw_dmrsa(dims, nel)
182182
end
183183
ccall(:jl_reshape_array, Array{T,N}, (Any, Any, Any), Array{T,N}, a, dims)
184184
end
185185

186186
# reshaping to same # of dimensions
187187
function reshape(a::Array{T,N}, dims::NTuple{N,Int}) where T where N
188188
if prod(dims) != length(a)
189-
throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(a))"))
189+
_throw_dmrsa(dims, length(a))
190190
end
191191
if dims == size(a)
192192
return a
@@ -197,11 +197,16 @@ end
197197
# reshaping to different # of dimensions
198198
function reshape(a::Array{T}, dims::NTuple{N,Int}) where T where N
199199
if prod(dims) != length(a)
200-
throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(a))"))
200+
_throw_dmrsa(dims, length(a))
201201
end
202202
ccall(:jl_reshape_array, Array{T,N}, (Any, Any, Any), Array{T,N}, a, dims)
203203
end
204204

205+
function _throw_dmrsa(dims, len)
206+
@_noinline_meta
207+
throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $len"))
208+
end
209+
205210
## Constructors ##
206211

207212
similar(a::Array{T,1}) where {T} = Array{T,1}(size(a,1))

base/error.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ Raise an `ErrorException` with the given message.
2727
2828
See also [`logging`](@ref).
2929
"""
30-
error(s...) = throw(ErrorException(Main.Base.string(s...)))
30+
function error(s::Vararg{Any,N}) where {N}
31+
@_noinline_meta
32+
throw(ErrorException(Main.Base.string(s...)))
33+
end
3134

3235
"""
3336
rethrow([e])
@@ -53,7 +56,10 @@ Get the backtrace of the current exception, for use within `catch` blocks.
5356
catch_backtrace() = ccall(:jl_get_backtrace, Array{Ptr{Void},1}, ())
5457

5558
## keyword arg lowering generates calls to this ##
56-
kwerr(kw, args...) = throw(MethodError(typeof(args[1]).name.mt.kwsorter, (kw,args...)))
59+
function kwerr(kw, args::Vararg{Any,N}) where {N}
60+
@_noinline_meta
61+
throw(MethodError(typeof(args[1]).name.mt.kwsorter, (kw,args...)))
62+
end
5763

5864
## system error handling ##
5965
"""

0 commit comments

Comments
 (0)