Skip to content

Commit 9f3074c

Browse files
authored
Remove a few array allocations from enumeration (#118288)
1 parent 3f25a27 commit 9f3074c

File tree

7 files changed

+20
-24
lines changed

7 files changed

+20
-24
lines changed

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/EnumConverter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ private static long GetEnumValue(bool isUnderlyingTypeUInt64, object enumVal, Cu
8787
{
8888
bool isUnderlyingTypeUInt64 = Enum.GetUnderlyingType(EnumType) == typeof(ulong);
8989
long convertedValue = 0;
90-
string[] values = strValue.Split(',');
91-
foreach (string v in values)
90+
foreach (Range v in strValue.AsSpan().Split(','))
9291
{
93-
convertedValue |= GetEnumValue(isUnderlyingTypeUInt64, Enum.Parse(EnumType, v, true), culture);
92+
convertedValue |= GetEnumValue(isUnderlyingTypeUInt64, Enum.Parse(EnumType, strValue.AsSpan(v), true), culture);
9493
}
9594
return Enum.ToObject(EnumType, convertedValue);
9695
}

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Linux.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private static DateTime BootTime
105105
/// <summary>Gets execution path</summary>
106106
private static string? GetPathToOpenFile()
107107
{
108-
string[] allowedProgramsToRun = { "xdg-open", "gnome-open", "kfmclient" };
108+
ReadOnlySpan<string> allowedProgramsToRun = ["xdg-open", "gnome-open", "kfmclient"];
109109
foreach (var program in allowedProgramsToRun)
110110
{
111111
string? pathToProgram = FindProgramInPath(program);

src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADStoreCtx_LoadStore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,15 @@ private Principal FindPrincipalByIdentRefHelper(
602602
// This is the tricky case. They didn't specify a UrnScheme, so we need to
603603
// try all of them.
604604

605-
string[] urnSchemesToTry = new string[]
606-
{
605+
ReadOnlySpan<string> urnSchemesToTry =
606+
[
607607
UrnScheme.SamAccountScheme,
608608
UrnScheme.UpnScheme,
609609
UrnScheme.DistinguishedNameScheme,
610610
UrnScheme.SidScheme,
611611
UrnScheme.GuidScheme,
612612
UrnScheme.NameScheme
613-
};
613+
];
614614

615615
StringBuilder innerLdapFilter = new StringBuilder();
616616

src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxIPv4InterfaceProperties.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public LinuxIPv4InterfaceProperties(LinuxNetworkInterface linuxNetworkInterface)
3535

3636
private bool GetIsForwardingEnabled()
3737
{
38-
string[] paths = new string[]
39-
{
38+
ReadOnlySpan<string> paths =
39+
[
4040
// /proc/sys/net/ipv4/conf/<name>/forwarding
4141
Path.Join(NetworkFiles.Ipv4ConfigFolder, _linuxNetworkInterface.Name, NetworkFiles.ForwardingFileName),
4242
// Fall back to global forwarding config /proc/sys/net/ipv4/ip_forward
4343
NetworkFiles.Ipv4GlobalForwardingFile
44-
};
44+
];
4545

4646
for (int i = 0; i < paths.Length; i++)
4747
{

src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.Unix.Android.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ public AndroidTzData()
227227
// On Android, time zone data is found in tzdata
228228
// Based on https://github.com/mono/mono/blob/main/mcs/class/corlib/System/TimeZoneInfo.Android.cs
229229
// Also follows the locations found at the bottom of https://github.com/aosp-mirror/platform_bionic/blob/master/libc/tzcode/bionic.cpp
230-
string[] tzFileDirList = new string[] {GetApexTimeDataRoot() + "/etc/tz/", // Android 10+, TimeData module where the updates land
230+
ReadOnlySpan<string> tzFileDirList = [ GetApexTimeDataRoot() + "/etc/tz/", // Android 10+, TimeData module where the updates land
231231
GetApexRuntimeRoot() + "/etc/tz/", // Android 10+, Fallback location if the above isn't found or corrupted
232232
Environment.GetEnvironmentVariable("ANDROID_DATA") + "/misc/zoneinfo/",
233-
Environment.GetEnvironmentVariable("ANDROID_ROOT") + DefaultTimeZoneDirectory};
233+
Environment.GetEnvironmentVariable("ANDROID_ROOT") + DefaultTimeZoneDirectory ];
234234
foreach (var tzFileDir in tzFileDirList)
235235
{
236236
string tzFilePath = Path.Combine(tzFileDir, TimeZoneFileName);

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/CertificateHelpers.Windows.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static bool TryGuessRsaKeySpec(CspParameters cspParameters, out int keyS
101101

102102
// These are ordered in terms of perceived likeliness, given that the key
103103
// is AT_SIGNATURE.
104-
int[] provTypes =
104+
ReadOnlySpan<int> provTypes =
105105
[
106106
PROV_RSA_FULL,
107107
PROV_RSA_AES,
@@ -120,10 +120,8 @@ private static bool TryGuessRsaKeySpec(CspParameters cspParameters, out int keyS
120120
{
121121
using (new RSACryptoServiceProvider(cspParameters))
122122
{
123-
{
124-
keySpec = cspParameters.KeyNumber;
125-
return true;
126-
}
123+
keySpec = cspParameters.KeyNumber;
124+
return true;
127125
}
128126
}
129127
catch (CryptographicException)
@@ -141,7 +139,7 @@ private static bool TryGuessDsaKeySpec(CspParameters cspParameters, out int keyS
141139
const int PROV_DSS = 3;
142140
const int PROV_DSS_DH = 13;
143141

144-
int[] provTypes =
142+
ReadOnlySpan<int> provTypes =
145143
[
146144
PROV_DSS_DH,
147145
PROV_DSS,
@@ -155,10 +153,8 @@ private static bool TryGuessDsaKeySpec(CspParameters cspParameters, out int keyS
155153
{
156154
using (new DSACryptoServiceProvider(cspParameters))
157155
{
158-
{
159-
keySpec = cspParameters.KeyNumber;
160-
return true;
161-
}
156+
keySpec = cspParameters.KeyNumber;
157+
return true;
162158
}
163159
}
164160
catch (CryptographicException)

src/libraries/System.Speech/src/Internal/ObjectToken/RegistryDataKey.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,13 @@ private static string GetFirstKeyAndParseRemainder(ref string registryPath)
484484

485485
private static RegistryKey RegKeyFromRootPath(string rootPath)
486486
{
487-
RegistryKey[] roots = new RegistryKey[] {
487+
ReadOnlySpan<RegistryKey> roots =
488+
[
488489
Registry.ClassesRoot,
489490
Registry.LocalMachine,
490491
Registry.CurrentUser,
491492
Registry.CurrentConfig
492-
};
493+
];
493494

494495
foreach (RegistryKey key in roots)
495496
{

0 commit comments

Comments
 (0)