8000 Speed up execution by offloading logging by danmoseley · Pull Request #75 · dotnet/api-docs-sync · GitHub
[go: up one dir, main page]

Skip to content

Speed up execution by offloading logging #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Batch console writes
  • Loading branch information
danmoseley committed Aug 2, 2021
commit 14accfa556d2a3b1a4accd7b1e7d78fc4fd56444
61 changes: 47 additions & 14 deletions Libraries/Log.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks.Dataflow;

namespace Libraries
Expand All @@ -12,28 +13,60 @@ public static class Log
public static async Task StartAsync()
{
ConsoleColor initialForeground = Console.ForegroundColor;
ConsoleColor foreground = initialForeground; // cheaper than reading it each time

StringBuilder combined = new();

bool unwrittenBlob = false;
(ConsoleColor color, string msg, object[]? args) blob = new ((ConsoleColor)(-1), "", null); // compiler can't figure out we won't use this

Stopwatch sw = Stopwatch.StartNew();

while (await bufferWrite.OutputAvailableAsync())
{
(ConsoleColor, string, object[]) t = await bufferWrite.ReceiveAsync();

if (t.Item1 != (ConsoleColor)(-1))
while (unwrittenBlob || await bufferWrite.OutputAvailableAsync())
{
if (Console.ForegroundColor != t.Item1)
Console.ForegroundColor = t.Item1;
if (unwrittenBlob && foreground != blob.color)
{
Console.ForegroundColor = blob.color;
foreground = blob.color;
}

if (!unwrittenBlob)
{
blob = await bufferWrite.ReceiveAsync();

if (blob.color != (ConsoleColor)(-1) && foreground != blob.color)
{
unwrittenBlob = true; // New color - emit what we have
break;
}
}

if (blob.args == null)
{
combined.Append(blob.msg);
}
else
{
combined.AppendFormat(blob.msg, blob.args);
}

unwrittenBlob = false;

if (sw.ElapsedMilliseconds > 1000)
break;
}

if (t.Item3 == null)
{
Console.Write(t.Item2);
}
else
{
Console.Write(t.Item2, t.Item3);
}
sw.Restart();

Console.Write(combined);

combined = combined.Length < ushort.MaxValue ? combined.Clear() : new StringBuilder();
}

Console.ForegroundColor = initialForeground;
if (foreground != initialForeground)
Console.ForegroundColor = initialForeground;
}

public static void Finished()
Expand Down
0