From d92548c5b0203eb24885d1acb2d0555c05bfe702 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 22 May 2021 19:54:52 +0200 Subject: [PATCH] AK: Avoid pagefaults when repeatedly enqueing/dequeing items in a Queue When repeatedly enqueing and dequeing a single item in a Queue we end up faulting in all the pages for the underlying Vector. This is a performance issue - especially where the element type is large. --- AK/Queue.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/AK/Queue.h b/AK/Queue.h index d52026f107f..c87750a8792 100644 --- a/AK/Queue.h +++ b/AK/Queue.h @@ -46,6 +46,13 @@ public: m_index_into_first = 0; } --m_size; + if (m_size == 0 && !m_segments.is_empty()) { + // This is not necessary for correctness but avoids faulting in + // all the pages for the underlying Vector in the case where + // the caller repeatedly enqueues and then dequeues a single item. + m_index_into_first = 0; + m_segments.last()->data.clear_with_capacity(); + } return value; }