Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 46c9e83

Browse files
committed
Some improvements for RegistryKey
- avoid empty array allocation - replace manual array copying with `Array.Copy` - replace CopyTo with ToArray
1 parent b6b18dd commit 46c9e83

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,10 @@ internal unsafe String[] InternalGetSubKeyNames()
842842
{
843843
EnsureNotDisposed();
844844
int subkeys = InternalSubKeyCount();
845-
String[] names = new String[subkeys]; // Returns 0-length array if empty.
846845

847846
if (subkeys > 0)
848847
{
848+
String[] names = new String[subkeys];
849849
char[] name = new char[MaxKeyLength + 1];
850850

851851
int namelen;
@@ -868,9 +868,11 @@ internal unsafe String[] InternalGetSubKeyNames()
868868
names[i] = new String(namePtr);
869869
}
870870
}
871+
872+
return names;
871873
}
872874

873-
return names;
875+
return Array.Empty<String>();
874876
}
875877

876878
/**
@@ -921,10 +923,10 @@ public unsafe String[] GetValueNames()
921923
EnsureNotDisposed();
922924

923925
int values = InternalValueCount();
924-
String[] names = new String[values];
925926

926927
if (values > 0)
927928
{
929+
String[] names = new String[values];
928930
char[] name = new char[MaxValueLength + 1];
929931
int namelen;
930932

@@ -953,9 +955,11 @@ public unsafe String[] GetValueNames()
953955
names[i] = new String(namePtr);
954956
}
955957
}
958+
959+
return names;
956960
}
957961

958-
return names;
962+
return Array.Empty<String>();
959963
}
960964

961965
/**
@@ -1201,10 +1205,7 @@ internal Object InternalGetValue(String name, Object defaultValue, bool doNotExp
12011205
try
12021206
{
12031207
char[] newBlob = new char[checked(blob.Length + 1)];
1204-
for (int i = 0; i < blob.Length; i++)
1205-
{
1206-
newBlob[i] = blob[i];
1207-
}
1208+
Array.Copy(blob, 0, newBlob, 0, blob.Length);
12081209
newBlob[newBlob.Length - 1] = (char)0;
12091210
blob = newBlob;
12101211
}
@@ -1215,8 +1216,7 @@ internal Object InternalGetValue(String name, Object defaultValue, bool doNotExp
12151216
blob[blob.Length - 1] = (char)0;
12161217
}
12171218

1218-
1219-
IList<String> strings = new List<String>();
1219+
var strings = new List<String>();
12201220
int cur = 0;
12211221
int len = blob.Length;
12221222

@@ -1250,8 +1250,7 @@ internal Object InternalGetValue(String name, Object defaultValue, bool doNotExp
12501250
cur = nextNull + 1;
12511251
}
12521252

1253-
data = new String[strings.Count];
1254-
strings.CopyTo((String[])data, 0);
1253+
data = strings.ToArray();
12551254
}
12561255
break;
12571256
case Interop.mincore.RegistryValues.REG_LINK:

0 commit comments

Comments
 (0)