-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged PR 680832: [BuildXL Perf] Reduce the size of EnvironmentVariab…
…le struct from 48 to 32 An array of `EnvironmentVariable` structs is created per pip and such arrays occupy quite a bit of memory. Here is an example from bxl dump for COSINE: ``` 305412 2828539152 BuildXL.Pips.Operations.EnvironmentVariable[] ``` I.e. the arrays are almost 3Gb. This PR reduces the size of `EnvironmentVariable` struct from 48 to 32 bytes by flattening types used in it. It also adds a reference to `ObjectLayoutInspector` nuget package that allows inspecting the layout at runtime. The layout before:  Here is the layout after: 
- Loading branch information
1 parent
2058595
commit dbfc427
Showing
8 changed files
with
161 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Public/Src/Pips/UnitTests/Pips/EnvironmentVariableLayoutTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using BuildXL.Pips.Operations; | ||
using BuildXL.Utilities; | ||
using Test.BuildXL.TestUtilities.Xunit; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Test.BuildXL.Pips | ||
{ | ||
public sealed class EnvironmentVariableLayoutTests : XunitBuildXLTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
public EnvironmentVariableLayoutTests(ITestOutputHelper output) | ||
: base(output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void PipDataStoredCorrectly() | ||
{ | ||
// EnvironmentVariable flattens PipData structure, so its important to make sure | ||
// that if PipData structure changes EnvironmentVariable is changed as well. | ||
// This test ensures the consistency. | ||
var pipData = PipData.Invalid; | ||
var envVar = new EnvironmentVariable(StringId.UnsafeCreateFrom(42), pipData, isPassThrough: true); | ||
Assert.Equal(pipData, envVar.Value); | ||
|
||
var pipDataEntry = new PipDataEntry(PipDataFragmentEscaping.NoEscaping, PipDataEntryType.NestedDataHeader, 42); | ||
pipData = PipData.CreateInternal( | ||
pipDataEntry, | ||
PipDataEntryList.FromEntries(new[] {pipDataEntry}), | ||
StringId.UnsafeCreateFrom(1)); | ||
envVar = new EnvironmentVariable(StringId.UnsafeCreateFrom(42), pipData, isPassThrough: false); | ||
Assert.Equal(pipData, envVar.Value); | ||
} | ||
|
||
[Fact] | ||
public void EnvironmentVariableSizeIs32() | ||
{ | ||
// EnvironmentVariable structs are stored for every pip and reducing the size of such structs | ||
// reasonably reduces the peak memory consumption. | ||
|
||
// Flattening the layout saves 30% of space compared to the naive old version. | ||
var layout = ObjectLayoutInspector.TypeLayout.GetLayout<EnvironmentVariable>(); | ||
_output.WriteLine(layout.ToString()); | ||
|
||
Assert.Equal(32, layout.Size); | ||
var oldLayout = ObjectLayoutInspector.TypeLayout.GetLayout<OldEnvironmentVariable>(); | ||
_output.WriteLine(oldLayout.ToString()); | ||
Assert.True(layout.Size < oldLayout.Size); | ||
} | ||
|
||
// Using StringIdStable and not StringId, because StringId layout is different for debug builds. | ||
private record struct OldEnvironmentVariable(StringIdStable Name, PipData Value, bool IsPassThrough); | ||
|
||
private record struct StringIdStable(int Value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters