Skip to content

Commit e44f7dd

Browse files
committed
Avoid some allocations BitVector256
1 parent a869b45 commit e44f7dd

File tree

1 file changed

+14
-10
lines changed
  • src/libraries/System.Private.CoreLib/src/System/SearchValues

1 file changed

+14
-10
lines changed

src/libraries/System.Private.CoreLib/src/System/SearchValues/BitVector256.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,32 @@ private readonly bool ContainsUnchecked(int b)
5050

5151
public readonly char[] GetCharValues()
5252
{
53-
var chars = new List<char>();
54-
for (int i = 0; i < 256; i++)
53+
Span<char> chars = stackalloc char[256];
54+
int size = 0;
55+
for (int b = 0; b < 256; b++)
5556
{
56-
if (ContainsUnchecked(i))
57+
if (ContainsUnchecked(b))
5758
{
58-
chars.Add((char)i);
59+
chars[size] = (char)b;
60+
size++;
5961
}
6062
}
61-
return chars.ToArray();
63+
return chars.Slice(0, size).ToArray();
6264
}
6365

6466
public readonly byte[] GetByteValues()
6567
{
66-
var bytes = new List<byte>();
67-
for (int i = 0; i < 256; i++)
68+
Span<byte> bytes = stackalloc byte[256];
69+
int size = 0;
70+
for (int b = 0; b < 256; b++)
6871
{
69-
if (ContainsUnchecked(i))
72+
if (ContainsUnchecked(b))
7073
{
71-
bytes.Add((byte)i);
74+
bytes[size] = (byte)b;
75+
size++;
7276
}
7377
}
74-
return bytes.ToArray();
78+
return bytes.Slice(0, size).ToArray();
7579
}
7680
}
7781
}

0 commit comments

Comments
 (0)