-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
GH-43949: [C++] io::BufferedInput: Fixing the invalid state with SetBufferSize #44387
base: main
Are you sure you want to change the base?
Conversation
|
@pitrou @felipecrv @wgtmac This might be a bit critical fixing Also cc @biljazovic sorry for so slow replying |
@@ -51,6 +51,7 @@ class BufferedBase { | |||
return !is_open_; | |||
} | |||
|
|||
// Reset the `buffer_` to a new buffer of size `buffer_size_` |
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.
// Reset the `buffer_` to a new buffer of size `buffer_size_` | |
// Resize buffer_ to buffer_size_ |
Let's keep consistent variable names in the comment across this file.
@@ -284,17 +285,30 @@ class BufferedInputStream::Impl : public BufferedBase { | |||
|
|||
// Resize internal read buffer. Note that the internal buffer-size | |||
// should be not larger than the raw_read_bound_. |
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.
// should be not larger than the raw_read_bound_. | |
// should not be larger than the raw_read_bound_. |
// | ||
// SetBufferSize will not change the buffer_pos_ and bytes_buffered_. |
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.
// | |
// SetBufferSize will not change the buffer_pos_ and bytes_buffered_. | |
// It will not change buffer states including buffer_pos_ and bytes_buffered_. |
new_buffer_size = | ||
std::min(new_buffer_size, buffer_size_ + (raw_read_bound_ - raw_read_total_)); |
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.
new_buffer_size = | |
std::min(new_buffer_size, buffer_size_ + (raw_read_bound_ - raw_read_total_)); | |
new_buffer_size = | |
std::min(new_buffer_size, buffer_pos_ + buffer_size_ + (raw_read_bound_ - raw_read_total_)); |
Isn't buffer_pos_
required to be kept as well?
@@ -350,7 +364,7 @@ class BufferedInputStream::Impl : public BufferedBase { | |||
} | |||
|
|||
Status DoBuffer() { | |||
// Fill buffer | |||
// Refill the buffer from the raw stream with `buffer_size_` bytes. |
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.
// Refill the buffer from the raw stream with `buffer_size_` bytes. | |
// Fill the buffer from the raw stream with at most `buffer_size_` bytes. |
Rationale for this change
See #43949
The problem is
Peek
andRead
both callsSetBufferSize
, however:Read
implicit says that, whenSetBufferSize
or read, the previous buffer is not being required. In this scenerio,bytes_buffered_
is always 0, sinceRead
will consume all data. Andnew_buffer_size == std::min(new_buffer_size, (raw_read_bound_ - raw_read_total_))
Peek
still requires the buffer-size here. So,bytes_buffered_
might not 0. WhenPeek
, thenew_buffer_size
would less than expected, which shrinks the bufferWhat changes are included in this PR?
Update the Logic of
SetBufferSize
bytes_buffered_ == 0
,SetBufferSize
can discard the current bufferSetBufferSize
should resize minimal tobuffer_size_ + (raw_read_bound_ - raw_read_total_)
, since it should considering the current bufferAre these changes tested?
Yes
Are there any user-facing changes?
Bugfix