msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

Send task-host build process environment as delta

Open OvesN opened this issue 5 months ago • 0 comments

Fixes #14097

Context

Out-of-proc task hosts re-send the same build-invariant data in every TaskHostConfiguration (and echo the environment back in every TaskHostTaskComplete). Two payloads dominate this redundancy on large -mt builds:

  1. The build process environment (~6 KB), shipped forward in every config and back in every result.
  2. On solution builds, the CurrentSolutionConfigurationContents global property — an XML blob describing every project's configuration (~57 KB on OrchardCore) — shipped forward in every config. It is ~99% of the global-properties payload and ~48% of all task-host config bytes.

Both are invariant for the vast majority of tasks, yet re-transmitted in full to connections that already received them.

Changes Made

  • Send the environment as a delta. Under a negotiated wire format (PacketVersion 5) the environment is sent in full only once per task-host connection; subsequent packets whose environment is unchanged carry a 1-byte "identical" marker instead of the full dictionary, on both the forward (config) and return (result) paths. The receiver reconstructs it from the last full environment seen on that connection.

  • Deduplicate the solution configuration blob. Solution (.sln/.slnx) builds inject CurrentSolutionConfigurationContents as a global property on every project. It is build-invariant (it describes the solution, not the project), but it is re-serialized into every TaskHostConfiguration. It cannot simply be dropped — a task may read it at runtime via IBuildEngine6.GetGlobalProperties() — so the same per-connection delta is applied (PacketVersion 65: the value is carried in its own field, excluded from the global-properties dictionary on the wire (so the large blob is never serialized twice), preceded by a 1-byte Full/Identical marker. It is sent once per connection, then only a marker; the task host reconstructs it from the connection baseline.

  • Skip redundant per-task environment apply/restore in the task host when the next task's environment is unchanged from the previous one.

  • Bug fixes:

    • Self-clear: feeding the reused (unchanged) environment back into SetEnvironment cleared it, because in -mt it was the driver's own backing dictionary. Fixed with a reference-equality guard.

Testing

  • Round-trip serialization tests for the new Full/Identical wire forms: environment and CurrentSolutionConfigurationContents at PacketVersion 5
  • -mt end-to-end regression tests: environment observed across consecutive task-host tasks, and an environment change made by one task-host task is observed by the next (verified failing before the aliasing fix, passing after).
  • Validated end-to-end: full build incl. net35, and OrchardCore.slnx -mt Rebuild succeeds.
  • Existing task-host test suites pass.

Measurements

Workload: OrchardCore (OrchardCore.slnx), -t:Rebuild -mt — 17,975 task-host task invocations. Serialized payload sizes measured with ThProfile (write-side stream-position deltas; deterministic, single run). Baseline = upstream main; patched = this PR.

Task-host IPC payload Baseline This PR Saved
Build process environment (forward + return) 122.0 MB 0.1 MB ≈ 122 MB
CurrentSolutionConfigurationContents (forward) 647.4 MB 1.8 MB ≈ 646 MB
Total task-host IPC (config + result) ≈ 1,548 MB ≈ 764 MB ≈ 785 MB (−51%)

Per direction:

Field Baseline This PR Reduction
TaskHostConfiguration total (forward) 1,342 MB 620 MB −54%
  ↳ environment 61.0 MB 0.1 MB −99.8%
  ↳ CurrentSolutionConfigurationContents 647.4 MB 1.8 MB −99.7%
  ↳ global properties (incl. the blob) 653.6 MB 7.9 MB −98.8%
TaskHostTaskComplete total (return) 206.4 MB 143.3 MB −31% (all from the environment)

The environment was ~4.6% of every config packet and ~29.7% of every result packet; CurrentSolutionConfigurationContents was ~48% of every config packet. After the change each carries only a 1-byte "unchanged" marker once the connection baseline is established. The remaining config payload is dominated by per-task task parameters, which are not invariant and so are not deduplicated.

Wall-clock / overhead

Sending the environment as a delta, deduplicating the solution configuration, and skipping the redundant per-task environment apply/restore did not measurably change total wall-clock time or task-host overhead in my measurements — the IPC savings are below the run-to-run noise floor on my machine. This is an IPC-volume optimization (fewer bytes serialized/transmitted/deserialized), not a critical-path one.

I measured TaskHost overhead on OrchardCore after restore on my machine three times and took the average, using this guide to compute task host overhead. (Note: in this guide all MSBuildMultiThreadableTask parameters were deliberately deleted; I kept them, of course.)

The measurement showed no significant improvement on total time for task and overhead: Baseline: baseline Patched version: patched

Metric Before → After Delta Surface reading
Total task-host time 871,916 → 1,008,858 ms +136,942 ms (+15.7%) looks like regression
Overhead share 58.6% → 54.4% −4.2 pp (−7.2% rel.) looks like improvement

Both deltas are likely noise (the two metrics point in opposite directions, and execute_ms — task work this change cannot affect — moved by a similar magnitude, indicating machine-load drift rather than a real effect).

OvesN avatar Jun 22 '26 08:06 OvesN