Skip to content

Commit b1e5429

Browse files
authored
cleanup and slightly improve speed (#1)
This gets rid of a lot of the compiler internals that aren't needed. It also changes the representation of `FixedSizeArray` to a `Memory` rather than a `MemoryRef` because the `MemoryRef` was only needed to allow the backing to change.
1 parent a08ce50 commit b1e5429

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/FixedSizeArrays.jl

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,28 @@ module FixedSizeArrays
33
export FixedSizeArray, FixedSizeVector, FixedSizeMatrix
44

55
mutable struct FixedSizeArray{T,N} <: DenseArray{T,N}
6-
ref::MemoryRef{T}
6+
mem::Memory{T}
77
const size::NTuple{N,Int}
88
end
99

1010
const FixedSizeVector{T} = FixedSizeArray{T,1}
1111
const FixedSizeMatrix{T} = FixedSizeArray{T,2}
1212

13-
eval(:(function (self::Type{FixedSizeArray{T,N}})(::UndefInitializer, size::Vararg{Int,N}) where {T,N}
14-
mem = fieldtype(fieldtype(self, :ref), :mem)(undef, prod(size))
15-
return $(Expr(:new, :self, :(Core.memoryref(mem)), :(size)))
16-
end))
13+
function (self::Type{FixedSizeArray{T,N}})(::UndefInitializer, size::Vararg{Int,N}) where {T,N}
14+
return FixedSizeArray(Memory{T}(undef, prod(size)), size)
15+
end
1716

18-
function Base.setindex!(A::FixedSizeArray{T}, x, i::Int) where {T}
19-
Base.@_noub_if_noinbounds_meta
20-
@boundscheck (i - 1)%UInt < length(A)%UInt || throw_boundserror(A, (i,))
21-
Core.memoryrefset!(Core.memoryref(A.ref, i, false), x isa T ? x : convert(T,x)::T, :not_atomic, false)
17+
Base.@propagate_inbounds function Base.setindex!(A::FixedSizeArray{T}, x, i::Int) where {T}
18+
getfield(A, :mem)[i] = x
2219
return A
2320
end
24-
function Base.setindex!(A::FixedSizeArray{T}, x, i1::Int, i2::Int, I::Int...) where {T}
25-
@inline
26-
Base.@_noub_if_noinbounds_meta
21+
Base.@inline function Base.setindex!(A::FixedSizeArray{T}, x, i1::Int, i2::Int, I::Int...) where {T}
2722
@boundscheck checkbounds(A, i1, i2, I...) # generally _to_linear_index requires bounds checking
28-
Core.memoryrefset!(Core.memoryref(A.ref, Base._to_linear_index(A, i1, i2, I...), false), x isa T ? x : convert(T,x)::T, :not_atomic, false)
23+
getfield(A, :mem)[Base._to_linear_index(A, i1, i2, I...)] = x
2924
return A
3025
end
31-
32-
function Base.getindex(A::FixedSizeArray, i::Int)
33-
Base.@_noub_if_noinbounds_meta
34-
@boundscheck Base.ult_int(Base.bitcast(UInt, Base.sub_int(i, 1)), Base.bitcast(UInt, length(A))) || throw_boundserror(A, (i,))
35-
Core.memoryrefget(Core.memoryref(getfield(A, :ref), i, false), :not_atomic, false)
26+
Base.@propagate_inbounds function Base.getindex(A::FixedSizeArray, i::Int)
27+
getfield(A, :mem)[i]
3628
end
3729
function Base.getindex(A::FixedSizeArray, i1::Int, i2::Int, I::Int...)
3830
@inline

0 commit comments

Comments
 (0)