Skip to content

Commit

Permalink
stream: fix reading LibuvStream into array
Browse files Browse the repository at this point in the history
Adds a new abstraction `take!(::Array{T,N}, ::Array{T,N})` for doing an
efficient `copyto!` equivalent. Previously it was assumed that `compact`
did this automatically, which wasn't a great assumption.

Fixes #56078
  • Loading branch information
vtjnash committed Oct 10, 2024
1 parent d4987a3 commit b98ef6d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
11 changes: 11 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ copy
return $(Expr(:new, :(typeof(a)), :(memoryref(newmem)), :(a.size)))
end

# a mutating version of copyto! that results in dst aliasing src afterwards
function take!(dst::Array{T,N}, src::Array{T,N}) where {T,N}
if getfield(dst, :ref) !== getfield(src, :ref)
setfield!(dst, :ref, getfield(src, :ref))
end
if getfield(dst, :size) !== getfield(src, :size)
setfield!(dst, :size, getfield(src, :size))
end
return dst
end

## Constructors ##

similar(a::Array{T,1}) where {T} = Vector{T}(undef, size(a,1))
Expand Down
4 changes: 4 additions & 0 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1541,3 +1541,7 @@ function countlines(io::IO; eol::AbstractChar='\n')
end

countlines(f::AbstractString; eol::AbstractChar = '\n') = open(io->countlines(io, eol = eol), f)::Int

# double mutating version of take!
take!(b::Vector{UInt8}, io::IO) = take!(b, take!(io))
_unsafe_take!(b::Vector{UInt8}, io::IO) = take!(b, _unsafe_take!(io))
2 changes: 1 addition & 1 deletion base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ function readbytes!(s::LibuvStream, a::Vector{UInt8}, nb::Int)
finally
s.buffer = sbuf
end
compact(newbuf)
_unsafe_take!(a, newbuf)
end
iolock_end()
return nread
Expand Down

0 comments on commit b98ef6d

Please sign in to comment.