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

unsafe read/write #14766

Merged
merged 3 commits into from
Jan 26, 2016
Merged

unsafe read/write #14766

merged 3 commits into from
Jan 26, 2016

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Jan 22, 2016

as mentioned at #14678 (comment), this renames write(io, ptr, nb) to unsafe_write (for consistency with unsafe_load, etc.) and ensures it exists for all IO subtypes. this makes the interface for creating an IO stream much simpler and a bit more efficient.

i'll add the corresponding change to unsafe_read later done

@@ -104,7 +104,7 @@ function sendfile(dst::File, src::File, src_offset::Int64, bytes::Int)
nothing
end

function write(f::File, buf::Ptr{UInt8}, len::Integer, offset::Integer=-1)
function unsafe_write(f::File, buf::Ptr{UInt8}, len::UInt, offset::Int64=Int64(-1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be tightened from Integer? this might break packages that are hitting code paths not tested in base

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i also renamed it.....

i'll attempt to put in a deprecations for this. the problem is that write(io, p, l) is supposed to be the same as write(io, p); write(io, l), but many IO streams were abusing some variant of this method signature to express the unsafe_write concept.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right but the renamed version is getting called elsewhere. It looks like you have a more generic method that calls convert below.

@vtjnash vtjnash changed the title wip: unsafe write unsafe read/write Jan 24, 2016
@@ -178,23 +181,17 @@ function write(to::IO, from::IO)
end
end

@inline unsafe_read{T}(s::IO, p::Ref{T}, n::Integer) = unsafe_read(s, unsafe_convert(Ref{T}, p)::Ptr, n) # mark noinline to ensure ref is gc-rooted somewhere (by the caller)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using @inline but comment says @noinline ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, good find

@vtjnash
Copy link
Member Author

vtjnash commented Jan 25, 2016

seeing no comments, i assume this will be good to merge (once I fix tkelman's comment above)

if r < nb
throw(EOFError())
readbytes!(s::LibuvStream, a::Vector{UInt8}, nb = length(a)) = readbytes!(s, a, Int(nb))
function readbytes!(s::LibuvStream, a::Vector{UInt8}, nb::Int)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is almost entirely copy-pasted from below, which is not great.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i'm hoping to address this function in some future PR. right now, every implementation of it has slightly different behavior, but in significant ways.

…:Uint)

this better clarifies the behavior of this method (the fallback for write(io, xs...) usually calls write on each element of xs)
and provides a fallback implementation for every subtype IO
this also allows the deletion of a substantial amount of duplicated and special-case code that is now made redundant

once Ref can be stack-allocated, this approach should gain an extra speed bump
vtjnash added a commit that referenced this pull request Jan 26, 2016
add unsafe read/write as the lowest-level IO functions to enable IO subtypes to implement efficient block-transfers
@vtjnash vtjnash merged commit a4d847e into master Jan 26, 2016
@vtjnash vtjnash deleted the jn/unsafe_write branch January 26, 2016 04:05
end

if nb <= SZ_UNBUFFERED_IO # Under this limit we are OK with copying the array from the stream's buffer
wait_readnb(s, nb)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be wait_readnb(s, Int(nb))?

Edit: Sorry, I confused the signature on line 904 with line 905. Nevermind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. nb is already coerced to an Int in the call signature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants