Skip to content

Invoke vcvarsall only once before windows compilation - #4570

Merged
mattleibow merged 1 commit into
mono:mainfrom
mmitche:mmitche/test-netcore-public-windows-pool
Jul 30, 2026
Merged

mattleibow merged 1 commit into
mono:mainfrom
mmitche:mmitche/test-netcore-public-windows-pool

Conversation

@mmitche

@mmitche mmitche commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Initialize the architecture-specific Visual C++ environment once before invoking Ninja for Windows SkiaSharp native builds.

The corresponding Skia change has merged as mono/skia#306. It removes the redundant vcvarsall.bat prefix from every Windows GN compile, archive, and link edge. This PR updates SkiaSharp to that merged commit and invokes one direct command per architecture:

call vcvarsall.bat <architecture> <windows-sdk> -vcvars_ver=<toolset> && ninja ...

Architecture mappings:

  • x86: amd64_x86
  • x64: amd64
  • ARM64: amd64_arm64

The build remains on the original Azure Pipelines / windows-2022 pool with Ninja's default parallelism.

Results

Validated in Azure build 1532137:

Architecture Original libSkiaSharp One-time vcvars Improvement
x86 49:52 21:33.863 56.8% faster
x64 46:33 20:17.847 56.4% faster
ARM64 45:06 20:38.700 54.2% faster

For each architecture:

  • the clean build completed every Ninja edge and linked successfully;
  • dependency validation passed;
  • the Visual C++ environment was initialized exactly once;
  • no Ninja edge invoked vcvarsall.bat.

Additional validation

Skia remains independently buildable on Windows without running vcvarsall: GN emits absolute compiler/linker paths and explicit MSVC and Windows SDK include/library paths. Standalone clang-cl and MSVC compile/relink tests succeeded with INCLUDE, LIB, and VCINSTALLDIR unset.

The final merged-submodule integration is being validated in Azure build 1532412.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Try the packages from this PR

Warning

Do not run these scripts without first reviewing the code in this PR.

Step 1 — Download the packages

bash / macOS / Linux:

curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 4570

PowerShell / Windows:

iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 4570"

Step 2 — Add the local NuGet source

dotnet nuget add source ~/.skiasharp/hives/pr-4570/packages --name skiasharp-pr-4570
More options
Option Description
--successful-only / -SuccessfulOnly Only use successful builds
--force / -Force Overwrite previously downloaded packages
--list / -List List available artifacts without downloading
--build-id ID / -BuildId ID Download from a specific build

Or download manually from Azure Pipelines — look for the nuget artifact on the build for this PR.

Remove the source when you're done:

dotnet nuget remove source skiasharp-pr-4570

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@mmitche! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@mmitche mmitche changed the title Test Windows builds on NetCore-Public Test NetCore-Public and Ninja oversubscription Jul 28, 2026
@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Cold-cache timing results from Azure build 1531532 on NetCore-Public / windows.vs2022.amd64.open (Standard_D4a_v4) with Ninja -j8:

Architecture Baseline libSkiaSharp Test Change
x86 49:52 39:53 20.0% faster
x64 46:33 51:27 10.5% slower
ARM64 45:06 49:24 9.5% slower

All three jobs were explicit cache misses. Portable, SHA-verified Spectre library provisioning succeeded in 9-12 seconds. The pool move plus -j8 is therefore not a uniform improvement: x86 improved substantially, while x64 and ARM64 regressed. A follow-up should isolate pool size from Ninja oversubscription by comparing the same pool with Ninja defaults versus -j8.

@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Starting the isolation run requested after build 1531532: this revision keeps the same NetCore-Public pool and compatibility provisioning but removes explicit Ninja -j8, returning to Ninja's default parallelism. The resulting cold-cache build will separate pool-size effects from compiler oversubscription.

@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Default-Ninja isolation results from cold-cache build 1531647, compared with the same NetCore-Public pool using -j8 in build 1531532:

Architecture Ninja default -j8 Default vs -j8
x86 50:18 39:53 -j8 20.7% faster
x64 36:44 51:27 default 28.6% faster
ARM64 36:52 49:24 default 25.4% faster

Against the original Azure Pipelines baseline, default Ninja on NetCore-Public was essentially flat for x86 (50:18 vs 49:52), but improved x64 by 21.1% (36:44 vs 46:33) and ARM64 by 18.3% (36:52 vs 45:06).

Conclusion: hard-coding -j8 is not appropriate across architectures. Keep Ninja defaults for x64/ARM64; if desired, test an x86-only override separately. Local experiments are now measuring removal of the per-edge vcvarsall wrapper and then PCH.

@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Local follow-up results for the Windows native build:

  • Initialize MSVC once instead of per Ninja edge: clean x64 libSkiaSharp went from 22:22.780 to 3:14.746. The build retained all 1,639 Ninja edges, reduced vcvarsall.bat initialization from 1,582 calls to 0, linked successfully, and passed the existing dependency checks. This is an 85.5% reduction locally. The prototype captured vcvarsall x64 10.0.22621.0 -vcvars_ver=14.4 once, removed the toolchain's per-edge wrapper, and inherited the captured environment in Ninja.
  • Core-target PCH prototype: added a conservative Windows PCH to skia_source_set("core"). GN generated a 12.9 MB PCH and the expected /Yc, /Yu, and /FI flags. Two clean PCH runs were 3:08.492 and 3:08.244; two alternating clean no-PCH controls were 3:22.793 and 3:30.680. That is 3:08.368 vs 3:26.737 average, about 8.9% faster in the controlled samples. Compared with the earlier 3:14.746 no-wrapper baseline, the likely benefit is more conservatively ~3-9% because local machine load varies.

Conclusion: removing repeated vcvarsall setup is the primary optimization and should be pursued first. PCH is viable and beneficial, but secondary. Both changes are local prototypes only and are not included in this PR.

@mmitche mmitche changed the title Test NetCore-Public and Ninja oversubscription Initialize vcvars once for Windows native builds Jul 29, 2026
@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Reworked the draft in commit d806da6: restored the original Windows pool, removed the old pool/PCH/oversubscription prototype, and pushed the architecture-scoped one-time vcvars implementation. CI should now measure this change in isolation.

@mmitche mmitche changed the title Initialize vcvars once for Windows native builds PR efficiency tests Jul 29, 2026
@mattleibow

mattleibow commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Thanks so much for this, I am working on this to move all builds to use the netcore-public agents where possible: #4565

Looking forward to faster starting, and faster BUILDING things.

@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Formalized the experiment in commit 3333d6b28:

  • Skia fork commit 0156fbba1c now removes the per-edge vcvarsall wrapper unconditionally; there is no GN opt-out switch.
  • SkiaSharp no longer uses a PowerShell environment-capture helper.
  • Each architecture launches one direct cmd.exe /c chain: call vcvarsall.bat <arch> <sdk> ... && python ninja.py ....
  • A clean local x64 run completed all 1,639 edges, linked successfully, passed dependency checks, initialized the environment once, and generated no per-edge vcvarsall, cmd /c, or env_setup Ninja commands.

This remains confined to the existing draft PR for CI testing. No separate mono/skia PR has been opened pending confirmation.

@mmitche

mmitche commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Removed the custom ninjaRunner callback in commit 69729bc85. GenerateGnBuild now handles shared GN setup; Windows calls it and then directly runs the one-time vcvarsall && ninja chain. Local incremental validation completed successfully.

@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Original-pool CI result — build 1532137

The formalized one-time vcvarsall && ninja implementation passed all three Win32 jobs:

Architecture Original baseline One-time vcvars Improvement Environment initialization
x86 49:52 21:33.863 56.8% faster 1
x64 46:33 20:17.847 56.4% faster 1
ARM64 45:06 20:38.700 54.2% faster 1

Validation from the task logs:

  • x86 completed all 1,637 Ninja edges.
  • x64 completed all 1,639 Ninja edges.
  • ARM64 completed all 1,635 Ninja edges.
  • Each log contains exactly one Initializing the Visual C++ environment once message.
  • Each log contains zero vcvarsall.bat or [vcvarsall.bat] Environment initialized messages from Ninja edges.
  • All dependency checks and final links succeeded.

This confirms the repeated per-edge Visual Studio setup was responsible for roughly half of the original Windows native build duration. No mono/skia PR has been opened pending confirmation.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Artifact size report

Packages from this PR (build 1532137) vs the latest nightly baseline 4.151.0-nightly.109 (observed 2026-07-29).

Total .nupkg size: 569.3 MB → 567.3 MB (−2.0 MB, -0.4%)

Packages

⚠️ marks growth over 500.0 KB or 2%. Changes under 50.0 KB are treated as noise.

Package baseline this PR Δ Δ%
SkiaSharp.NativeAssets.WinUI 109.0 MB 107.1 MB 🟢 −1.9 MB -1.7%
HarfBuzzSharp.NativeAssets.Win32 20.7 MB 20.5 MB 🟢 −199.9 KB -0.9%
SkiaSharp.NativeAssets.WebAssembly 69.6 MB 69.7 MB 🔴 +78.6 KB +0.1%

+37 package(s) unchanged (< 50.0 KB).

Per-file changes

HarfBuzzSharp.NativeAssets.Win32

File Size
runtimes/win-x64/native/libHarfBuzzSharp.pdb 22.0 MB → 21.8 MB (🟢 −240.0 KB)
runtimes/win-arm64/native/libHarfBuzzSharp.pdb 22.3 MB → 22.3 MB (🟢 −8.0 KB)
runtimes/win-x86/native/libHarfBuzzSharp.pdb 22.0 MB → 21.9 MB (🟢 −8.0 KB)

SkiaSharp.NativeAssets.WinUI

File Size
runtimes/win-x64/native/libGLESv2.pdb 66.7 MB → 66.4 MB (🟢 −256.0 KB)
runtimes/win-arm64/native/libGLESv2.pdb 64.5 MB → 64.3 MB (🟢 −208.0 KB)
runtimes/win-x86/native/libGLESv2.pdb 67.2 MB → 67.0 MB (🟢 −208.0 KB)
runtimes/win-x64/native/libEGL.pdb 6.4 MB → 6.3 MB (🟢 −32.0 KB)
runtimes/win-x86/native/libEGL.pdb 6.5 MB → 6.5 MB (🟢 −16.0 KB)
runtimes/win-arm64/native/SkiaSharp.Views.WinUI.Native.pdb 67.7 MB → 67.7 MB (🟢 −8.0 KB)
runtimes/win-x64/native/SkiaSharp.Views.WinUI.Native.pdb 68.0 MB → 68.0 MB (🟢 −8.0 KB)
🧬 win-x64 (runtimes/win-x64/native/libGLESv2.dll) 4.9 MB → 4.9 MB (🔴 +2.0 KB)
🧬 win-arm64 (runtimes/win-arm64/native/libGLESv2.dll) 4.4 MB → 4.4 MB (🔴 +1.0 KB)

Informational only — this never blocks the PR. Native binaries are labelled by os/arch.

@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

The corresponding Skia change is now open as mono/skia#306: mono/skia#306. This PR will move its submodule back to mono/skia after that change merges.

@mattleibow

Copy link
Copy Markdown
Contributor

@mmitche I merged the skia PR, you can update the submodule

@mattleibow mattleibow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM + the one submodule thing.

Comment thread .gitmodules Outdated
[submodule "externals/skia"]
path = externals/skia
url = https://github.com/mono/skia.git
url = https://github.com/mmitche/skia.git

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need to revert this.

@mmitche

mmitche commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

mono/skia#306 has merged. Commit 0027ea8d0 restores the canonical mono/skia submodule URL and updates the gitlink to merge commit a0dd072787. The SkiaSharp PR now contains only the caller-side one-time vcvarsall && ninja integration.

@mmitche
mmitche marked this pull request as ready for review July 29, 2026 23:48
Update to the merged Skia toolchain optimization and initialize the architecture-specific Visual C++ environment once before invoking Ninja.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: eff0dd62-bdd5-40c3-89ea-7edf50c82b76
@mmitche
mmitche force-pushed the mmitche/test-netcore-public-windows-pool branch from 0027ea8 to db99544 Compare July 29, 2026 23:50
@mmitche mmitche changed the title PR efficiency tests Invoke vcvarsall only once before windows compilation Jul 30, 2026
@mattleibow
mattleibow merged commit 7c23c53 into mono:main Jul 30, 2026
6 of 64 checks passed
@github-project-automation github-project-automation Bot moved this from Approved to Done in SkiaSharp Backlog Jul 30, 2026
@mmitche

mmitche commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Final validation passed on the squashed one-commit PR (db995447b). Azure build 1532418 completed the Win32 x86, x64, and ARM64 jobs successfully using merged mono/skia commit a0dd072787.

