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

rearrange inlining of ensureroom and speed it up #27874

Merged
merged 3 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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