From 4a2cacceec49685123f617a7956b2468806605a5 Mon Sep 17 00:00:00 2001 From: j0nimost Date: Thu, 26 Oct 2023 09:01:28 +0300 Subject: [PATCH] reduce allocations on newline --- src/Kafa/Writer/KafaWriter.cs | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/Kafa/Writer/KafaWriter.cs b/src/Kafa/Writer/KafaWriter.cs index 7ff6eee..8d65a0e 100644 --- a/src/Kafa/Writer/KafaWriter.cs +++ b/src/Kafa/Writer/KafaWriter.cs @@ -9,6 +9,8 @@ internal class KafaWriter : IDisposable private IBufferWriter? _bufferWriter; private KafaPooledWriter? _kafaPooledWriter; private Stream? _stream = default; + private readonly byte[] _unixNewLine =new byte[1] {(byte)'\n'}; + private readonly byte[] _winNewLine = new byte[2] {(byte)'\r',(byte)'\n'}; private readonly KafaOptions _options; @@ -34,25 +36,9 @@ public void WriteSeparator() public void WriteLine() { - byte[] newLine; - if (Environment.OSVersion.Platform == PlatformID.Unix) - { - newLine = new byte[1] - { - (byte)'\n' - }; - } - else - { - newLine = new byte[2] - { - (byte)'\r', - (byte)'\n' - }; - } + var newLine = Environment.OSVersion.Platform == PlatformID.Unix ? _unixNewLine : _winNewLine; Write(newLine); - } public void Write(string str)