# WindowsSdk: Use ntdll instead of advapi32 APIs for registry querying · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/70058471e341870dc6991cf9cb92a590a94912c2)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 70058471e341870dc6991cf9cb92a590a94912c2

Requested commit: 70058471e341870dc6991cf9cb92a590a94912c2

Commit: 70058471e341870dc6991cf9cb92a590a94912c2

Tree: 8b608fd7f028d8008f6b914b8420842c3c9971b2

Author: Ryan Liptak

Committer: Ryan Liptak

## Message

```
WindowsSdk: Use ntdll instead of advapi32 APIs for registry querying

The dependency on advapi32.dll actually silently brings along 3 other dlls at runtime (msvcrt.dll, sechost.dll, bcrypt.dll), even if no advapi32 APIs are called. So, this commit actually reduces the number of dlls loaded at runtime by 4 (but only when LLVM is not linked, since LLVM has its own dependency on advapi32.dll).

The data is not super conclusive, but the ntdll version of WindowsSdk appears to run slightly faster than the previous advapi32 version:

Benchmark 1: libc-ntdll.exe ..
  Time (mean ± σ):       6.0 ms ±   0.6 ms    [User: 3.9 ms, System: 7.1 ms]
  Range (min … max):     4.8 ms …   7.9 ms    112 runs

Benchmark 2: libc-advapi32.exe ..
  Time (mean ± σ):       7.2 ms ±   0.5 ms    [User: 5.4 ms, System: 9.2 ms]
  Range (min … max):     6.1 ms …   8.9 ms    103 runs

Summary
  'libc-ntdll.exe ..' ran
    1.21 ± 0.15 times faster than 'libc-advapi32.exe ..'

and this mostly seems to be due to changes in the implementation (the advapi32 APIs do a lot of NtQueryKey calls that the new implementation doesn't do) rather than due to the decrease in dll loading. LLVM-less zig binaries don't show the same reduction (the only difference here is the DLLs being loaded):

Benchmark 1: stage4-ntdll\bin\zig.exe version
  Time (mean ± σ):       3.0 ms ±   0.6 ms    [User: 5.3 ms, System: 4.8 ms]
  Range (min … max):     1.3 ms …   4.2 ms    112 runs

Benchmark 2: stage4-advapi32\bin\zig.exe version
  Time (mean ± σ):       3.5 ms ±   0.6 ms    [User: 6.9 ms, System: 5.5 ms]
  Range (min … max):     2.5 ms …   5.9 ms    111 runs

Summary
  'stage4-ntdll\bin\zig.exe version' ran
    1.16 ± 0.28 times faster than 'stage4-advapi32\bin\zig.exe version'

---

With the removal of the advapi32 dependency, the non-ntdll dependencies that remain in an LLVM-less Zig binary are ws2_32.dll (which brings along rpcrt4.dll at runtime), kernel32.dll (which brings along kernelbase.dll at runtime), and crypt32.dll (which brings along ucrtbase.dll at runtime).

```

## Parents

- [421c3c3cc57f05bf53678ee79230c66200a69081](https://git.cafe/gitcafe/zig/commit/421c3c3cc57f05bf53678ee79230c66200a69081?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/70058471e341870dc6991cf9cb92a590a94912c2?format=markdown)
