AK: Remove arbitrary 1 KB limit when filling a BufferedStream's buffer

When reading, we currently only fill a BufferedStream's buffer when it
is empty, and only with 1 KB of data. This means that while the buffer
defaults to a size of 16 KB, at least 15 KB is always unused.
This commit is contained in:
Timothy Flynn 2023-03-29 20:25:34 -04:00 committed by Andreas Kling
parent 8ff36e5910
commit 5c38b14045
Notes: sideshowbarker 2024-07-16 23:17:55 +09:00
3 changed files with 26 additions and 9 deletions

View file

@ -231,12 +231,11 @@ private:
if (m_buffer.empty_space() == 0)
return 0;
// TODO: Figure out if we can do direct writes in a comfortable way.
Array<u8, 1024> temporary_buffer;
auto const fillable_slice = temporary_buffer.span().trim(min(temporary_buffer.size(), m_buffer.empty_space()));
size_t nread = 0;
do {
auto result = stream().read_some(fillable_slice);
while (true) {
auto result = m_buffer.fill_from_stream(stream());
if (result.is_error()) {
if (!result.error().is_errno())
return result.release_error();
@ -246,11 +245,11 @@ private:
break;
return result.release_error();
}
auto const filled_slice = result.value();
VERIFY(m_buffer.write(filled_slice) == filled_slice.size());
nread += filled_slice.size();
nread += result.value();
break;
} while (true);
}
return nread;
}

View file

@ -6,6 +6,7 @@
#include <AK/CircularBuffer.h>
#include <AK/MemMem.h>
#include <AK/Stream.h>
namespace AK {
@ -189,4 +190,20 @@ ErrorOr<void> CircularBuffer::discard(size_t discarding_size)
return {};
}
ErrorOr<size_t> CircularBuffer::fill_from_stream(Stream& stream)
{
auto next_span = next_write_span();
if (next_span.size() == 0)
return 0;
auto bytes = TRY(stream.read_some(next_span));
m_used_space += bytes.size();
m_seekback_limit += bytes.size();
if (m_seekback_limit > capacity())
m_seekback_limit = capacity();
return bytes.size();
}
}

View file

@ -27,6 +27,7 @@ public:
size_t write(ReadonlyBytes bytes);
Bytes read(Bytes bytes);
ErrorOr<void> discard(size_t discarded_bytes);
ErrorOr<size_t> fill_from_stream(Stream&);
/// Compared to `read()`, this starts reading from an offset that is `distance` bytes
/// before the current write pointer and allows for reading already-read data.