Skip to content
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

Rename Memory wrapper from view to unsafe_vector #54273

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ export
pointer,
pointer_from_objref,
unsafe_wrap,
unsafe_vector,
unsafe_string,
reenable_sigint,
unsafe_copyto!,
Expand Down
8 changes: 4 additions & 4 deletions base/genericmemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,17 @@ end
end

"""
view(m::GenericMemory{M, T}, inds::Union{UnitRange, OneTo})
unsafe_vector(m::GenericMemory{M, T}, inds::Union{UnitRange, OneTo, Colon})

Create a vector `v::Vector{T}` backed by the specified indices of `m`. It is only safe to
resize `v` if `m` is subseqently not used.
"""
function view(m::GenericMemory{M, T}, inds::Union{UnitRange, OneTo}) where {M, T}
isempty(inds) && return T[] # needed to allow view(Memory{T}(undef, 0), 2:1)
function unsafe_vector(m::GenericMemory{M, T}, inds::Union{UnitRange, OneTo}) where {M, T}
isempty(inds) && return T[] # needed to allow unsafe_vector(Memory{T}(undef, 0), 2:1)
@boundscheck checkbounds(m, inds)
ref = MemoryRef(m, first(inds)) # @inbounds would be safe here but does not help performance.
dims = (Int(length(inds)),)
$(Expr(:new, :(Array{T, 1}), :ref, :dims))
end
end
view(m::GenericMemory, inds::Colon) = view(m, eachindex(m))
unsafe_vector(m::GenericMemory, inds::Colon) = unsafe_vector(m, eachindex(m))
8 changes: 4 additions & 4 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end

# allocate Vector{UInt8}s for IOBuffer storage that can efficiently become Strings
StringMemory(n::Integer) = unsafe_wrap(Memory{UInt8}, _string_n(n))
StringVector(n::Integer) = view(StringMemory(n), 1:n)::Vector{UInt8}
StringVector(n::Integer) = unsafe_vector(StringMemory(n), 1:n)::Vector{UInt8}

# IOBuffers behave like Files. They are typically readable and writable. They are seekable. (They can be appendable).

