Skip to content

Commit 0427e85

Browse files
authored
asking terminal for dimensions during every frame is expensive (#11504)
Part of #11160 ### Context While profiling some of Eric's PRs and one of my experiments, I've noticed following: <img width="953" alt="terminall_loger_get_width_height_cost" src="https://github.com/user-attachments/assets/bb3478c3-e669-4911-b6b0-0c834e38305e" /> Terminal width/height are behind a lock and repeated access is quite expensive. ### Changes Made I've set it so that the update is happening only once every ~second or so. This makes the cost negligible. Note that this appears to be ~1.5% CPU time saved on the main node, which is the one under heaviest load due to IPC with all the other nodes. ### Testing None, this should be non-disruptive change. ### Notes Refresh frequency is up to discussion. Making it twice a second should be fine as well.
2 parents dd2f7ad + 20a1cfe commit 0427e85

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/Build/Logging/TerminalLogger/TerminalLogger.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1058,11 +1058,22 @@ private void ErrorRaised(object sender, BuildErrorEventArgs e)
10581058
private void ThreadProc()
10591059
{
10601060
// 1_000 / 30 is a poor approx of 30Hz
1061+
var count = 0;
10611062
while (!_cts.Token.WaitHandle.WaitOne(1_000 / 30))
10621063
{
1064+
count++;
10631065
lock (_lock)
10641066
{
1065-
DisplayNodes();
1067+
// Querying the terminal for it's dimensions is expensive, so we only do it every 30 frames e.g. once a second.
1068+
if (count >= 30)
1069+
{
1070+
count = 0;
1071+
DisplayNodes();
1072+
}
1073+
else
1074+
{
1075+
DisplayNodes(false);
1076+
}
10661077
}
10671078
}
10681079

@@ -1073,9 +1084,11 @@ private void ThreadProc()
10731084
/// Render Nodes section.
10741085
/// It shows what all build nodes do.
10751086
/// </summary>
1076-
internal void DisplayNodes()
1087+
internal void DisplayNodes(bool updateSize = true)
10771088
{
1078-
TerminalNodesFrame newFrame = new TerminalNodesFrame(_nodes, width: Terminal.Width, height: Terminal.Height);
1089+
var width = updateSize ? Terminal.Width : _currentFrame.Width;
1090+
var height = updateSize ? Terminal.Height : _currentFrame.Height;
1091+
TerminalNodesFrame newFrame = new TerminalNodesFrame(_nodes, width: width, height: height);
10791092

10801093
// Do not render delta but clear everything if Terminal width or height have changed.
10811094
if (newFrame.Width != _currentFrame.Width || newFrame.Height != _currentFrame.Height)

0 commit comments

Comments
 (0)