Skip to content

Commit 362c872

Browse files
authored
Despecialize methods (#40)
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 84281b7 commit 362c872

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

.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
@@ -134,7 +134,7 @@ end
134134
Get the `MemoryRef` of `x`. This reference is guaranteed to be inbounds,
135135
except if `x` is empty, where it may point to one element past the end.
136136
"""
137-
Base.memoryref(x::MemoryView) = x.ref
137+
Base.memoryref(@nospecialize(x::MemoryView)) = x.ref
138138

139139
_get_mutability(::MemoryView{T, M}) where {T, M} = M
140140

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.0-DEV.966"
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
@@ -41,7 +41,7 @@ function Base.copy(x::MemoryView{T, M}) where {T, M}
4141
return unsafe_new_memoryview(M, memoryref(newmem), x.len)
4242
end
4343

44-
function Base.checkbounds(v::MemoryView, is...)
44+
function Base.checkbounds(@nospecialize(v::MemoryView), is...)
4545
checkbounds_lightboundserror(v, is...)
4646
end
4747

@@ -69,10 +69,10 @@ Base.unsafe_convert(::Type{Ptr{T}}, v::MemoryView{T}) where {T} = pointer(v)
6969
Base.cconvert(::Type{<:Ptr{T}}, v::MemoryView{T}) where {T} = v.ref
7070
Base.elsize(::Type{<:MemoryView{T}}) where {T} = Base.elsize(Memory{T})
7171
Base.sizeof(x::MemoryView) = Base.elsize(typeof(x)) * length(x)
72-
Base.strides(::MemoryView) = (1,)
72+
Base.strides(@nospecialize(::MemoryView)) = (1,)
7373

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

7777
function Base.mightalias(a::MemoryView{T}, b::MemoryView{T}) where {T}
7878
(isempty(a) | isempty(b)) && return false
@@ -126,7 +126,7 @@ function Base.getindex(v::MemoryView{T, M}, idx::Base.OneTo) where {T, M}
126126
return unsafe_new_memoryview(M, v.ref, last(idx))
127127
end
128128

129-
Base.getindex(v::MemoryView, ::Colon) = v
129+
Base.getindex(@nospecialize(v::MemoryView), ::Colon) = v
130130
Base.@propagate_inbounds Base.view(v::MemoryView, idx::AbstractUnitRange) = v[idx]
131131

132132
# Efficient way to get `mem[1:include_last]`.
@@ -398,9 +398,9 @@ end
398398
function Iterators.reverse(mem::MemoryView{T}) where {T}
399399
return ReverseMemoryView{T}(ImmutableMemoryView(mem))
400400
end
401-
Iterators.reverse(x::ReverseMemoryView) = x.mem
401+
Iterators.reverse(@nospecialize(x::ReverseMemoryView)) = x.mem
402402

403-
Base.length(x::ReverseMemoryView) = length(x.mem)
403+
Base.length(@nospecialize(x::ReverseMemoryView)) = length(x.mem)
404404
Base.eltype(::Type{ReverseMemoryView{T}}) where {T} = T
405405

406406
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)