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
Change to Channels
  • Loading branch information
danmoseley committed Aug 3, 2021
commit 4621ab0d1c93ca78f10726b52ed8e9d5f00310c4
16 changes: 8 additions & 8 deletions Libraries/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks.Dataflow;
using System.Threading.Channels;

namespace Libraries
{
public static class Log
{
private static BufferBlock<(ConsoleColor, string, object[]?)> bufferWrite = new();
private static Channel<(ConsoleColor, string, object[]?)> channel = Channel.CreateUnbounded<(ConsoleColor, string, object[]?)>();

public static async Task StartAsync()
{
Expand All @@ -25,9 +25,9 @@ public static async Task StartAsync()

Stopwatch stopwatch = Stopwatch.StartNew();

while (await bufferWrite.OutputAvailableAsync())
while (await channel.Reader.WaitToReadAsync())
{
while (unwrittenBlob || await bufferWrite.OutputAvailableAsync())
while (unwrittenBlob || await channel.Reader.WaitToReadAsync())
{
if (unwrittenBlob && foreground != blob.color)
{
Expand All @@ -37,7 +37,7 @@ public static async Task StartAsync()

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

if (blob.color != (ConsoleColor)(-1) && foreground != blob.color)
{
Expand Down Expand Up @@ -77,18 +77,18 @@ public static async Task StartAsync()

public static void Finished()
{
bufferWrite.Complete();
channel.Writer.Complete();
}

public static void Print(bool endline, ConsoleColor foregroundColor, string format, params object[]? args)
{
if (endline)
{
bufferWrite.Post((foregroundColor, format + Environment.NewLine, args));
channel.Writer.WriteAsync((foregroundColor, format + Environment.NewLine, args));
}
else
{
bufferWrite.Post((foregroundColor, format, args));
channel.Writer.WriteAsync((foregroundColor, format, args));
}
}

Expand Down
0