mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-12 11:09:18 +00:00
LibCore: Do short forward seeks by discarding bytes from the buffer
This saves us an actual seek and rereading already stored buffer data in cases where the seek is entirely covered by the currently buffered data. This is especially important since we implement `discard` using `seek` for seekable streams.
This commit is contained in:
parent
bdf991fe76
commit
add2e2c076
Notes:
sideshowbarker
2024-07-17 06:38:11 +09:00
Author: https://github.com/timschumi
Commit: add2e2c076
Pull-request: https://github.com/SerenityOS/serenity/pull/17042
Reviewed-by: https://github.com/LucasChollet ✅
2 changed files with 30 additions and 3 deletions
|
@ -819,6 +819,11 @@ public:
|
|||
m_buffer.clear();
|
||||
}
|
||||
|
||||
ErrorOr<void> discard_bytes(size_t count)
|
||||
{
|
||||
return m_buffer.discard(count);
|
||||
}
|
||||
|
||||
private:
|
||||
ErrorOr<size_t> populate_read_buffer()
|
||||
{
|
||||
|
@ -875,8 +880,15 @@ public:
|
|||
virtual void close() override { m_helper.stream().close(); }
|
||||
virtual ErrorOr<off_t> seek(i64 offset, SeekMode mode) override
|
||||
{
|
||||
if (mode == SeekMode::FromCurrentPosition)
|
||||
if (mode == SeekMode::FromCurrentPosition) {
|
||||
// If possible, seek using the buffer alone.
|
||||
if (0 <= offset && static_cast<u64>(offset) <= m_helper.buffered_data_size()) {
|
||||
MUST(m_helper.discard_bytes(offset));
|
||||
return TRY(m_helper.stream().tell()) - m_helper.buffered_data_size();
|
||||
}
|
||||
|
||||
offset = offset - m_helper.buffered_data_size();
|
||||
}
|
||||
|
||||
auto result = TRY(m_helper.stream().seek(offset, mode));
|
||||
m_helper.clear_buffer();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue