Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
71b3888
Fix Console.Write on iOS
MaximLipnin Aug 2, 2021
8f6f7bc
Move the helper method to iOS-specific location
MaximLipnin Aug 2, 2021
a97f0d5
Add a test
MaximLipnin Aug 2, 2021
90bf33c
Remove unused using
MaximLipnin Aug 2, 2021
e6806e4
Apply Ralf's suggestion
MaximLipnin Aug 2, 2021
2bdb6e7
Move Interop.Sys.Log to a local function
MaximLipnin Aug 2, 2021
612dc1c
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 3, 2021
6f8542b
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 9, 2021
47d8f01
Move the caching logic over to StreamWriter
MaximLipnin Aug 10, 2021
4f0653a
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 10, 2021
14545a8
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 11, 2021
725e64c
feedback
MaximLipnin Aug 11, 2021
5b8499c
Remove unused usings
MaximLipnin Aug 12, 2021
61bb68b
Derive from StreamWriter
MaximLipnin Aug 12, 2021
68e10e7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 12, 2021
ac45868
Feedback
MaximLipnin Aug 12, 2021
2e57d54
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 16, 2021
c8aa30f
Try a solution not changing public API surface and using NSLogStream …
MaximLipnin Aug 16, 2021
b76ddfe
Some feedback
MaximLipnin Aug 17, 2021
b999fff
Use ArrayPool
MaximLipnin Aug 17, 2021
49f5c41
Not rely on the length of the rented array
MaximLipnin Aug 17, 2021
aaa1f35
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 18, 2021
90da2a0
Address the feedback
MaximLipnin Aug 27, 2021
88685c7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 27, 2021
976954b
not use _ prefix for local variable
MaximLipnin Aug 27, 2021
245edb3
Address the feedback
MaximLipnin Aug 27, 2021
a234df5
Merge branch 'fix_console_write_on_ios' of https://github.com/maximli…
MaximLipnin Aug 27, 2021
34d215f
Fix an error
MaximLipnin Aug 30, 2021
1eb7528
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 30, 2021
a2e118e
Print a span rather than a string
MaximLipnin Aug 30, 2021
732a62f
Slice the char span to the exact used length
MaximLipnin Sep 1, 2021
9375848
Address Stephen's feedback
MaximLipnin Sep 1, 2021
7504d30
Remove a redundant using
MaximLipnin Sep 1, 2021
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
Try a solution not changing public API surface and using NSLogStream …
…with a decoder
  • Loading branch information
MaximLipnin committed Aug 16, 2021
commit c8aa30ff4b88c3847cb69be8f9f5f2b65d8a0b8a
6 changes: 0 additions & 6 deletions src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="'$(TargetsMacCatalyst)' == 'true'">$(DefineConstants);TARGET_MACCATALYST</DefineConstants>
<DefineConstants Condition="'$(TargetsiOS)' == 'true'">$(DefineConstants);TARGET_IOS</DefineConstants>
<DefineConstants Condition="'$(TargetstvOS)' == 'true'">$(DefineConstants);TARGET_TVOS</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Console.cs" />
<Compile Include="System\ConsoleCancelEventArgs.cs" />
Expand All @@ -30,7 +25,6 @@
Link="Common\Interop\Unix\Interop.Log.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="System\IO\StreamWriter.iOS.cs" />
</ItemGroup>
<!-- Android -->
<ItemGroup Condition="'$(TargetsAndroid)' == 'true'">
Expand Down
4 changes: 0 additions & 4 deletions src/libraries/System.Console/src/System/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,7 @@ private static TextWriter CreateOutputWriter(Stream outputStream)
{
return TextWriter.Synchronized(outputStream == Stream.Null ?
StreamWriter.Null :
#if TARGET_IOS || TARGET_TVOS || TARGET_MACCATALYST
new IOSStreamWriter(
#else
new StreamWriter(
#endif
stream: outputStream,
encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream.
bufferSize: WriteBufferSize,
Expand Down
72 changes: 69 additions & 3 deletions src/libraries/System.Console/src/System/ConsolePal.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,89 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace System
{
internal sealed class NSLogStream : ConsoleStream
{
public NSLogStream() : base(FileAccess.Write) {}
private readonly StringBuilder _buffer = new StringBuilder();
private Decoder _decoder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private Decoder _decoder;
private readonly Decoder _decoder;


public NSLogStream() : base(FileAccess.Write)
{
_decoder = Encoding.Unicode.GetDecoder();
}

public override int Read(Span<byte> buffer) => throw Error.GetReadNotSupported();

public override unsafe void Write(ReadOnlySpan<byte> buffer)
{
fixed (byte* ptr = buffer)
int charCount = _decoder.GetCharCount(buffer, false);
Span<char> charSpan = new char[charCount];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this array be pooled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please clarify a bit with an example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time this method is called, it is allocating a new array just to use as a scratch pad, and then thrown away. If we pool the array instead, we don't need to allocate a new array every time.

Here's an example:

https://github.com/dotnet/runtime/blob/60af14837e78d69d5469c2c0c7632cfca761aa09/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L66-L78

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

int count = _decoder.GetChars(buffer, charSpan, false);
if (count > 0)
{
WriteOrCache(_buffer, charSpan);
}
}

private static unsafe void WriteOrCache(StringBuilder cache, Span<char> charBuffer)
{
int lineStartIndex = 0;
for (int i = 0; i < charBuffer.Length; i++)
{
if (charBuffer[i] == '\n')
{
Span<char> lineSpan = charBuffer.Slice(lineStartIndex, i - lineStartIndex);
if (cache.Length > 0)
{
printer(cache.Append(lineSpan).ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you can use the StringBuilder.GetChunks() method instead of allocating a new string here. You should be able to enumerate through the "chunks" of the StringBuilder, and write them to Interop.Sys.Log without allocating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String builder can contain more than 1 chunk so enumerating though and writing them to Interop.Sys.Log means invoking NSLog several times, which would result in the same situation as right now because NSLog automatically appends a new line to any input.

cache.Clear();
}
else
{
printer(lineSpan.ToString());
}

lineStartIndex = i + 1;
}
}

if (lineStartIndex < charBuffer.Length)
{
// no newlines found, add the entire buffer to the cache
cache.Append(charBuffer.Slice(lineStartIndex, charBuffer.Length - lineStartIndex));
}

static void printer(string line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method names should be PascalCased. And should be verbs.

Suggested change
static void printer(string line)
static void Print(string line)

{
fixed (char* ptr = line)
{
Interop.Sys.Log((byte*)ptr, line.Length * 2);
}
}
}

protected override void Dispose(bool disposing)
{
if (_buffer.Length > 0)
{
Interop.Sys.Log(ptr, buffer.Length);
var line = _buffer.ToString();
_buffer.Clear();
unsafe
{
fixed (char* ptr = line)
{
Interop.Sys.Log((byte*)ptr, line.Length * 2);
}
}
}

base.Dispose(disposing);
}
}

Expand Down
104 changes: 0 additions & 104 deletions src/libraries/System.Console/src/System/IO/StreamWriter.iOS.cs

This file was deleted.

Loading