Expand Down Expand Up @@ -466,7 +466,7 @@ function take!(io::IOBuffer)
if nbytes == 0 || io.reinit
data = StringVector(0)
elseif io.writable
data = view(io.data, io.offset+1:nbytes+io.offset)
data = unsafe_vector(io.data, io.offset+1:nbytes+io.offset)
else
data = copyto!(StringVector(nbytes), 1, io.data, io.offset + 1, nbytes)
end
Expand All @@ -475,7 +475,7 @@ function take!(io::IOBuffer)
if nbytes == 0
data = StringVector(0)
elseif io.writable
data = view(io.data, io.ptr:io.ptr+nbytes-1)
data = unsafe_vector(io.data, io.ptr:io.ptr+nbytes-1)
else
data = read!(io, data)
end
Expand All @@ -501,7 +501,7 @@ state. This should only be used internally for performance-critical
It might save an allocation compared to `take!` (if the compiler elides the
Array allocation), as well as omits some checks.
"""
_unsafe_take!(io::IOBuffer) = view(io.data, io.offset+1:io.size)
_unsafe_take!(io::IOBuffer) = unsafe_vector(io.data, io.offset+1:io.size)

function write(to::IO, from::GenericIOBuffer)
written::Int = bytesavailable(from)
Expand Down
2 changes: 1 addition & 1 deletion base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ String(s::AbstractString) = print_to_string(s)
unsafe_wrap(::Type{Memory{UInt8}}, s::String) = ccall(:jl_string_to_genericmemory, Ref{Memory{UInt8}}, (Any,), s)
function unsafe_wrap(::Type{Vector{UInt8}}, s::String)
mem = unsafe_wrap(Memory{UInt8}, s)
view(mem, eachindex(mem))
unsafe_vector(mem, :)
end

Vector{UInt8}(s::CodeUnits{UInt8,String}) = copyto!(Vector{UInt8}(undef, length(s)), s)
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Base.reshape
Base.dropdims
Base.vec
Base.SubArray
Base.unsafe_vector
```

## Concatenation and permutation
Expand Down
36 changes: 18 additions & 18 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3214,38 +3214,38 @@ end
end
end

@testset "Wrapping Memory into Arrays with view and reshape" begin
@testset "Wrapping Memory into Arrays with unsafe_vector and reshape" begin
mem::Memory{Int} = Memory{Int}(undef, 10) .= 11:20

@test_throws DimensionMismatch reshape(mem, 10, 10)
@test_throws DimensionMismatch reshape(mem, 5)
@test_throws BoundsError view(mem, 1:10, 1:10)
@test_throws BoundsError view(mem, 1:11)
@test_throws BoundsError view(mem, 3:11)
@test_throws BoundsError view(mem, 0:4)

@test @inferred(view(mem, 1:5))::Vector{Int} == 11:15
@test @inferred(view(mem, 1:2))::Vector{Int} == 11:12
@test @inferred(view(mem, 1:10))::Vector{Int} == 11:20
@test @inferred(view(mem, 3:8))::Vector{Int} == 13:18
@test @inferred(view(mem, 20:19))::Vector{Int} == []
@test @inferred(view(mem, -5:-7))::Vector{Int} == []
@test @inferred(view(mem, :))::Vector{Int} == mem
@test_throws BoundsError unsafe_vector(mem, 1:11)
@test_throws BoundsError unsafe_vector(mem, 3:11)
@test_throws BoundsError unsafe_vector(mem, 0:4)

@test @inferred(unsafe_vector(mem, 1:5))::Vector{Int} == 11:15
@test @inferred(unsafe_vector(mem, 1:2))::Vector{Int} == 11:12
@test @inferred(unsafe_vector(mem, 1:10))::Vector{Int} == 11:20
@test @inferred(unsafe_vector(mem, 3:8))::Vector{Int} == 13:18
@test @inferred(unsafe_vector(mem, 20:19))::Vector{Int} == []
@test @inferred(unsafe_vector(mem, -5:-7))::Vector{Int} == []
@test @inferred(unsafe_vector(mem, :))::Vector{Int} == mem
@test @inferred(reshape(mem, 5, 2))::Matrix{Int} == reshape(11:20, 5, 2)

# 53990
@test @inferred(view(mem, unsigned(1):10))::Vector{Int} == 11:20
@test @inferred(unsafe_vector(mem, unsigned(1):10))::Vector{Int} == 11:20

empty_mem = Memory{Module}(undef, 0)
@test_throws BoundsError view(empty_mem, 0:1)
@test_throws BoundsError view(empty_mem, 1:2)
@test_throws BoundsError unsafe_vector(empty_mem, 0:1)
@test_throws BoundsError unsafe_vector(empty_mem, 1:2)
@test_throws DimensionMismatch reshape(empty_mem, 1)
@test_throws DimensionMismatch reshape(empty_mem, 1, 2, 3)
@test_throws ArgumentError reshape(empty_mem, 2^16, 2^16, 2^16, 2^16)

@test @inferred(view(empty_mem, 1:0))::Vector{Module} == []
@test @inferred(view(empty_mem, 10:3))::Vector{Module} == []
@test @inferred(view(empty_mem, :))::Vector{Module} == empty_mem
@test @inferred(unsafe_vector(empty_mem, 1:0))::Vector{Module} == []
@test @inferred(unsafe_vector(empty_mem, 10:3))::Vector{Module} == []
@test @inferred(unsafe_vector(empty_mem, :))::Vector{Module} == empty_mem
@test isempty(@inferred(reshape(empty_mem, 0, 7, 1))::Array{Module, 3})

offset_inds = OffsetArrays.IdOffsetRange(values=3:6, indices=53:56)
Expand Down