From 398f7ae988fc868a992ebccff81c9e786ddac2ec Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Mon, 10 Jul 2023 20:34:56 -0400 Subject: [PATCH] AK: Move chunks a single time in `cleanup_unused_chunks()` All elements of the vector were moved to the left, for each element to remove. This patch makes the function move each element exactly once. On the same test case as the previous commit, it makes the function disappear from the profile. These two commits combined reduce the decompression time by 12%. --- AK/MemoryStream.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/AK/MemoryStream.cpp b/AK/MemoryStream.cpp index c65c5234ccf..a5c8fc1985f 100644 --- a/AK/MemoryStream.cpp +++ b/AK/MemoryStream.cpp @@ -270,14 +270,14 @@ ErrorOr AllocatingMemoryStream::next_write_range() void AllocatingMemoryStream::cleanup_unused_chunks() { - // FIXME: Move these all at once. - while (m_read_offset >= CHUNK_SIZE) { - VERIFY(m_write_offset >= m_read_offset); + VERIFY(m_write_offset >= m_read_offset); - auto buffer = m_chunks.take_first(); - m_read_offset -= CHUNK_SIZE; - m_write_offset -= CHUNK_SIZE; - } + auto const chunks_to_remove = m_read_offset / CHUNK_SIZE; + + m_chunks.remove(0, chunks_to_remove); + + m_read_offset -= CHUNK_SIZE * chunks_to_remove; + m_write_offset -= CHUNK_SIZE * chunks_to_remove; } }