HLE: find an import's implementation by name, not by scanning every module #141

Closed
TG0000 wants to merge 1 commit from upstream-pr/141 into main
TG0000 commented 2026-11-24 00:54:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T00:54:52Z.


What

registry::lookup binds every title import we implement and serves GetProcAddress on host modules (host_override). For each call it walked all modules, and each module's serves lower-cased the DLL name into a new String before testing its patterns. Then it compared the name against every export of the serving modules. A miss paid the full cost, and every import we leave to the host is a miss.

The registry is now indexed once, by export name, in registry order, keeping the first export of a name in each module. A lookup:

  1. hashes the name with FNV-1a (the keys are our own export names, so the default hasher's resistance to chosen keys buys nothing);
  2. tests the DLL patterns of only the modules that export that name (nearly always one), case-insensitively and without allocating.

The result doesn't change: the first serving module in registry order wins, and within it the first export with that name. HleModule::serves and the other callers are untouched, and only crates/victory-hle/src/registry.rs changes.

Numbers

Release build, one core. The benchmark looks up every registry export under each DLL name that serves it (upper-cased, API sets with a version suffix), plus one host-only miss per DLL name. That's 605 lookups, best of 5, over 3 runs:

ns per import
scan (before) 444–489
index (after) 41–46
speedup 10.3x–11.8x

To run it: cargo test --release -p victory-hle registry_lookup_throughput -- --ignored --nocapture

Tests

  • The index matches the old scan for every DLL pattern (as written, upper-cased, versioned API sets, unserved names, empty and 200-byte names) crossed with every export name plus misses. This runs on the real registry and on overlapping synthetic modules (exact and prefix patterns serving the same DLL, one name exported by two modules, one name exported twice by a single module, a module with no DLLs).
  • matches agrees with HleModule::serves on mixed-case, non-ASCII, empty and prefix-boundary names.
  • Every registry pattern is lower case, which the case-insensitive match relies on.
  • 300 modules sharing one export name keep registry order.
  • An ignored throughput benchmark.

Checked with:

  • cargo test --workspace --exclude victory-gpu: all pass.
  • cargo clippy -p victory-hle --all-targets: no warnings in the touched file.
  • cargo xtask policy: OK.
  • cargo fmt --check on the file: clean.

victory-gpu's tests don't build on current main (9aef3d9): crates/victory-gpu/src/host.rs defines mod tests twice (lines 252 and 1974). This PR doesn't touch that crate, so I left it out of the workspace run.

Doesn't overlap any open PR.

This PR is AI-assisted (Claude). It was tested on Windows 11 with the MSVC stable toolchain, using unit tests and a synthetic release benchmark. No game data is involved and nothing needed a GPU.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572106883 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/141). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T00:54:52Z. --- ## What `registry::lookup` binds every title import we implement and serves `GetProcAddress` on host modules (`host_override`). For each call it walked all modules, and each module's `serves` lower-cased the DLL name into a new `String` before testing its patterns. Then it compared the name against every export of the serving modules. A miss paid the full cost, and every import we leave to the host is a miss. The registry is now indexed once, by export name, in registry order, keeping the first export of a name in each module. A lookup: 1. hashes the name with FNV-1a (the keys are our own export names, so the default hasher's resistance to chosen keys buys nothing); 2. tests the DLL patterns of only the modules that export that name (nearly always one), case-insensitively and without allocating. The result doesn't change: the first serving module in registry order wins, and within it the first export with that name. `HleModule::serves` and the other callers are untouched, and only `crates/victory-hle/src/registry.rs` changes. ## Numbers Release build, one core. The benchmark looks up every registry export under each DLL name that serves it (upper-cased, API sets with a version suffix), plus one host-only miss per DLL name. That's 605 lookups, best of 5, over 3 runs: | | ns per import | |---|---| | scan (before) | 444–489 | | index (after) | 41–46 | | speedup | **10.3x–11.8x** | To run it: `cargo test --release -p victory-hle registry_lookup_throughput -- --ignored --nocapture` ## Tests - The index matches the old scan for every DLL pattern (as written, upper-cased, versioned API sets, unserved names, empty and 200-byte names) crossed with every export name plus misses. This runs on the real registry and on overlapping synthetic modules (exact and prefix patterns serving the same DLL, one name exported by two modules, one name exported twice by a single module, a module with no DLLs). - `matches` agrees with `HleModule::serves` on mixed-case, non-ASCII, empty and prefix-boundary names. - Every registry pattern is lower case, which the case-insensitive match relies on. - 300 modules sharing one export name keep registry order. - An ignored throughput benchmark. Checked with: - `cargo test --workspace --exclude victory-gpu`: all pass. - `cargo clippy -p victory-hle --all-targets`: no warnings in the touched file. - `cargo xtask policy`: OK. - `cargo fmt --check` on the file: clean. `victory-gpu`'s tests don't build on current `main` (9aef3d9): `crates/victory-gpu/src/host.rs` defines `mod tests` twice (lines 252 and 1974). This PR doesn't touch that crate, so I left it out of the workspace run. Doesn't overlap any open PR. This PR is AI-assisted (Claude). It was tested on Windows 11 with the MSVC stable toolchain, using unit tests and a synthetic release benchmark. No game data is involved and nothing needed a GPU. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jeremygold02 commented 2026-11-24 06:55:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:33:46Z.


Tested head 2f6efc1933109d4aa5e6ad4320edb8bc63086683 with cargo test -p victory-hle --lib registry -- --test-threads=1: 7 passed, 0 failed, 1 ignored (the ignored case is a benchmark; 84 unrelated tests were filtered out). Coverage includes indexed lookup agreeing with a full scan, preserving module/export order, case-insensitive patterns, and first-serving-module precedence (registry.rs). This is unit coverage; title import resolution was not exercised in a runtime.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122132501 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/141#issuecomment-7122132501). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:33:46Z. --- Tested head `2f6efc1933109d4aa5e6ad4320edb8bc63086683` with `cargo test -p victory-hle --lib registry -- --test-threads=1`: **7 passed, 0 failed, 1 ignored** (the ignored case is a benchmark; 84 unrelated tests were filtered out). Coverage includes indexed lookup agreeing with a full scan, preserving module/export order, case-insensitive patterns, and first-serving-module precedence ([`registry.rs`](https://github.com/Marvisak/VIctory/blob/2f6efc1933109d4aa5e6ad4320edb8bc63086683/crates/victory-hle/src/registry.rs)). This is unit coverage; title import resolution was not exercised in a runtime.
upstream-sync closed this pull request 2026-11-24 09:05:01 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!141
No description provided.