mattleibow added a commit that referenced this pull request Jul 30, 2026
[Windows] Stop pinning a Windows SDK version by default (#4588)

Context: #4588
Context: 7c23c53
Follow-up: #4589

The benchmark-source (windows-latest, Windows, native/windows) leg of
Track - Benchmarks died the instant it began the native build:

    Done. Made 114 targets from 63 files in 358ms
    Initializing the Visual C++ environment once for amd64.
    An error occurred when executing task 'libSkiaSharp'.
    Error: Process 'cmd.exe' failed with error: 1

RunNinjaWithVcVars invoked vcvarsall.bat with the default of the
windowsSdkVersion argument, 10.0.22621.0, which matches the AzDO agents.
The hosted runner is windows-2025-vs2026 and ships only Windows SDK
10.0.26100.0, and vcvarsall hard-fails on an SDK that is not installed:

    [ERROR:winsdk.bat] Windows SDK 10.0.22621.0 :
    'C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um'
    not found or was incomplete

Nothing needed that default. Before the MSVC environment was initialized
explicitly there was no such argument at all: gn resolved win_sdk_version
itself and the toolchain interpolated that resolved value into the
vcvarsall command it generated, so the version was always one that
existed on the machine and the two could never disagree. Hardcoding a
constant only meant the build broke wherever that exact SDK was absent -
this runner today, and equally a developer box carrying just a newer one.

Default the argument to empty and omit it from both gn and vcvarsall when
unset, so each detects an SDK that is present. Setting it still pins both
together, so --windowsSdkVersion remains available to build against a
specific SDK.

  * drop the `>nul` that hid the failure. It suppressed the vcvarsall
    banner, but `>nul` redirects only stdout and VsDevCmd writes its
    errors there too, so it discarded the diagnosis along with the
    banner - rerunning with `2>nul` still prints the [ERROR:winsdk.bat]
    lines, while `>nul` yields nothing but the exit code. The banner is
    five lines per architecture and worth keeping in the log.
  * this went unnoticed because benchmark-source runs on pull requests
    only and is skipped on main, so #4570 merged green without the path
    ever being exercised.
  * whether the vcvarsall call is needed at all is left open in #4589:
    every tool is invoked by absolute path and gn emits the SDK include
    and lib directories as explicit -imsvc and /LIBPATH: flags, so it may
    be redundant for our LLVM-based configuration.

Co-authored-by: Matthew Leibowitz <[email protected]>
Co-authored-by: Copilot App <[email protected]>
mattleibow added a commit that referenced this pull request Aug 4, 2026
[Windows] Initialize the MSVC environment once per native build (#4570)

Context: #4570
Skia change: mono/skia#306

Update the Skia submodule to remove per-edge vcvarsall invocation, then
initialize the architecture-specific Visual C++ environment once before
launching Ninja. Split GN generation from Ninja execution so Windows can
capture the required MSVC and Windows SDK environment without changing
other native build paths.

Running vcvarsall for every compile, archive, and link edge added
substantial process overhead. Clean hosted-agent builds for x86, x64,
and ARM64 completed successfully with one initialization per job and
reduced native build times by 54–57%.

Co-authored-by: Matt Mitchell (.NET) <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
Co-authored-by: Alexander Köplinger <[email protected]>
Co-authored-by: Copilot <[email protected]>
mattleibow added a commit that referenced this pull request Aug 4, 2026
[Windows] Stop pinning a Windows SDK version by default (#4588)

Context: #4588
Context: 7c23c53
Follow-up: #4589

The benchmark-source (windows-latest, Windows, native/windows) leg of
Track - Benchmarks died the instant it began the native build:

    Done. Made 114 targets from 63 files in 358ms
    Initializing the Visual C++ environment once for amd64.
    An error occurred when executing task 'libSkiaSharp'.
    Error: Process 'cmd.exe' failed with error: 1

RunNinjaWithVcVars invoked vcvarsall.bat with the default of the
windowsSdkVersion argument, 10.0.22621.0, which matches the AzDO agents.
The hosted runner is windows-2025-vs2026 and ships only Windows SDK
10.0.26100.0, and vcvarsall hard-fails on an SDK that is not installed:

    [ERROR:winsdk.bat] Windows SDK 10.0.22621.0 :
    'C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um'
    not found or was incomplete

Nothing needed that default. Before the MSVC environment was initialized
explicitly there was no such argument at all: gn resolved win_sdk_version
itself and the toolchain interpolated that resolved value into the
vcvarsall command it generated, so the version was always one that
existed on the machine and the two could never disagree. Hardcoding a
constant only meant the build broke wherever that exact SDK was absent -
this runner today, and equally a developer box carrying just a newer one.

Default the argument to empty and omit it from both gn and vcvarsall when
unset, so each detects an SDK that is present. Setting it still pins both
together, so --windowsSdkVersion remains available to build against a
specific SDK.

  * drop the `>nul` that hid the failure. It suppressed the vcvarsall
    banner, but `>nul` redirects only stdout and VsDevCmd writes its
    errors there too, so it discarded the diagnosis along with the
    banner - rerunning with `2>nul` still prints the [ERROR:winsdk.bat]
    lines, while `>nul` yields nothing but the exit code. The banner is
    five lines per architecture and worth keeping in the log.
  * this went unnoticed because benchmark-source runs on pull requests
    only and is skipped on main, so #4570 merged green without the path
    ever being exercised.
  * whether the vcvarsall call is needed at all is left open in #4589:
    every tool is invoked by absolute path and gn emits the SDK include
    and lib directories as explicit -imsvc and /LIBPATH: flags, so it may
    be redundant for our LLVM-based configuration.

Co-authored-by: Matthew Leibowitz <[email protected]>
Co-authored-by: Copilot App <[email protected]>
@mattleibow mattleibow added this to the 4.152.0-preview.1 milestone Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants