Skip to content

Commit

Permalink
rearrange inlining of ensureroom and speed it up (#27874)
Browse files Browse the repository at this point in the history
* rearrange inlining of `ensureroom` and speed it up
  • Loading branch information
JeffBezanson authored and KristofferC committed Oct 30, 2018
1 parent 9704474 commit e2f7b2f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
12 changes: 10 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,19 @@ offset `do`. Return `dest`.
"""
function copyto!(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, n::Integer) where T
n == 0 && return dest
n > 0 || throw(ArgumentError(string("tried to copy n=", n, " elements, but n should be nonnegative")))
n > 0 || _throw_argerror(n)
if soffs < 1 || doffs < 1 || soffs+n-1 > length(src) || doffs+n-1 > length(dest)
throw(BoundsError())
end
unsafe_copyto!(dest, doffs, src, soffs, n)
return dest
end

# Outlining this because otherwise a catastrophic inference slowdown
# occurs, see discussion in #27874
function _throw_argerror(n)
@_noinline_meta
throw(ArgumentError(string("tried to copy n=", n, " elements, but n should be nonnegative")))
end

copyto!(dest::Array{T}, src::Array{T}) where {T} = copyto!(dest, 1, src, 1, length(src))
Expand All @@ -284,7 +292,7 @@ function fill!(dest::Array{T}, x) where T
for i in 1:length(dest)
@inbounds dest[i] = xT
end
dest
return dest
end

"""
Expand Down
19 changes: 13 additions & 6 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,9 @@ function compact(io::GenericIOBuffer)
return io
end

@inline ensureroom(io::GenericIOBuffer, nshort::Int) = ensureroom(io, UInt(nshort))
@inline function ensureroom(io::GenericIOBuffer, nshort::UInt)
@noinline function ensureroom_slowpath(io::GenericIOBuffer, nshort::UInt)
io.writable || throw(ArgumentError("ensureroom failed, IOBuffer is not writeable"))
if !io.seekable
nshort >= 0 || throw(ArgumentError("ensureroom failed, requested number of bytes must be ≥ 0, got $nshort"))
if !ismarked(io) && io.ptr > 1 && io.size <= io.ptr - 1
io.ptr = 1
io.size = 0
Expand All @@ -308,9 +306,18 @@ end
end
end
end
n = min(nshort + (io.append ? io.size : io.ptr-1), io.maxsize)
if n > length(io.data)
resize!(io.data, n)
return
end

@inline ensureroom(io::GenericIOBuffer, nshort::Int) = ensureroom(io, UInt(nshort))
@inline function ensureroom(io::GenericIOBuffer, nshort::UInt)
if !io.writable || (!io.seekable && io.ptr > 1)
ensureroom_slowpath(io, nshort)
end
n = min((nshort % Int) + (io.append ? io.size : io.ptr-1), io.maxsize)
l = length(io.data)
if n > l
_growend!(io.data, (n - l) % UInt)
end
return io
end
Expand Down

0 comments on commit e2f7b2f

Please sign in to comment.