Skip to content

Commit 3f91b3c

Browse files
committed
Make UTF8Encoding threadlocal. Also add better checks. Possible Fix #516,#517
1 parent 75310d2 commit 3f91b3c

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

LiteNetLib/Utils/NetDataReader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ public string GetString(int maxLength)
294294

295295
ArraySegment<byte> data = GetBytesSegment(actualSize);
296296

297-
return (maxLength > 0 && NetDataWriter.uTF8Encoding.GetCharCount(data.Array, data.Offset, data.Count) > maxLength) ?
297+
return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(data.Array, data.Offset, data.Count) > maxLength) ?
298298
string.Empty :
299-
NetDataWriter.uTF8Encoding.GetString(data.Array, data.Offset, data.Count);
299+
NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count);
300300
}
301301

302302
public string GetString()
@@ -315,7 +315,7 @@ public string GetString()
315315

316316
ArraySegment<byte> data = GetBytesSegment(actualSize);
317317

318-
return NetDataWriter.uTF8Encoding.GetString(data.Array, data.Offset, data.Count);
318+
return NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count);
319319
}
320320

321321
public ArraySegment<byte> GetBytesSegment(int count)
@@ -456,9 +456,9 @@ public string PeekString(int maxLength)
456456
return null;
457457
}
458458

459-
return (maxLength > 0 && NetDataWriter.uTF8Encoding.GetCharCount(_data, _position + 2, actualSize) > maxLength) ?
459+
return (maxLength > 0 && NetDataWriter.uTF8Encoding.Value.GetCharCount(_data, _position + 2, actualSize) > maxLength) ?
460460
string.Empty :
461-
NetDataWriter.uTF8Encoding.GetString(_data, _position + 2, actualSize);
461+
NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize);
462462
}
463463

464464
public string PeekString()
@@ -475,7 +475,7 @@ public string PeekString()
475475
return null;
476476
}
477477

478-
return NetDataWriter.uTF8Encoding.GetString(_data, _position + 2, actualSize);
478+
return NetDataWriter.uTF8Encoding.Value.GetString(_data, _position + 2, actualSize);
479479
}
480480
#endregion
481481

LiteNetLib/Utils/NetDataWriter.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net;
33
using System.Runtime.CompilerServices;
44
using System.Text;
5+
using System.Threading;
56

67
namespace LiteNetLib.Utils
78
{
@@ -28,11 +29,8 @@ public int Length
2829
get => _position;
2930
}
3031

31-
// Cache encoding instead of creating it with BinaryWriter each time
32-
// 1000 readers before: 1MB GC, 30ms
33-
// 1000 readers after: .8MB GC, 18ms
34-
public static readonly UTF8Encoding uTF8Encoding = new UTF8Encoding(false, true);
35-
public const int StringBufferMaxLength = 1024 * 32; // <- short.MaxValue + 1
32+
public static readonly ThreadLocal<UTF8Encoding> uTF8Encoding = new ThreadLocal<UTF8Encoding>(() => new UTF8Encoding(false, true));
33+
public const int StringBufferMaxLength = 65535;
3634
private readonly byte[] _stringBuffer = new byte[StringBufferMaxLength];
3735

3836
public NetDataWriter() : this(true, InitialSize)
@@ -356,16 +354,16 @@ public void Put(string value)
356354
/// </summary>
357355
public void Put(string value, int maxLength)
358356
{
359-
if (value == null)
357+
if (string.IsNullOrEmpty(value))
360358
{
361359
Put((ushort)0);
362360
return;
363361
}
364362

365363
int length = maxLength > 0 && value.Length > maxLength ? maxLength : value.Length;
366-
int size = uTF8Encoding.GetBytes(value, 0, length, _stringBuffer, 0);
364+
int size = uTF8Encoding.Value.GetBytes(value, 0, length, _stringBuffer, 0);
367365

368-
if (size >= StringBufferMaxLength)
366+
if (size == 0 || size >= StringBufferMaxLength)
369367
{
370368
Put((ushort)0);
371369
return;

0 commit comments

Comments
 (0)