-
Notifications
You must be signed in to change notification settings - Fork 8
Switch default storage to MemoryRef-based array #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jakobnissen
wants to merge
3
commits into
JuliaArrays:main
Choose a base branch
from
jakobnissen:memref
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Internal type used as the default, heap-backed parent container for FixedSizeArray. | ||
| # This is used over Memory{T}, because a MemoryRef inlines into the parent struct, and | ||
| # contains the pointer. This way, it saves a memory indirection over Memory. | ||
| # This type does not boundscheck, hence the unsafety. The FSA wrapper will handle boundschecks. | ||
| struct UnsafeRefArray{T} <: DenseVector{T} | ||
| ref::MemoryRef{T} | ||
| end | ||
|
|
||
| # TODO: Would be nice to be able to omit the check for n < 0 in the Memory | ||
| # constructor, but this is in the memorynew built-in and cannot be disabled | ||
| function UnsafeRefArray{T}(::UndefInitializer, n::Int) where T | ||
| mem = Memory{T}(undef, n) | ||
| UnsafeRefArray{T}(memoryref(mem)) | ||
| end | ||
|
|
||
| UnsafeRefArray{T}(::UndefInitializer, n::Tuple{Int}) where T = UnsafeRefArray{T}(undef, @inbounds(n[1])) | ||
|
|
||
| @static if VERSION < v"1.12.0-DEV.966" | ||
| Base.parent(x::UnsafeRefArray) = x.ref.mem | ||
| else | ||
| Base.parent(x::UnsafeRefArray) = parent(x.ref) | ||
| end | ||
|
|
||
| # This version introduced Base.memoryindex as public API | ||
| @static if VERSION < v"1.13.0-DEV.1289" | ||
| internal_memindex(x::MemoryRef) = Core.memoryrefoffset(x) | ||
| else | ||
| internal_memindex(x::MemoryRef) = Base.memoryindex(x) | ||
| end | ||
|
|
||
| function Base.checkbounds(A::UnsafeRefArray, is...) | ||
| checkbounds_lightboundserror(A, is...) | ||
| end | ||
|
|
||
| Base.size(x::UnsafeRefArray) = ((length(parent(x)) - internal_memindex(x.ref) + 1),) | ||
|
|
||
| Base.firstindex(::UnsafeRefArray) = 1 | ||
|
|
||
| function Base.getindex(x::UnsafeRefArray, i::Int) | ||
| @inbounds(memoryref(x.ref, i)[]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which part are you trying to
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both making the ref, and dereferencing it, both of which checks bounds. |
||
| end | ||
|
|
||
| function Base.setindex!(x::UnsafeRefArray{T}, v, i::Int) where T | ||
| vT = convert(T, v)::T | ||
| @inbounds(memoryref(x.ref, i)[] = vT) | ||
| end | ||
|
|
||
| Base.IndexStyle(::Type{<:UnsafeRefArray}) = Base.IndexLinear() | ||
|
|
||
| Base.elsize(::Type{UnsafeRefArray{T}}) where {T} = Base.elsize(Memory{T}) | ||
|
|
||
| function Base.similar(A::UnsafeRefArray, ::Type{S}, dims::Dims) where S | ||
| UnsafeRefArray{S}(undef, dims) | ||
| end | ||
|
|
||
| function Base.isassigned(x::UnsafeRefArray, i::Int) | ||
| @boundscheck checkbounds(Bool, x, i) || false | ||
| ref = @inbounds memoryref(x.ref, i) | ||
| isassigned(ref) | ||
| end | ||
|
|
||
| function Base.copy(x::UnsafeRefArray) | ||
| len = length(x) | ||
| newmem = Memory{eltype(x)}(undef, len) | ||
| newref = memoryref(newmem) | ||
| @inbounds unsafe_copyto!(newref, x.ref, len) | ||
| typeof(x)(newref) | ||
| end | ||
|
|
||
| function Base.unsafe_convert(::Type{Ptr{T}}, x::UnsafeRefArray) where T | ||
| Base.unsafe_convert(Ptr{T}, x.ref) | ||
| end | ||
|
|
||
| Base.dataids(x::UnsafeRefArray) = Base.dataids(parent(x)) | ||
|
|
||
| # Collects.jl interface for UnsafeRefArray | ||
|
|
||
| function (c::Collect)(::Type{UnsafeRefArray{T}}, collection) where {T} | ||
| if collection isa UnsafeRefArray{T} | ||
| return copy(collection) | ||
| end | ||
| if (T isa Type) && Base.IteratorSize(collection) isa Union{Base.HasLength, Base.HasShape} | ||
| result = UnsafeRefArray{T}(undef, Int(length(collection))::Int) | ||
| copyto!(result, collection) | ||
| return result | ||
| end | ||
| vec = c(Vector{T}, collection) | ||
| result = UnsafeRefArray{T}(undef, length(vec)) | ||
| copyto!(result, vec) | ||
| result | ||
| end | ||
|
|
||
| function (c::Collect)(::Type{UnsafeRefArray}, collection) | ||
| if collection isa UnsafeRefArray | ||
| return copy(collection) | ||
| end | ||
| vec = c(Vector, collection) | ||
| T = eltype(vec) | ||
| result = UnsafeRefArray{T}(undef, length(vec)) | ||
| copyto!(result, vec) | ||
| result | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we can't use
MemoryRefdirectly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
MemoryRef <: Ref, whereasFixedSizeArraymust be backed by aDenseVector: