Skip to content

Commit 9b64cb5

Browse files
committed
Replace all possible T[0] with Array.Empty<T>
1 parent f284088 commit 9b64cb5

File tree

16 files changed

+596
-467
lines changed

16 files changed

+596
-467
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ using (var ms = new MemoryStream())
791791
using (var ms = new MemoryStream())
792792
{
793793
// serialize empty array.
794-
ProtoBuf.Serializer.Serialize<Parent>(ms, new Parent { Array = new int[0] });
794+
ProtoBuf.Serializer.Serialize<Parent>(ms, new Parent { Array = System.Array.Empty<int>() });
795795

796796
ms.Position = 0;
797797
var result = ProtoBuf.Serializer.Deserialize<Parent>(ms);

src/MessagePack.ImmutableCollection/Formatters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected override ImmutableStack<T> Complete(T[] intermediateCollection)
218218

219219
protected override T[] Create(int count, MessagePackSerializerOptions options)
220220
{
221-
return new T[count];
221+
return count == 0 ? Array.Empty<T>() : new T[count];
222222
}
223223
}
224224

@@ -308,7 +308,7 @@ protected override IImmutableStack<T> Complete(T[] intermediateCollection)
308308

309309
protected override T[] Create(int count, MessagePackSerializerOptions options)
310310
{
311-
return new T[count];
311+
return count == 0 ? Array.Empty<T>() : new T[count];
312312
}
313313
}
314314

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
419419

420420
protected override T[] Create(int count, MessagePackSerializerOptions options)
421421
{
422-
return new T[count];
422+
return count == 0 ? Array.Empty<T>() : new T[count];
423423
}
424424

425425
protected override Stack<T>.Enumerator GetSourceEnumerator(Stack<T> source)
@@ -475,7 +475,7 @@ protected override ReadOnlyCollection<T> Complete(T[] intermediateCollection)
475475

476476
protected override T[] Create(int count, MessagePackSerializerOptions options)
477477
{
478-
return new T[count];
478+
return count == 0 ? Array.Empty<T>() : new T[count];
479479
}
480480
}
481481

@@ -489,7 +489,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
489489

490490
protected override T[] Create(int count, MessagePackSerializerOptions options)
491491
{
492-
return new T[count];
492+
return count == 0 ? Array.Empty<T>() : new T[count];
493493
}
494494

495495
protected override IList<T> Complete(T[] intermediateCollection)
@@ -508,7 +508,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
508508

509509
protected override T[] Create(int count, MessagePackSerializerOptions options)
510510
{
511-
return new T[count];
511+
return count == 0 ? Array.Empty<T>() : new T[count];
512512
}
513513

514514
protected override ICollection<T> Complete(T[] intermediateCollection)
@@ -562,7 +562,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
562562

563563
protected override T[] Create(int count, MessagePackSerializerOptions options)
564564
{
565-
return new T[count];
565+
return count == 0 ? Array.Empty<T>() : new T[count];
566566
}
567567

568568
protected override IEnumerable<T> Complete(T[] intermediateCollection)
@@ -796,6 +796,10 @@ public IList Deserialize(ref MessagePackReader reader, MessagePackSerializerOpti
796796
IMessagePackFormatter<object> formatter = options.Resolver.GetFormatterWithVerify<object>();
797797

798798
var count = reader.ReadArrayHeader();
799+
if (count == 0)
800+
{
801+
return Array.Empty<object>();
802+
}
799803

800804
var list = new object[count];
801805
options.Security.DepthStep(ref reader);
@@ -969,7 +973,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
969973

970974
protected override T[] Create(int count, MessagePackSerializerOptions options)
971975
{
972-
return new T[count];
976+
return count == 0 ? Array.Empty<T>() : new T[count];
973977
}
974978

975979
protected override IReadOnlyList<T> Complete(T[] intermediateCollection)
@@ -987,7 +991,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
987991

988992
protected override T[] Create(int count, MessagePackSerializerOptions options)
989993
{
990-
return new T[count];
994+
return count == 0 ? Array.Empty<T>() : new T[count];
991995
}
992996

993997
protected override IReadOnlyCollection<T> Complete(T[] intermediateCollection)
@@ -1065,7 +1069,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
10651069

10661070
protected override T[] Create(int count, MessagePackSerializerOptions options)
10671071
{
1068-
return new T[count];
1072+
return count == 0 ? Array.Empty<T>() : new T[count];
10691073
}
10701074

10711075
protected override ConcurrentStack<T> Complete(T[] intermediateCollection)

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DateTimeFormatters.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,23 @@ public DateTime[] Deserialize(ref MessagePackReader reader, MessagePackSerialize
5151
{
5252
if (reader.TryReadNil())
5353
{
54-
return null;
54+
return default;
5555
}
56-
else
56+
57+
var len = reader.ReadArrayHeader();
58+
if (len == 0)
5759
{
58-
var len = reader.ReadArrayHeader();
59-
var array = new DateTime[len];
60-
for (int i = 0; i < array.Length; i++)
61-
{
62-
var dateData = reader.ReadInt64();
63-
array[i] = DateTime.FromBinary(dateData);
64-
}
60+
return Array.Empty<DateTime>();
61+
}
6562

66-
return array;
63+
var array = new DateTime[len];
64+
for (int i = 0; i < array.Length; i++)
65+
{
66+
var dateData = reader.ReadInt64();
67+
array[i] = DateTime.FromBinary(dateData);
6768
}
69+
70+
return array;
6871
}
6972
}
7073
}

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/PrimitiveObjectFormatter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOpt
281281
case MessagePackType.Array:
282282
{
283283
var length = reader.ReadArrayHeader();
284+
if (length == 0)
285+
{
286+
return Array.Empty<object>();
287+
}
284288

285289
IMessagePackFormatter<object> objectFormatter = resolver.GetFormatter<object>();
286290
var array = new object[length];

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/StandardClassLibraryFormatter.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ public String[] Deserialize(ref MessagePackReader reader, MessagePackSerializerO
8484
{
8585
return null;
8686
}
87-
else
87+
88+
var len = reader.ReadArrayHeader();
89+
if (len == 0)
8890
{
89-
var len = reader.ReadArrayHeader();
90-
var array = new String[len];
91-
for (int i = 0; i < array.Length; i++)
92-
{
93-
array[i] = reader.ReadString();
94-
}
91+
return Array.Empty<String>();
92+
}
9593

96-
return array;
94+
var array = new String[len];
95+
for (int i = 0; i < array.Length; i++)
96+
{
97+
array[i] = reader.ReadString();
9798
}
99+
100+
return array;
98101
}
99102
}
100103

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Internal/AutomataDictionary.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ public void EmitMatch(ILGenerator il, LocalBuilder bytesSpan, LocalBuilder key,
142142

143143
private class AutomataNode : IComparable<AutomataNode>
144144
{
145-
private static readonly AutomataNode[] EmptyNodes = new AutomataNode[0];
146-
private static readonly ulong[] EmptyKeys = new ulong[0];
147-
148145
#pragma warning disable SA1401 // Fields should be private
149146
internal ulong Key;
150147
internal int Value;
@@ -164,8 +161,8 @@ public AutomataNode(ulong key)
164161
{
165162
this.Key = key;
166163
this.Value = -1;
167-
this.nexts = EmptyNodes;
168-
this.nextKeys = EmptyKeys;
164+
this.nexts = Array.Empty<AutomataNode>();
165+
this.nextKeys = Array.Empty<ulong>();
169166
this.count = 0;
170167
this.OriginalKey = null;
171168
}

0 commit comments

Comments
 (0)