Skip to content

Commit 49474ca

Browse files
committed
Despecialize methods
Despecialize methods where there is no performance benefit of specialization, e.g. `size(::MemoryView)`, where all specializations compile to the same asm code.
1 parent 18f4d0a commit 49474ca

4 files changed

Lines changed: 13 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ Manifest.toml
2828
TODO.md
2929

3030
*.code-workspace
31+
/.claude

src/MemoryViews.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ end
129129
Get the `MemoryRef` of `x`. This reference is guaranteed to be inbounds,
130130
except if `x` is empty, where it may point to one element past the end.
131131
"""
132-
Base.memoryref(x::MemoryView) = x.ref
132+
Base.memoryref(@nospecialize(x::MemoryView)) = x.ref
133133

134134
_get_mutability(::MemoryView{T, M}) where {T, M} = M
135135

src/basic.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ end
99
# The parent method for memoryref was added in 1.12. In versions before that,
1010
# it can be accessed by reaching into internals.
1111
@static if VERSION < v"1.12"
12-
Base.parent(v::MemoryView) = v.ref.mem
12+
Base.parent(@nospecialize(v::MemoryView)) = v.ref.mem
1313
else
14-
Base.parent(v::MemoryView) = parent(v.ref)
14+
Base.parent(@nospecialize(v::MemoryView)) = parent(v.ref)
1515
end
1616

17-
Base.size(v::MemoryView) = (v.len,)
18-
Base.IndexStyle(::Type{<:MemoryView}) = Base.IndexLinear()
17+
Base.size(@nospecialize(v::MemoryView)) = (v.len,)
18+
Base.IndexStyle(@nospecialize(T::Type{<:MemoryView})) = Base.IndexLinear()
1919

2020
function Base.iterate(x::MemoryView, i::Int = 1)
2121
((i - 1) % UInt) < (length(x) % UInt) || return nothing
@@ -48,7 +48,7 @@ function Base.copy(x::MemoryView{T, M}) where {T, M}
4848
return unsafe_new_memoryview(M, memoryref(newmem), x.len)
4949
end
5050

51-
function Base.checkbounds(v::MemoryView, is...)
51+
function Base.checkbounds(@nospecialize(v::MemoryView), is...)
5252
checkbounds_lightboundserror(v, is...)
5353
end
5454

@@ -76,10 +76,10 @@ Base.unsafe_convert(::Type{Ptr{T}}, v::MemoryView{T}) where {T} = pointer(v)
7676
Base.cconvert(::Type{<:Ptr{T}}, v::MemoryView{T}) where {T} = v.ref
7777
Base.elsize(::Type{<:MemoryView{T}}) where {T} = Base.elsize(Memory{T})
7878
Base.sizeof(x::MemoryView) = Base.elsize(typeof(x)) * length(x)
79-
Base.strides(::MemoryView) = (1,)
79+
Base.strides(@nospecialize(::MemoryView)) = (1,)
8080

8181
# For two distinct element types, they can't alias
82-
Base.mightalias(::MemoryView, ::MemoryView) = false
82+
Base.mightalias(@nospecialize(::MemoryView), @nospecialize(::MemoryView)) = false
8383

8484
function Base.mightalias(a::MemoryView{T}, b::MemoryView{T}) where {T}
8585
(isempty(a) | isempty(b)) && return false
@@ -131,7 +131,7 @@ function Base.getindex(v::MemoryView{T, M}, idx::Base.OneTo) where {T, M}
131131
return unsafe_new_memoryview(M, v.ref, last(idx))
132132
end
133133

134-
Base.getindex(v::MemoryView, ::Colon) = v
134+
Base.getindex(@nospecialize(v::MemoryView), ::Colon) = v
135135
Base.@propagate_inbounds Base.view(v::MemoryView, idx::AbstractUnitRange) = v[idx]
136136

137137
# Efficient way to get `mem[1:include_last]`.
@@ -392,9 +392,9 @@ end
392392
function Iterators.reverse(mem::MemoryView{T}) where {T}
393393
return ReverseMemoryView{T}(ImmutableMemoryView(mem))
394394
end
395-
Iterators.reverse(x::ReverseMemoryView) = x.mem
395+
Iterators.reverse(@nospecialize(x::ReverseMemoryView)) = x.mem
396396

397-
Base.length(x::ReverseMemoryView) = length(x.mem)
397+
Base.length(@nospecialize(x::ReverseMemoryView)) = length(x.mem)
398398
Base.eltype(::Type{ReverseMemoryView{T}}) where {T} = T
399399

400400
function Base.iterate(x::ReverseMemoryView, state = length(x))

src/construction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MemoryView(v::MemoryView) = v
1+
MemoryView(@nospecialize(v::MemoryView)) = v
22

33
# Array and Memory
44
# Array with more than 1 dimension is not equal to the view, since they have different axes

0 commit comments

Comments
 (0)