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 15, 2024
1 parent d09abe5 commit 62071f0
Show file tree
Hide file tree
Showing 3 changed files with 18 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 @@ -355,6 +355,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))
4 changes: 3 additions & 1 deletion base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ function readbytes!(s::LibuvStream, a::Vector{UInt8}, nb::Int)
if bytesavailable(sbuf) >= nb
nread = readbytes!(sbuf, a, nb)
else
initsize = length(a)
newbuf = PipeBuffer(a, maxsize=nb)
newbuf.size = newbuf.offset # reset the write pointer to the beginning
nread = try
Expand All @@ -951,7 +952,8 @@ function readbytes!(s::LibuvStream, a::Vector{UInt8}, nb::Int)
finally
s.buffer = sbuf
end
compact(newbuf)
_unsafe_take!(a, newbuf)
length(a) >= initsize || resize!(a, initsize)
end
iolock_end()
return nread
Expand Down

0 comments on commit 62071f0

Please sign in to comment.