Skip to content

Commit 54299d9

Browse files
giordanotopolarity
andauthored
Remove redundant convert in _setindex! (JuliaLang#56178)
Follow up to JuliaLang#56034, ref: JuliaLang#56034 (comment). --------- Co-authored-by: Cody Tapscott <[email protected]>
1 parent a9acdae commit 54299d9

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

base/array.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,11 +999,11 @@ function setindex!(A::Array{T}, x, i1::Int, i2::Int, I::Int...) where {T}
999999
x = x isa T ? x : convert(T, x)::T
10001000
return _setindex!(A, x, i1, i2, I...)
10011001
end
1002-
function _setindex!(A::Array{T}, x, i1::Int, i2::Int, I::Int...) where {T}
1002+
function _setindex!(A::Array{T}, x::T, i1::Int, i2::Int, I::Int...) where {T}
10031003
@inline
10041004
@_noub_if_noinbounds_meta
10051005
@boundscheck checkbounds(A, i1, i2, I...) # generally _to_linear_index requires bounds checking
1006-
memoryrefset!(memoryrefnew(A.ref, _to_linear_index(A, i1, i2, I...), false), x isa T ? x : convert(T,x)::T, :not_atomic, false)
1006+
memoryrefset!(memoryrefnew(A.ref, _to_linear_index(A, i1, i2, I...), false), x, :not_atomic, false)
10071007
return A
10081008
end
10091009

base/genericmemory.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,18 @@ getindex(A::Memory, c::Colon) = copy(A)
235235

236236
## Indexing: setindex! ##
237237

238-
function setindex!(A::Memory{T}, x, i1::Int) where {T}
239-
val = x isa T ? x : convert(T,x)::T
238+
function _setindex!(A::Memory{T}, x::T, i1::Int) where {T}
240239
ref = memoryrefnew(memoryref(A), i1, @_boundscheck)
241-
memoryrefset!(ref, val, :not_atomic, @_boundscheck)
240+
memoryrefset!(ref, x, :not_atomic, @_boundscheck)
242241
return A
243242
end
244243

244+
function setindex!(A::Memory{T}, x, i1::Int) where {T}
245+
@_propagate_inbounds_meta
246+
val = x isa T ? x : convert(T,x)::T
247+
return _setindex!(A, val, i1)
248+
end
249+
245250
function setindex!(A::Memory{T}, x, i1::Int, i2::Int, I::Int...) where {T}
246251
@inline
247252
@boundscheck (i2 == 1 && all(==(1), I)) || throw_boundserror(A, (i1, i2, I...))

0 commit comments

Comments
 (0)