runtime: unified loader/OS perf pack (console paths, per-DLL import cache, SHA-NI, lock-free registry) #115

Open
github-bot wants to merge 5 commits from github-bot/gh-115 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 02:21 UTC · original
ubiquitarianism:old-pr/9849 → main · +1792 −109 · 12 files · 5 commits · draft

Carried over from old PR 9849 by TG0000 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9849, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
TG0000: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Consolidated C++ runtime startup, path-parsing, SHA-256, and lock-free unimplemented-import performance pack replacing closed PRs #8045, #8340, #8358, and #8439 (all previously independently verified on Windows MSVC, Linux GCC + ASan/UBSan, and real executable loading).

What this combines

  1. Zero-copy console path parsing (was #8045): cuts allocations (7 -> 1 on G:, 11 -> 3 on T:) and -84% CPU; preserves win32_io_path() long-path handling on main.
  2. Per-DLL import probe & host DLL load cache (was #8340): probes game dir and loads host DLL once per imported DLL instead of once per import (~36x faster, 725 -> 12 exists/LoadLibraryExA calls), caching only successful non-null probes (rt::Lazy::get_if_truthy) so failed probes stay retryable; preserves resolve_compat on main.
  3. In-place unrolled sha256_hex with x86 SHA-NI (was #8358): 4.6x–7.0x faster (237 -> 33.9 us on 64 KiB) with portable fallback.
  4. Lock-free repeat unimplemented import calls (was #8439): no mutex or map lookup on repeat calls (51x–81x faster).

Testing

  • Compiled with clang++ (-std=c++20 -O2 -Wall -Wextra -Werror, 0 new warnings) on Windows 11 (Ryzen 9 5900X):
    • CTest (-DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF): shader_library, sha256_hex (x86 SHA extensions path), and unimplemented_import (81.0x speedup) — 3/3 pass.
    • pe_loader_import_cache_test --reps 3: ok (2,000 random tables + dynamic retry case, 36.47x speedup).
    • file_system_parse_test: 272/272 checks pass.
  • git diff --check and scripts/check_no_game_data.py clean. No game data used.

AI-assisted (Claude Opus 5.5).

**@⁠Andretuta** on GitHub · 2026-11-24 02:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/115) `ubiquitarianism:old-pr/9849` → `main` · +1792 −109 · 12 files · 5 commits · draft > Carried over from **old PR 9849** by `TG0000` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9849`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `TG0000`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Consolidated C++ runtime startup, path-parsing, SHA-256, and lock-free unimplemented-import performance pack replacing closed PRs `#8045`, `#8340`, `#8358`, and `#8439` (all previously independently verified on Windows MSVC, Linux GCC + ASan/UBSan, and real executable loading). ### What this combines 1. **Zero-copy console path parsing** (was `#8045`): cuts allocations (`7 -> 1` on `G:`, `11 -> 3` on `T:`) and `-84%` CPU; preserves `win32_io_path()` long-path handling on `main`. 2. **Per-DLL import probe & host DLL load cache** (was `#8340`): probes game dir and loads host DLL once per imported DLL instead of once per import (`~36x` faster, `725 -> 12` `exists`/`LoadLibraryExA` calls), caching only successful non-null probes (`rt::Lazy::get_if_truthy`) so failed probes stay retryable; preserves `resolve_compat` on `main`. 3. **In-place unrolled `sha256_hex` with x86 SHA-NI** (was `#8358`): `4.6x–7.0x` faster (`237 -> 33.9 us` on 64 KiB) with portable fallback. 4. **Lock-free repeat unimplemented import calls** (was `#8439`): no mutex or map lookup on repeat calls (`51x–81x` faster). ### Testing - Compiled with clang++ (`-std=c++20 -O2 -Wall -Wextra -Werror`, 0 new warnings) on Windows 11 (Ryzen 9 5900X): - CTest (`-DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF`): `shader_library`, `sha256_hex` (x86 SHA extensions path), and `unimplemented_import` (81.0x speedup) — 3/3 pass. - `pe_loader_import_cache_test --reps 3`: ok (2,000 random tables + dynamic retry case, 36.47x speedup). - `file_system_parse_test`: 272/272 checks pass. - `git diff --check` and `scripts/check_no_game_data.py` clean. No game data used. AI-assisted (Claude Opus 5.5).
console_drive compared the GAME: prefix through a lowered substring, and for G:/T: paths copied both roots out of
the lock and lowered copies of the path and each root in lies_under; parse_console grew each component a character
at a time and copied it into parts; and every T: path ran std::filesystem::create_directories on the scratch folder,
whose status opens a handle to it. Now the prefix and roots are compared in place (roots under g_mutex), components
are sliced from the path, and create_directories only runs when GetFileAttributesW does not report the folder.

file_system_parse_test checks the parse against the earlier code on edge cases and 200 000 generated paths.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Loader::resolve ran std::filesystem::exists(<game>/<dll>) and LoadLibraryExA for every import that the shim
table does not serve, though every import of a descriptor names the same DLL. bind_imports now installs a cache
(Loader::ImportCache) for the module it binds; resolve() keeps, per DLL name, the lowered key, prefer_host_copy,
the bundled module (or none) and the host handle (or failure), each worked out on the first import that needs it.
A kept answer is the one asking again gives: load() of a mapped module returns it from by_name_, and
LoadLibraryExA of a loaded DLL returns the same handle (only its reference count differs; the loader never frees
a host DLL). Bindings, addresses, ImportRecords and the RtlPcToFileHeader redirects are unchanged.

runtime/test/pe_loader_import_cache_test.cpp checks resolve before and after on fixed and 2000 random import
tables (same results and side effects) and times the calls removed: 725 System32 imports in 12 descriptors bind
about 30x faster (18 ms -> 0.56 ms on this machine).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Gemini 4 Neon <[email protected]>
Every pipeline key, each stage's program lookup and every cached-PSO blob in
d3d12_vulkan_device.cpp goes through leonida::sha256_hex. It copied the whole input
into a vector (then reallocated it on the padding push), padded it a byte at a
time, grew its 64-character result a character at a time, and compressed with a
plain scalar loop.

- Whole blocks are compressed where they lie. Only the tail and its padding (one
  or two blocks) go through a 128-byte stack buffer, so a hash allocates only its
  result (1 allocation instead of 2 to 7).
- The portable compression runs eight rounds per step with the working variables
  named in rotated order, keeps a 16-word rolling schedule, and uses the
  fewer-operation forms of Ch and Maj: 1.22-1.80x on its own.
- On x86-64 with GCC/Clang, a SHA256RNDS2/MSG1/MSG2 path runs when CPUID reports
  SHA, SSSE3 and SSE4.1. The answer is cached in a constant-initialized atomic,
  not a function-local static. LEONIDA_SHA256_X86=0 builds the portable path only.

The output is unchanged. runtime/test/sha256_hex_test.cpp checks the portable
path, the SHA-extensions path and sha256_hex against a verbatim copy of the old
function: FIPS 180-4 vectors including the million-'a' one, a null empty span,
every length 0..1100 at aligned and odd addresses, 300 random lengths up to
70 000 bytes, and constant fills around both padding cases. It also counts
allocations. `--bench` prints the timings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rt_unimplemented took the process-wide g_called_mutex and walked a
std::map<const char*, ReturnClass> on every call of every unimplemented
import, from every thread. The thunk now loads the address of a
per-import record (runtime/shims/unimplemented_import.h) into r10: the
return class is worked out once when the thunk is made, and an atomic
flag answers "first call" for exactly one caller. A repeat call is one
relaxed load; only an import's first call takes the mutex, to add it to
the list report_called_unimplemented prints.

runtime/test/unimplemented_import_test.cpp (CPU only) drives the old
bookkeeping and the new one with the same fixed, random and 8-thread
call sequences, checks every answer and the called sets match, counts
heap allocations and times a call.

Item: PERF-RT-UNIMPL-RECORD (#8344).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:44:58 +00:00
This pull request has changes conflicting with the target branch.
  • runtime/CMakeLists.txt
  • runtime/shims/file_system.cpp
  • runtime/src/pe_loader.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/115/head:github-bot/gh-115'
git switch 'github-bot/gh-115'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-115'
git switch 'github-bot/gh-115'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-115'
git switch 'github-bot/gh-115'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-115'
git switch 'main'
git merge --squash 'github-bot/gh-115'
git switch 'main'
git merge --ff-only 'github-bot/gh-115'
git switch 'main'
git merge 'github-bot/gh-115'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.