ladybird/AK
kleines Filmröllchen 49d29c8298 AK: Rehash HashTable in-place instead of shrinking
As seen on TV, HashTable can get "thrashed", i.e. it has a bunch of
deleted buckets that count towards the load factor. This means that hash
tables which are large enough for their contents need to be resized.
This was fixed in 9d8da16 with a workaround that shrinks the HashTable
back down in these cases, as after the resize and re-hash the load
factor is very low again. However, that's not a good solution. If you
insert and remove repeatedly around a size boundary, you might get
frequent resizes, which involve frequent re-allocations.

The new solution is an in-place rehashing algorithm that I came up with.
(Do complain to me, I'm at fault.) Basically, it iterates the buckets
and re-hashes the used buckets while marking the deleted slots empty.
The issue arises with collisions in the re-hash. For this reason, there
are two kinds of used buckets during the re-hashing: the normal "used"
buckets, which are old and are treated as free space, and the
"re-hashed" buckets, which are new and treated as used space, i.e. they
trigger probing. Therefore, the procedure for relocating a bucket's
contents is as follows:
- Locate the "real" bucket of the contents with the hash. That bucket is
  the starting point for the target bucket, and the current (old) bucket
  is the bucket we want to move.
- While we still need to move the bucket:
  - If we're the target, something strange happened last iteration or we
    just re-hashed to the same location. We're done.
  - If the target is empty or deleted, just move the bucket. We're done.
  - If the target is a re-hashed full bucket, we probe by double-hashing
    our hash as usual. Henceforth, we move our target for the next
    iteration.
  - If the target is an old full bucket, we swap the target and to-move
buckets. Therefore, the bucket to move is a the correct location and the
former target, which still needs to find a new place, is now in the
bucket to move. So we can just continue with the loop; the target is
re-obtained from the bucket to move. This happens for each and every
bucket, though some buckets are "coincidentally" moved before their
point of iteration is reached. Either way, this guarantees full in-place
movement (even without stack storage) and therefore space complexity of
O(1). Time complexity is amortized O(2n) asssuming a good hashing
function.

This leads to a performance improvement of ~30% on the benchmark
introduced with the last commit.

Co-authored-by: Hendiadyoin1 <leon.a@serenityos.org>
2022-03-31 12:06:13 +02:00
..
.clang-tidy Meta: Add basic clang-tidy configuration 2021-11-14 22:52:35 +01:00
AllOf.h AK: Reimplement all_of in terms of find_if 2021-07-24 13:11:13 +04:30
AnyOf.h AK: Implement any_of using common implementation 2021-08-06 23:57:48 +02:00
ArbitrarySizedEnum.h AK: Add an ArbitrarySizedEnum template 2022-03-27 18:54:56 +02:00
Array.h AK: Array can tell its inner type 2022-02-26 16:01:26 +01:00
Assertions.h AK: Suppress clang-tidy warning on TODO() 2022-02-21 19:01:16 +02:00
Atomic.h AK: Prevent Atomic with complex types 2022-02-23 00:42:49 +00:00
Badge.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
Base64.cpp AK: Fix implicit and narrowing conversions in Base64 2022-03-16 16:19:53 +00:00
Base64.h AK+Userland: Make AK::decode_base64 return ErrorOr 2022-01-24 22:36:09 +01:00
BinaryBufferWriter.h AK: Add helper type for serializing structures into buffer 2021-07-18 19:58:17 +04:30
BinaryHeap.h AK: Add missing headers 2021-10-06 23:52:40 +01:00
BinarySearch.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
BitCast.h AK: Use __builtin_bit_cast if available 2021-08-21 13:48:59 +04:30
Bitmap.h AK: Make Bitmap construction OOM-fallible 2022-02-11 17:49:46 +02:00
BitmapView.h AK+Everywhere: Replace __builtin bit functions 2021-12-21 22:13:51 +01:00
BitStream.h LibCore: Introduce BigEndianInputBitStream 2022-01-22 01:13:42 +03:30
Buffered.h Everywhere: Switch from EnableIf to requires 2022-03-17 22:15:42 -07:00
BuiltinWrappers.h AK+Everywhere: Replace __builtin bit functions 2021-12-21 22:13:51 +01:00
BumpAllocator.h AK: Make BumpAllocator work in multi-threaded environments 2021-10-31 18:43:03 +01:00
ByteBuffer.h AK: Don't call memcpy() in ByteBuffer::append(u8) 2022-02-13 14:44:36 +01:00
ByteReader.h AK: Generalize ByteReader 2021-07-14 11:26:34 +04:30
CharacterTypes.h AK: Change static base36 character map to function-local constexpr 2022-02-10 10:22:54 +00:00
Checked.h AK: Resolve clang-tidy warnings about unusual assignment operators 2021-11-14 22:52:35 +01:00
CheckedFormatString.h AK: Add missing Array.h include to CheckedFormatString.h 2021-12-24 14:35:33 -08:00
CircularDeque.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
CircularDuplexStream.h Everywhere: Fix many spelling errors 2022-01-07 10:56:59 +01:00
CircularQueue.h AK: Resolve clang-tidy readability-bool-conversion warnings 2021-11-14 22:52:35 +01:00
CMakeLists.txt Meta: Add support for declaring components 2021-06-17 11:03:51 +02:00
Complex.h AK+Everywhere: Add sincos and use it in some places 2022-03-15 11:39:42 +01:00
Concepts.h AK: Add a 'OneOf' concept 2022-03-28 23:11:48 +02:00
DateConstants.h Everywhere: Deduplicate day/month name constants 2022-03-18 23:48:50 +00:00
DateTimeLexer.h Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Debug.h.in LibXML: Add a fairly basic XML parser 2022-03-28 23:11:48 +02:00
Demangle.h AK: Remove kfree definition 2022-01-16 11:18:04 +01:00
DisjointChunks.h AK: Skip over initial empty chunks in DisjointChunks 2022-02-27 00:11:14 +03:30
DistinctNumeric.h AK: Add non-const DistinctNumeric::value() getter 2022-03-27 18:54:56 +02:00
DoublyLinkedList.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
Endian.h AK: Add formatters for BigEndian and LittleEndian 2021-08-02 00:25:11 +02:00
EnumBits.h AK+Kernel: Implement and use EnumBits has_any_flag() 2021-07-16 11:49:50 +02:00
Error.h AK: VERIFY inside release_value_but_fixme_should_propagate_errors() 2022-02-16 19:49:41 -05:00
ExtraMathConstants.h Userland: Move non-standard math constants from math.h 2021-04-27 23:06:16 +02:00
FileStream.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
Find.h AK: Rewrite {AnyOf,AllOf,Find}.h to use the IteratorPairWith concept 2021-07-22 22:56:20 +02:00
FixedArray.h AK: Add at() indexing methods to FixedArray 2022-02-27 00:11:14 +03:30
FixedPoint.h AK: Add FixedPoint base 2 logarithm 2022-02-28 13:59:31 +01:00
FlyString.cpp AK: Implement FlyString's comparison operators in terms of StringView's 2022-01-29 23:08:27 +01:00
FlyString.h Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Format.cpp Everywhere: Switch from EnableIf to requires 2022-03-17 22:15:42 -07:00
Format.h AK: Fix typo in warnln_if() 2022-03-19 11:01:49 -07:00
Forward.h AK: Add forward declaration for Utf8CodePointIterator 2022-02-23 21:53:30 +00:00
Function.h AK: Suppress false positive readability-non-const-parameter in Function 2021-11-14 22:52:35 +01:00
GenericLexer.cpp AK: Exclude GenericLexer String APIs from the Kernel 2022-02-16 22:21:37 +01:00
GenericLexer.h AK: Add a 'is_not_any_of' similar to 'is_any_of' to GenericLexer 2022-03-28 23:11:48 +02:00
HashFunctions.h AK: Use a full-period xorshift PRNG for double_hash 2022-01-07 12:34:44 +01:00
HashMap.h AK: Remove return value from HashTable::remove() and HashMap::remove() 2022-03-07 00:08:22 +01:00
HashTable.h AK: Rehash HashTable in-place instead of shrinking 2022-03-31 12:06:13 +02:00
Hex.cpp AK: Return KString instead of String from encode_hex in the Kernel 2022-02-16 22:21:37 +01:00
Hex.h AK: Return KString instead of String from encode_hex in the Kernel 2022-02-16 22:21:37 +01:00
IDAllocator.h AK: Use get_random() in IDAllocator 2021-08-30 18:35:36 +02:00
IntegralMath.h AK: Move integral log2 and exp to IntegerMath.h 2022-02-06 17:52:33 +00:00
IntrusiveDetails.h AK: Add missing headers 2021-10-06 23:52:40 +01:00
IntrusiveList.h AK+Everywhere: Reduce the number of template parameters of IntrusiveList 2021-09-10 18:05:46 +03:00
IntrusiveListRelaxedConst.h AK+Everywhere: Reduce the number of template parameters of IntrusiveList 2021-09-10 18:05:46 +03:00
IntrusiveRedBlackTree.h AK+Kernel: Reduce the number of template parameters of IntrusiveRBTree 2021-09-10 18:05:46 +03:00
IPv4Address.h AK+Kernel: Return KString from IPv4Address::to_string() in the Kernel 2022-02-16 22:21:37 +01:00
IPv6Address.h AK: Add IPv6Address class 2022-03-08 23:05:44 +01:00
IterationDecision.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
Iterator.h AK: Explicitly name types in Iterator.h 2022-01-16 00:38:21 +03:30
JsonArray.h Everywhere: Make JSON serialization fallible 2022-02-27 20:37:57 +01:00
JsonArraySerializer.h Everywhere: Make JSON serialization fallible 2022-02-27 20:37:57 +01:00
JsonObject.h Everywhere: Make JSON serialization fallible 2022-02-27 20:37:57 +01:00
JsonObjectSerializer.h AK: Add float support for JsonValue and JsonObjectSerializer 2022-03-08 22:09:52 +01:00
JsonParser.cpp AK: Fix userland parsing of rounded floating point numbers 2022-02-16 07:22:51 -05:00
JsonParser.h AK: Remove unused String[256] from JsonParser 2022-03-10 18:43:09 +01:00
JsonPath.cpp AK: Add implied const qualifiers to the Json interface 2021-12-15 23:35:14 -08:00
JsonPath.h Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
JsonValue.cpp AK: Allow constructing a JsonValue from a StringView explicitly 2022-01-28 23:40:25 +01:00
JsonValue.h AK: Add float support for JsonValue and JsonObjectSerializer 2022-03-08 22:09:52 +01:00
kmalloc.cpp AK: Declare operators new and delete as global functions 2021-07-05 20:23:42 +02:00
kmalloc.h AK+Kernel: Avoid double memory clearing of HashTable buckets 2022-03-15 11:56:46 +01:00
kstdio.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
LEB128.h AK: Don't perform the shift when it's too large when decoding LEB128 2021-08-31 16:37:49 +02:00
LexicalPath.cpp AK: Add LexicalPath::prepend() 2021-11-22 09:03:47 +01:00
LexicalPath.h AK: Add LexicalPath::prepend() 2021-11-22 09:03:47 +01:00
MACAddress.h AK+Kernel: Return KString from MACAddress::to_string() in the Kernel 2022-02-16 22:21:37 +01:00
Math.h AK+Everywhere: Add sincos and use it in some places 2022-03-15 11:39:42 +01:00
MemMem.h Everywhere: Remove redundant inline keyword 2022-01-29 21:45:17 +02:00
Memory.h AK: Add naive implementations of AK::timing_safe_compare 2022-03-13 19:08:58 -07:00
MemoryStream.h Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr 2022-01-24 22:36:09 +01:00
NeverDestroyed.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
NoAllocationGuard.h Everywhere: Use my new serenityos.org e-mail :^) 2022-01-14 11:54:09 +01:00
Noncopyable.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
NonnullOwnPtr.h AK: Hide the infallible make<T> factory function from the Kernel 2022-02-03 23:33:20 +01:00
NonnullOwnPtrVector.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
NonnullPtrVector.h AK: Add const variant of Vector::in_reverse() 2022-03-18 15:18:48 +01:00
NonnullRefPtr.h AK: Mark smart pointer classes as [[nodiscard]] 2021-12-05 15:31:03 +01:00
NonnullRefPtrVector.h AK: Make NonnullRefPtrVector constructible from Vector<NonnullRefPtr> 2021-06-11 00:30:09 +02:00
NumberFormat.h AK: Added human_readable_time() method for "unsaved changes" dialogs 2022-01-09 20:25:48 -08:00
NumericLimits.h AK: Use proper type for bool NumericLimits::min and max specialization 2021-11-14 22:52:35 +01:00
Optional.h AK: Make Optional<T> explicitly constructible from Optional<U> 2022-01-23 18:53:42 +02:00
OwnPtr.h AK: Convert the try_make<T> factory function to use ErrorOr 2022-02-03 23:33:20 +01:00
Platform.h AK+Everywhere: Replace __builtin bit functions 2021-12-21 22:13:51 +01:00
PrintfImplementation.h AK: Allow printing wide characters using %ls modifier 2022-03-30 11:30:43 +04:30
Ptr32.h AK: Fix building Ptr32 on x86_64 2021-06-24 09:27:13 +02:00
Queue.h AK+Everywhere: Reduce the number of template parameters of IntrusiveList 2021-09-10 18:05:46 +03:00
QuickSort.h AK: Guarantee a maximum stack depth for dual_pivot_quick_sort 2021-04-28 21:38:48 +02:00
Random.cpp AK: Introduce get_random_uniform() 2021-05-14 22:24:02 +02:00
Random.h AK+LibCore: Standardize on AK_OS_MACOS instead of __APPLE__ 2021-09-12 18:31:10 +02:00
RecursionDecision.h AK: Add RecursionDecision 2021-08-07 15:21:58 +02:00
RedBlackTree.h AK: Clear minimum when removing last node of RedBlackTree 2022-02-10 14:09:39 +00:00
RefCounted.h AK+Kernel: Remove one_ref_left() footgun 2022-01-11 01:12:16 +01:00
RefCountForwarder.h AK: Add RefCountForwarder<T> 2021-12-09 21:28:52 +01:00
RefPtr.h Kernel: Convert try_make_ref_counted to use ErrorOr 2022-02-03 23:33:20 +01:00
Result.h AK: Allow to "get a result" from Result<void> 2021-11-28 13:33:51 -08:00
ReverseIterator.h AK: Implement wrapper for reverse range for loop 2022-03-09 17:16:28 +01:00
ScopedValueRollback.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
ScopeGuard.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
ScopeLogger.h AK: Store the 'extra' field of ScopeLogger as String 2021-06-29 00:07:19 +04:30
SIMD.h AK: Add char SIMD types 2021-07-22 23:33:21 +02:00
SIMDExtras.h LibGL+LibSoftGPU: Implement the stencil buffer 2022-01-17 12:49:00 +01:00
SIMDMath.h AK/SIMD: Suppress psabi warnings and add explanatory comment 2022-01-09 21:03:08 +01:00
Singleton.h AK: Yield while waiting for another thread to initialize a Singleton 2022-01-19 01:28:13 +01:00
SinglyLinkedList.h AK: Fix erroneous move operators for SinglyLinkedList 2022-01-19 00:13:56 +01:00
SinglyLinkedListWithCount.h Everywhere: Use bgianf@serenityos.org for my copyright attribution 2021-04-22 21:15:54 +02:00
SourceGenerator.h AK: Add appendln helper to SourceGenerator 2022-03-28 23:08:08 +02:00
SourceLocation.h AK: Display SourceLocation function name in color 2022-03-28 23:11:48 +02:00
Span.h AK: Add constructor to create Span from Array 2022-02-26 16:01:26 +01:00
Stack.h AK: Use unchecked_append in AK::Stack, as we always validate the size 2022-01-05 14:04:18 +01:00
StackInfo.cpp AK: Fix preprocessor OS check 2021-12-12 11:10:34 -08:00
StackInfo.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
Statistics.h AK: Add min and max functions to Statistics 2021-11-06 22:09:25 -07:00
StdLibExtraDetails.h AK: Add a 'OneOf' concept 2022-03-28 23:11:48 +02:00
StdLibExtras.h AK: Add is_power_of_2 helper 2022-01-29 17:41:06 +02:00
Stream.h LibIPC+IPCCompiler+AK: Make IPC value decoders return ErrorOr<void> 2021-11-28 23:14:19 +01:00
String.cpp AK: Add String::split_view(Function<bool(char)>) 2022-02-25 19:38:31 +01:00
String.h AK: Add a case insensitive of is_one_of to String[View] 2022-03-21 10:48:17 +01:00
StringBuilder.cpp AK: Add a try variant of StringBuilder::append_escaped_for_json 2022-02-27 20:37:57 +01:00
StringBuilder.h AK: Add a try variant of StringBuilder::append_escaped_for_json 2022-02-27 20:37:57 +01:00
StringHash.h AK: Define a traits helper for case-insensitive StringView hashing 2022-01-11 00:36:45 +01:00
StringImpl.cpp AK: Make CaseInsensitiveStringTraits allocation-free 2022-02-19 14:45:59 +01:00
StringImpl.h AK: Make CaseInsensitiveStringTraits allocation-free 2022-02-19 14:45:59 +01:00
StringUtils.cpp AK+Tests: Fix StringUtils::contains() being confused by repeating text 2022-03-18 23:51:56 +00:00
StringUtils.h AK: Add convert_to_uint_from_octal 2021-12-21 13:13:04 -08:00
StringView.cpp AK: Exclude StringView String APIs from the Kernel 2022-02-16 22:21:37 +01:00
StringView.h AK: Add a case insensitive of is_one_of to String[View] 2022-03-21 10:48:17 +01:00
TemporaryChange.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
Time.cpp AK: Add Time::from_ticks() 2022-02-28 20:09:37 +01:00
Time.h AK: Add Time::from_ticks() 2022-02-28 20:09:37 +01:00
Traits.h AK: Support using custom comparison operations for hash compatible keys 2022-01-29 23:01:23 +02:00
Trie.h AK+Kernel: Specialize Trie for NNOP<KString> and use it in UnveilNode 2022-02-16 22:21:37 +01:00
Try.h AK: Mark the error branch of the TRY() macro as unlikely 2022-01-16 02:01:23 +02:00
Tuple.h AK: Correct Tuple's constructor signatures 2021-08-02 17:22:50 +04:30
TypeCasts.h AK: Rename downcast<T> => verify_cast<T> 2021-06-24 19:57:01 +02:00
TypedTransfer.h AK: Fast path for single-element TypedTransfer::copy 2021-12-17 13:13:00 -08:00
TypeList.h Everywhere: "indexes" => "indices" 2021-04-29 22:23:52 +02:00
Types.h AK+Kernel: Suppress clang-tidy warnings from the cert-* category 2021-11-14 22:52:35 +01:00
UBSanitizer.h AK+Kernel+LibSanitizer: Store "ubsan-is-deadly" flag as Atomic<bool> 2022-02-03 16:11:26 +01:00
UFixedBigInt.h AK: Remove unused String.h include from UFixedBigInt.h 2022-03-27 18:54:56 +02:00
UnicodeUtils.h AK: Remove now-unused AK::UnicodeUtils methods 2022-01-18 15:13:25 +00:00
URL.cpp Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
URL.h AK: Convert AK::Format formatting helpers to returning ErrorOr<void> 2021-11-17 00:21:13 +01:00
URLParser.cpp Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
URLParser.h Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Userspace.h AK+Kernel: Remove implicit conversion from Userspace<T*> to FlatPtr 2021-11-16 00:13:22 +01:00
Utf8View.cpp AK: Inline all the trivial Utf8View functions 2021-09-18 19:54:24 +02:00
Utf8View.h AK: Make Utf8View constexpr-constructible 2022-01-17 14:46:07 +00:00
Utf16View.cpp AK: Add a Utf16View::code_unit_offset_of(Utf16CodePointIterator) helper 2022-01-31 21:05:04 +02:00
Utf16View.h AK: Add a Utf16View::code_unit_offset_of(Utf16CodePointIterator) helper 2022-01-31 21:05:04 +02:00
Utf32View.h AK: Add the at()/operator[]() getter to Utf32View 2021-07-18 21:10:55 +04:30
UUID.cpp AK: Return KString instead of String from encode_hex in the Kernel 2022-02-16 22:21:37 +01:00
UUID.h AK+Kernel: Return KString from UUID::to_string() in the Kernel 2022-02-16 22:21:37 +01:00
Variant.h AK: Conditionally disable a few variant ctors/assignments 2022-02-15 18:03:02 +02:00
Vector.h AK: Make Vector capable of holding forward-declared types 2022-03-28 23:11:48 +02:00
Weakable.h Everywhere: Switch from EnableIf to requires 2022-03-17 22:15:42 -07:00
WeakPtr.h Everywhere: Switch from EnableIf to requires 2022-03-17 22:15:42 -07:00