diff --git a/LiteNetLib/Utils/NetDataReader.cs b/LiteNetLib/Utils/NetDataReader.cs index b0dfbfd1..2c1c383a 100644 --- a/LiteNetLib/Utils/NetDataReader.cs +++ b/LiteNetLib/Utils/NetDataReader.cs @@ -294,9 +294,9 @@ public string GetString(int maxLength) ArraySegment data = GetBytesSegment(actualSize); - return (maxLength > 0 && NetDataWriter.uTF8Encoding.GetCharCount(data.Array, data.Offset, data.Count) > maxLength) ? + return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(data.Array, data.Offset, data.Count) > maxLength) ? string.Empty : - NetDataWriter.uTF8Encoding.GetString(data.Array, data.Offset, data.Count); + NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count); } public string GetString() @@ -315,7 +315,7 @@ public string GetString() ArraySegment data = GetBytesSegment(actualSize); - return NetDataWriter.uTF8Encoding.GetString(data.Array, data.Offset, data.Count); + return NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count); } public ArraySegment GetBytesSegment(int count) @@ -456,9 +456,9 @@ public string PeekString(int maxLength) return null; } - return (maxLength > 0 && NetDataWriter.uTF8Encoding.GetCharCount(_data, _position + 2, actualSize) > maxLength) ? + return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(_data, _position + 2, actualSize) > maxLength) ? string.Empty : - NetDataWriter.uTF8Encoding.GetString(_data, _position + 2, actualSize); + NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize); } public string PeekString() @@ -475,7 +475,7 @@ public string PeekString() return null; } - return NetDataWriter.uTF8Encoding.GetString(_data, _position + 2, actualSize); + return NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize); } #endregion diff --git a/LiteNetLib/Utils/NetDataWriter.cs b/LiteNetLib/Utils/NetDataWriter.cs index c60d571c..baa8351a 100644 --- a/LiteNetLib/Utils/NetDataWriter.cs +++ b/LiteNetLib/Utils/NetDataWriter.cs @@ -2,6 +2,7 @@ using System.Net; using System.Runtime.CompilerServices; using System.Text; +using System.Threading; namespace LiteNetLib.Utils { @@ -28,11 +29,8 @@ public int Length get => _position; } - // Cache encoding instead of creating it with BinaryWriter each time - // 1000 readers before: 1MB GC, 30ms - // 1000 readers after: .8MB GC, 18ms - public static readonly UTF8Encoding uTF8Encoding = new UTF8Encoding(false, true); - public const int StringBufferMaxLength = 1024 * 32; // <- short.MaxValue + 1 + public static readonly ThreadLocal uTF8Encoding = new ThreadLocal(() => new UTF8Encoding(false, true)); + public const int StringBufferMaxLength = 65535; private readonly byte[] _stringBuffer = new byte[StringBufferMaxLength]; public NetDataWriter() : this(true, InitialSize) @@ -356,16 +354,16 @@ public void Put(string value) /// public void Put(string value, int maxLength) { - if (value == null) + if (string.IsNullOrEmpty(value)) { Put((ushort)0); return; } int length = maxLength > 0 && value.Length > maxLength ? maxLength : value.Length; - int size = uTF8Encoding.GetBytes(value, 0, length, _stringBuffer, 0); + int size = uTF8Encoding.Value.GetBytes(value, 0, length, _stringBuffer, 0); - if (size >= StringBufferMaxLength) + if (size == 0 || size >= StringBufferMaxLength) { Put((ushort)0); return;