msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

[Performance]: RAR is 1.8x slower in MSBuild.exe compared to dotnet build

Open ladipro opened this issue 2 years ago • 1 comments

There's a significant difference between Framework and Core MSBuild in the time taken up by the ResolveAssemblyReference when building OrchardCore.

Details:

  • Upstream OrchardCore at commit 2804ec773.
  • MSBuild 17.9 candidate (commit 5fcddc790 but unlikely to make a difference)
  • Command lines (after doing a restore and build):
    • dotnet build --no-restore /bl
    • msbuild /m /bl

image

Expected output

  • Understand what makes MSBuild.exe slower. Are some of the optimizations introduced in 8.0 not kicking in?
  • Fix (or suggest a fix; depending on complexity) the perf gap.

ladipro avatar Jan 04 '24 11:01 ladipro

After conducting an investigation, a couple of weak spots were identified:

  • A slow function call within mscorlib.SystemIO - Microsoft.IO.Redist was utilized to address this performance gap https://github.com/dotnet/msbuild/pull/9814.
  • TaskItem.CopyMetadataTo is slower in msbuild.exe due to the underlying system.collections.immutable - can we do anything here @rainersigwald ? Update: it was discussed offline - one of the potential strategy is to rethink the way we set metadata here https://github.com/dotnet/msbuild/blob/main/src/Tasks/AssemblyDependency/ReferenceTable.cs#L2674

image

  • Invoking SystemState.FileExists in an executable is more costly due to underlying PInvoke operations. image

update Running on amd64 gives a noticeable boost! image

  • LogResults invocation is very different in msbuild.exe, it goes through CentralForwardingLogger.EventSource_AnyEventRaised image The idea that came to Rainer's mind is related to making logging async, because now it looks like a bottleneck in RAR. The reason why it is implemented this way for a single node invocation: https://github.com/dotnet/msbuild/blob/67f702b535a02be05a696376883bd9cd6d576e38/src/Build/BackEnd/BuildManager/BuildParameters.cs#L320-L324 https://github.com/dotnet/msbuild/blob/67f702b535a02be05a696376883bd9cd6d576e38/src/MSBuild/XMake.cs#L1428-L1433 After enabling MSBUILDLOGASYNC=1 we received an immediate perf boost! We have to decide on switching it by default.

image

The snapshot used for comparison: image

YuliiaKovalova avatar Feb 01 '24 12:02 YuliiaKovalova

The investigation is done and conclusions are described above. The potential work items is created for async logging + optimization for File traversal was applied.

YuliiaKovalova avatar Mar 25 '24 12:03 YuliiaKovalova