-
Notifications
You must be signed in to change notification settings - Fork 180
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
Allow passing pre-allocated buffer for response body #984
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,10 +128,36 @@ function readbody(stream::Stream, res::Response, decompress::Union{Nothing, Bool | |
end | ||
end | ||
|
||
# 2 most common types of IOBuffers | ||
const IOBuffers = Union{IOBuffer, Base.GenericIOBuffer{SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int64}}, true}}} | ||
|
||
function readbody!(stream::Stream, res::Response, buf_or_stream) | ||
if !iserror(res) | ||
if isbytes(res.body) | ||
res.body = read(buf_or_stream) | ||
if length(res.body) > 0 | ||
# user-provided buffer to read response body into | ||
# specify write=true to make the buffer writable | ||
# but also specify maxsize, which means it won't be grown | ||
# (we don't want to be changing the user's buffer for them) | ||
body = IOBuffer(res.body; write=true, maxsize=length(res.body)) | ||
if buf_or_stream isa BufferStream | ||
# if it's a BufferStream, the response body was gzip encoded | ||
# so using the default write is fastest because it utilizes | ||
# readavailable under the hood, for which BufferStream is optimized | ||
write(body, buf_or_stream) | ||
elseif buf_or_stream isa Stream | ||
# for HTTP.Stream, there's already an optimized read method | ||
# that just needs an IOBuffer to write into | ||
readall!(buf_or_stream, body) | ||
else | ||
error("unreachable") | ||
end | ||
else | ||
res.body = read(buf_or_stream) | ||
end | ||
elseif (res.body isa IOBuffers || res.body isa Base.GenericIOBuffer) && buf_or_stream isa Stream | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be simplified, since: julia> IOBuffers <: Base.GenericIOBuffer
true |
||
# optimization for IOBuffer response_stream to avoid temporary allocations | ||
readall!(buf_or_stream, res.body) | ||
else | ||
write(res.body, buf_or_stream) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sleepy drive by review alert
Should this be
Base.ensureroom(buf, buf.ptr + n - 1)
andpointer(data, buf.ptr)
and similarly forbufcheck
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, perhaps, but we're updating
buf.size
ourselves (and notbuf.ptr
), sobuf.size
is correct for this case. From what I can tell,buf.size
tracks the last byte written to the IOBuffer, whereasbuf.ptr
tracks the next by to read from the IOBuffer. So if that's right, then I think trackingbuf.size
is correct here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, writing to IOBuffer should advance both
ptr
andsize
:Note that seekstart will allow us reuse the parts of the buffer that were pre-allocated earlier. So I think we should be working with ptr here, as it says which part of buffer is free (>= ptr) and which is not (< ptr)