LibJS: Replace source map HashMap with sorted Vector

Bytecode source map entries are always added in order of increasing
bytecode offset, and lookups only happen during error handling (a cold
path). This makes a sorted vector with binary search a better fit than
a hash map.

This change reduces memory overhead and speeds up bytecode generation
by avoiding hash table operations during compilation. Lookups remain
fast via binary search, and since source_range_at() is only called
when generating stack traces, the O(log n) lookup is acceptable.
This commit is contained in:
Andreas Kling 2026-01-26 17:51:53 +01:00 committed by Andreas Kling
parent d488f9f12f
commit 81bee185e6
Notes: github-actions[bot] 2026-02-06 11:03:57 +00:00
4 changed files with 23 additions and 11 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/BinarySearch.h>
#include <LibJS/Bytecode/BasicBlock.h>
#include <LibJS/Bytecode/Executable.h>
#include <LibJS/Bytecode/Instruction.h>
@ -115,13 +116,19 @@ UnrealizedSourceRange Executable::source_range_at(size_t offset) const
return {};
auto it = InstructionStreamIterator(bytecode.span().slice(offset), this);
VERIFY(!it.at_end());
auto mapping = source_map.get(offset);
if (!mapping.has_value())
auto* entry = binary_search(source_map, offset, nullptr, [](size_t needle, SourceMapEntry const& entry) -> int {
if (needle < entry.bytecode_offset)
return -1;
if (needle > entry.bytecode_offset)
return 1;
return 0;
});
if (!entry)
return {};
return UnrealizedSourceRange {
.source_code = source_code,
.start_offset = mapping->source_start_offset,
.end_offset = mapping->source_end_offset,
.start_offset = entry->source_record.source_start_offset,
.end_offset = entry->source_record.source_end_offset,
};
}