Skip to content

Truly independent web browser

AK: Remove unused seekback support from CircularBuffer

Andreas Klingd86b95f32460
diff --git a/AK/CircularBuffer.cpp b/AK/CircularBuffer.cpp
index 8dbbde57321..5e10ab196da 100644
--- a/AK/CircularBuffer.cpp
+++ b/AK/CircularBuffer.cpp
@@ -82,7 +82,6 @@ void CircularBuffer::clear()
{
m_reading_head = 0;
m_used_space = 0;
- m_seekback_limit = 0;
}
Bytes CircularBuffer::next_write_span()
@@ -108,17 +107,6 @@ ReadonlyBytes CircularBuffer::next_read_span(size_t offset) const
return m_buffer.span().slice(reading_head, min(capacity() - reading_head, used_space));
}
-ReadonlyBytes CircularBuffer::next_seekback_span(size_t distance) const
-{
- VERIFY(m_seekback_limit <= capacity());
- VERIFY(distance <= m_seekback_limit);
-
- // Note: We are adding the capacity once here to ensure that we can wrap around the negative space by using modulo.
- auto read_offset = (capacity() + m_reading_head + m_used_space - distance) % capacity();
-
- return m_buffer.span().slice(read_offset, min(capacity() - read_offset, distance));
-}
-
size_t CircularBuffer::write(ReadonlyBytes bytes)
{
auto remaining = bytes.size();
@@ -132,10 +120,6 @@ size_t CircularBuffer::write(ReadonlyBytes bytes)
m_used_space += written_bytes;
- m_seekback_limit += written_bytes;
- if (m_seekback_limit > capacity())
- m_seekback_limit = capacity();
-
remaining -= written_bytes;
}
@@ -165,27 +149,6 @@ Bytes CircularBuffer::read(Bytes bytes)
return bytes.trim(bytes.size() - remaining);
}
-ErrorOr<Bytes> CircularBuffer::read_with_seekback(Bytes bytes, size_t distance) const
-{
- if (distance > m_seekback_limit)
- return Error::from_string_literal("Tried a seekback read beyond the seekback limit");
-
- auto remaining = bytes.size();
-
- while (remaining > 0) {
- auto const next_span = next_seekback_span(distance);
- if (next_span.size() == 0)
- break;
-
- auto written_bytes = next_span.copy_trimmed_to(bytes.slice(bytes.size() - remaining));
-
- distance -= written_bytes;
- remaining -= written_bytes;
- }
-
- return bytes.trim(bytes.size() - remaining);
-}
-
ErrorOr<void> CircularBuffer::discard(size_t discarding_size)
{
if (m_used_space < discarding_size)
@@ -205,10 +168,6 @@ ErrorOr<size_t> CircularBuffer::fill_from_stream(Stream& stream)
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();
}
@@ -229,30 +188,4 @@ ErrorOr<size_t> CircularBuffer::flush_to_stream(Stream& stream)
return written_bytes;
}
-ErrorOr<size_t> CircularBuffer::copy_from_seekback(size_t distance, size_t length)
-{
- if (distance > m_seekback_limit)
- return Error::from_string_literal("Tried a seekback copy beyond the seekback limit");
-
- auto remaining_length = length;
- while (remaining_length > 0) {
- if (empty_space() == 0)
- break;
-
- auto next_span = next_seekback_span(distance);
- if (next_span.size() == 0)
- break;
-
- auto length_written = write(next_span.trim(remaining_length));
- remaining_length -= length_written;
-
- // If we copied right from the end of the seekback area (i.e. our length is larger than the distance)
- // and the last copy was one complete "chunk", we can now double the distance to copy twice as much data in one go.
- if (remaining_length > distance && length_written == distance)
- distance *= 2;
- }
-
- return length - remaining_length;
-}
-
}
diff --git a/AK/CircularBuffer.h b/AK/CircularBuffer.h
index c951dc882a3..6c37447e154 100644
--- a/AK/CircularBuffer.h
+++ b/AK/CircularBuffer.h
@@ -30,12 +30,6 @@ public:
ErrorOr<size_t> fill_from_stream(Stream&);
ErrorOr<size_t> flush_to_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.
- ErrorOr<Bytes> read_with_seekback(Bytes bytes, size_t distance) const;
-
- ErrorOr<size_t> copy_from_seekback(size_t distance, size_t length);
-
[[nodiscard]] size_t empty_space() const;
[[nodiscard]] size_t used_space() const;
[[nodiscard]] size_t capacity() const;
@@ -44,20 +38,18 @@ public:
void clear();
-protected:
+private:
CircularBuffer(ByteBuffer);
[[nodiscard]] bool is_wrapping_around() const;
[[nodiscard]] Bytes next_write_span();
[[nodiscard]] ReadonlyBytes next_read_span(size_t offset = 0) const;
- [[nodiscard]] ReadonlyBytes next_seekback_span(size_t distance) const;
ByteBuffer m_buffer {};
size_t m_reading_head {};
size_t m_used_space {};
- size_t m_seekback_limit {};
};
}
diff --git a/Tests/AK/TestCircularBuffer.cpp b/Tests/AK/TestCircularBuffer.cpp
index 6808ae505fc..a320ce77ba6 100644
--- a/Tests/AK/TestCircularBuffer.cpp
+++ b/Tests/AK/TestCircularBuffer.cpp
@@ -328,18 +328,3 @@ TEST_CASE(offset_of_with_until_and_after_wrapping_around)
result = circular_buffer.offset_of("Well "sv, 14, 19);
EXPECT_EQ(result.value_or(42), 14ul);
}
-
-BENCHMARK_CASE(looping_copy_from_seekback)
-{
- auto circular_buffer = MUST(CircularBuffer::create_empty(16 * MiB));
-
- {
- auto written_bytes = circular_buffer.write("\0"sv.bytes());
- EXPECT_EQ(written_bytes, 1ul);
- }
-
- {
- auto copied_bytes = TRY_OR_FAIL(circular_buffer.copy_from_seekback(1, 15 * MiB));
- EXPECT_EQ(copied_bytes, 15 * MiB);
- }
-}