Skip to content

Commit

Permalink
Make UTF8Encoding threadlocal. Also add better checks. Possible Fix #516
Browse files Browse the repository at this point in the history
  • Loading branch information
RevenantX committed Apr 2, 2023
1 parent 75310d2 commit 3f91b3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
12 changes: 6 additions & 6 deletions LiteNetLib/Utils/NetDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ public string GetString(int maxLength)

ArraySegment<byte> 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()
Expand All @@ -315,7 +315,7 @@ public string GetString()

ArraySegment<byte> 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<byte> GetBytesSegment(int count)
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand Down
14 changes: 6 additions & 8 deletions LiteNetLib/Utils/NetDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace LiteNetLib.Utils
{
Expand All @@ -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> uTF8Encoding = new ThreadLocal<UTF8Encoding>(() => new UTF8Encoding(false, true));
public const int StringBufferMaxLength = 65535;
private readonly byte[] _stringBuffer = new byte[StringBufferMaxLength];

public NetDataWriter() : this(true, InitialSize)
Expand Down Expand Up @@ -356,16 +354,16 @@ public void Put(string value)
/// </summary>
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;
Expand Down

0 comments on commit 3f91b3c

Please sign in to comment.