AK: Make some Stream::read* functions available inline

These are quite bottlenecky in wasm, the next commit will try to make
use of this by calling them directly instead of doing a vcall, and
having them inlineable helps the compiler a bit.
This commit is contained in:
Ali Mohammad Pur
2025-06-06 14:09:42 +02:00
committed by Ali Mohammad Pur
parent bd7c188b86
commit 834fb0be36
Notes: github-actions[bot] 2025-08-08 10:57:07 +00:00
6 changed files with 33 additions and 39 deletions

View File

@@ -15,13 +15,6 @@ ConstrainedStream::ConstrainedStream(MaybeOwned<Stream> stream, u64 limit)
{
}
ErrorOr<Bytes> ConstrainedStream::read_some(Bytes bytes)
{
auto result = TRY(m_stream->read_some(bytes.trim(m_limit)));
m_limit -= result.size();
return result;
}
ErrorOr<void> ConstrainedStream::discard(size_t discarded_bytes)
{
if (discarded_bytes >= m_limit)

View File

@@ -16,7 +16,12 @@ class ConstrainedStream : public Stream {
public:
ConstrainedStream(MaybeOwned<Stream>, u64 limit);
virtual ErrorOr<Bytes> read_some(Bytes) override;
virtual ErrorOr<Bytes> read_some(Bytes bytes) override
{
auto result = TRY(m_stream->read_some(bytes.trim(m_limit)));
m_limit -= result.size();
return result;
}
virtual ErrorOr<void> discard(size_t discarded_bytes) override;
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;

View File

@@ -44,13 +44,6 @@ ErrorOr<void> FixedMemoryStream::truncate(size_t)
return Error::from_errno(EBADF);
}
ErrorOr<Bytes> FixedMemoryStream::read_some(Bytes bytes)
{
auto read = m_bytes.slice(m_offset).copy_trimmed_to(bytes);
m_offset += read;
return bytes.trim(read);
}
ErrorOr<void> FixedMemoryStream::read_until_filled(AK::Bytes bytes)
{
if (remaining() < bytes.size())

View File

@@ -30,7 +30,12 @@ public:
virtual bool is_open() const override;
virtual void close() override;
virtual ErrorOr<void> truncate(size_t) override;
virtual ErrorOr<Bytes> read_some(Bytes bytes) override;
virtual ErrorOr<Bytes> read_some(Bytes bytes) override
{
auto read = m_bytes.slice(m_offset).copy_trimmed_to(bytes);
m_offset += read;
return bytes.trim(read);
}
virtual ErrorOr<void> read_until_filled(Bytes bytes) override;
virtual ErrorOr<size_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override;

View File

@@ -12,28 +12,6 @@
namespace AK {
ErrorOr<void> Stream::read_until_filled(Bytes buffer)
{
size_t nread = 0;
while (nread < buffer.size()) {
if (is_eof())
return Error::from_string_literal("Reached end-of-file before filling the entire buffer");
auto result = read_some(buffer.slice(nread));
if (result.is_error()) {
if (result.error().is_errno() && result.error().code() == EINTR) {
continue;
}
return result.release_error();
}
nread += result.value().size();
}
return {};
}
ErrorOr<ByteBuffer> Stream::read_until_eof(size_t block_size)
{
return read_until_eof_impl(block_size);

View File

@@ -29,7 +29,27 @@ public:
virtual ErrorOr<Bytes> read_some(Bytes) = 0;
/// Tries to fill the entire buffer through reading. Returns whether the
/// buffer was filled without an error.
virtual ErrorOr<void> read_until_filled(Bytes);
virtual ErrorOr<void> read_until_filled(Bytes buffer)
{
size_t nread = 0;
while (nread < buffer.size()) {
if (is_eof())
return Error::from_string_literal("Reached end-of-file before filling the entire buffer");
auto result = read_some(buffer.slice(nread));
if (result.is_error()) {
if (result.error().is_errno() && result.error().code() == EINTR) {
continue;
}
return result.release_error();
}
nread += result.value().size();
}
return {};
}
/// Reads the stream until EOF, storing the contents into a ByteBuffer which
/// is returned once EOF is encountered. The block size determines the size
/// of newly allocated chunks while reading.