-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Use caching ConsoleStream for both iOS and Android #58967
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Use caching ConsoleStream for both iOS and Android
Expands on #56713 by moving the caching implementation to a separate internal class leaving only the interop calls in ConsolePal.iOS and ConsolePal.Android Fixes #57304
- Loading branch information
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
77 changes: 77 additions & 0 deletions
77
src/libraries/System.Console/src/System/IO/CachedConsoleStream.cs
This file contains hidden or 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,77 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Text; | ||
|
|
||
| namespace System.IO | ||
| { | ||
| internal abstract class CachedConsoleStream : ConsoleStream | ||
| { | ||
| private readonly StringBuilder _buffer = new StringBuilder(); | ||
| private readonly Encoding _encoding; | ||
| private readonly Decoder _decoder; | ||
|
|
||
| public CachedConsoleStream(Encoding encoding) : base(FileAccess.Write) | ||
| { | ||
| _encoding = encoding; | ||
| _decoder = _encoding.GetDecoder(); | ||
| } | ||
|
|
||
| public override int Read(Span<byte> buffer) => throw Error.GetReadNotSupported(); | ||
|
|
||
| public override void Write(ReadOnlySpan<byte> buffer) | ||
| { | ||
| int maxCharCount = _encoding.GetMaxCharCount(buffer.Length); | ||
| char[]? pooledBuffer = null; | ||
| Span<char> charSpan = maxCharCount <= 512 ? stackalloc char[512] : (pooledBuffer = ArrayPool<char>.Shared.Rent(maxCharCount)); | ||
| try | ||
| { | ||
| int count = _decoder.GetChars(buffer, charSpan, false); | ||
| if (count > 0) | ||
| { | ||
| WriteOrCache(this, _buffer, charSpan.Slice(0, count)); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if (pooledBuffer != null) | ||
| { | ||
| ArrayPool<char>.Shared.Return(pooledBuffer); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected abstract unsafe void Print(ReadOnlySpan<char> line); | ||
steveisok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private static void WriteOrCache(CachedConsoleStream stream, StringBuilder cache, Span<char> charBuffer) | ||
| { | ||
| int lastNewLine = charBuffer.LastIndexOf('\n'); | ||
| if (lastNewLine != -1) | ||
| { | ||
| Span<char> lineSpan = charBuffer.Slice(0, lastNewLine); | ||
| if (cache.Length > 0) | ||
| { | ||
| stream.Print(cache.Append(lineSpan).ToString()); | ||
| cache.Clear(); | ||
| } | ||
| else | ||
| { | ||
| stream.Print(lineSpan); | ||
| } | ||
|
|
||
| if (lastNewLine + 1 < charBuffer.Length) | ||
| { | ||
| cache.Append(charBuffer.Slice(lastNewLine + 1)); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // no newlines found, add the entire buffer to the cache | ||
| cache.Append(charBuffer); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.