Skip to content

trace2: enumerate Windows processes once for ancestry - #6552

Open
tyrielv wants to merge 1 commit into
git-for-windows:mainfrom
tyrielv:trace2-single-pass-ancestry
Open

tyrielv wants to merge 1 commit into
git-for-windows:mainfrom
tyrielv:trace2-single-pass-ancestry

Conversation

@tyrielv

@tyrielv tyrielv commented Nov 9, 2026

Copy link
Copy Markdown

Problem and Context

When Trace2 is active on Windows, Git creates a system-wide process snapshot and searches it again for each ancestor.
Repeated Process32First and Process32Next calls increase the synchronous startup cost of short Git commands.

Changes

  • Enumerate the snapshot once into a PID-keyed hashmap, then follow parent IDs in memory.
  • Preserve ancestor order, missing-parent behavior, cycle detection, and the truncation limit.
  • Release the snapshot before the parent walk and free all temporary entries after the walk.
  • Add unit tests for missing entries, unordered entries, bucket collisions, rehashes, high PIDs, cycles, and truncation through an internal helper.
  • Add a functional test that checks the parent chain and compares the legacy windows/ancestry and cmd_ancestry arrays.
  • Add a performance test for git version with Trace2 disabled and active against NUL, registered in Make and Meson.

Performance

Matched local x64 builds at 581291682ad use identical compiler flags, version strings, and configuration on an ARM64 Windows host.
Each cell below uses 80 randomized, interleaved samples.

Command / target Before After
git --version, Trace2 off 55.1 ms 55.1 ms
git --version, active Trace2 to NUL 112.2 ms 85.4 ms
git rev-parse --git-dir, active Trace2 to NUL 114.7 ms 90.3 ms

Local-file targets show comparable improvements.
The ancestry interval falls from 35.4 ms to 13.0 ms across 20 paired traces with identical ancestor arrays.
The system snapshot and event writes remain synchronous, so this change does not remove all Trace2 overhead.
Temporary memory scales with the snapshot process count.

Trace2 collects Windows process ancestry synchronously before command
dispatch. Each ancestor lookup restarts Process32First/Process32Next on
the same snapshot, so a deep process tree repeats the enumeration work.

Enumerate the snapshot once into a PID-keyed hashmap and follow parent
IDs through that map. Preserve the existing ancestor order, missing-parent
handling, cycle marker, and truncation limit. Release the snapshot before
the in-memory walk and free the map after the walk.
Temporary memory scales with process count. Snapshot creation and event
writes remain synchronous.

Give the internal walk an explicit start PID for deterministic Windows
unit tests. Add unit tests for missing entries, unordered entries,
colliding buckets, rehashes, high PIDs, cycles, and truncation.
Add a functional test that checks the parent chain and compares the
legacy and cmd_ancestry arrays.

Add a performance test for git version with Trace2 disabled and enabled.
On an ARM64 Windows host, matched local x64 builds at 5812916 show
median git --version wall time of 112.2 ms before and 85.4 ms after, with
Trace2 active against NUL. Both builds use identical flags, version
strings, and configuration. Each result uses 80 interleaved samples.
The Trace2-disabled medians remain 55.1 ms.

Signed-off-by: Tyrie Vella <tyrielv@gmail.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Assisted-by: GPT-6.1 Sol
@tyrielv
tyrielv marked this pull request as ready for review November 10, 2026 00:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant