From e73e5794462341f6ffa0bf46f03e4c4c5a884604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Mon, 7 Mar 2022 22:24:15 +0100 Subject: [PATCH] Tests: Introduce a HashTable benchmark for "table thrashing" Thrashing is what I call the situations where a table is mostly filled with deleted markers, causing an increase in size (at least temporarily) when a simple re-hash would be enough to get rid of those. This happens when a hash table (especially with many elements) has a lot of deletes and re-inserts done to it, which is what this benchmark does. --- Tests/AK/TestHashTable.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Tests/AK/TestHashTable.cpp b/Tests/AK/TestHashTable.cpp index a521763e23..2e3ddc5582 100644 --- a/Tests/AK/TestHashTable.cpp +++ b/Tests/AK/TestHashTable.cpp @@ -234,3 +234,21 @@ TEST_CASE(capacity_leak) } EXPECT(table.capacity() < 100u); } + +// Inserting and removing a bunch of elements will "thrash" the table, leading to a lot of "deleted" markers. +BENCHMARK_CASE(benchmark_thrashing) +{ + HashTable table; + // Ensure that there needs to be some copying when rehashing. + table.set(3); + table.set(7); + table.set(11); + table.set(13); + for (int i = 0; i < 10'000; ++i) { + table.set(-i); + } + for (int i = 0; i < 10'000'000; ++i) { + table.set(i); + table.remove(i); + } +}