summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Van Patten <jvp@justinvp.com>2015-04-03 12:19:11 -0700
committerJustin Van Patten <jvp@justinvp.com>2015-06-30 15:07:58 -0700
commit11cddd0eff920152206345acfa827df9fa855ecc (patch)
tree9a96de724984e9722c277eb92ff52b5dd5919b4d
parent572b986b2e7ca4bc25e05be96872bcab506b745e (diff)
downloadcoreclr-11cddd0eff920152206345acfa827df9fa855ecc.tar.gz
coreclr-11cddd0eff920152206345acfa827df9fa855ecc.tar.bz2
coreclr-11cddd0eff920152206345acfa827df9fa855ecc.zip
String.Split optimization
- Use EmptyArray<String>.Value instead of new String[0] on .NET Core - Return early before allocating internal array(s) when count == 1
-rw-r--r--src/mscorlib/src/System/String.cs37
1 files changed, 27 insertions, 10 deletions
diff --git a/src/mscorlib/src/System/String.cs b/src/mscorlib/src/System/String.cs
index 9d4dcde887..fbf93c9860 100644
--- a/src/mscorlib/src/System/String.cs
+++ b/src/mscorlib/src/System/String.cs
@@ -973,17 +973,26 @@ namespace System {
if ((count == 0) || (omitEmptyEntries && this.Length == 0))
{
+#if FEATURE_CORECLR
+ return EmptyArray<String>.Value;
+#else
+ // Keep the old behavior of returning a new empty array
+ // to mitigate any potential compat risk.
return new String[0];
+#endif
+ }
+
+ if (count == 1)
+ {
+ return new String[] { this };
}
int[] sepList = new int[Length];
int numReplaces = MakeSeparatorList(separator, ref sepList);
- //Handle the special case of no replaces and special count.
- if (0 == numReplaces || count == 1) {
- String[] stringArray = new String[1];
- stringArray[0] = this;
- return stringArray;
+ // Handle the special case of no replaces.
+ if (0 == numReplaces) {
+ return new String[] { this };
}
if(omitEmptyEntries)
@@ -1021,18 +1030,26 @@ namespace System {
}
if ((count == 0) || (omitEmptyEntries && this.Length ==0)) {
+#if FEATURE_CORECLR
+ return EmptyArray<String>.Value;
+#else
+ // Keep the old behavior of returning a new empty array
+ // to mitigate any potential compat risk.
return new String[0];
+#endif
+ }
+
+ if (count == 1) {
+ return new String[] { this };
}
int[] sepList = new int[Length];
int[] lengthList = new int[Length];
int numReplaces = MakeSeparatorList(separator, ref sepList, ref lengthList);
- //Handle the special case of no replaces and special count.
- if (0 == numReplaces || count == 1) {
- String[] stringArray = new String[1];
- stringArray[0] = this;
- return stringArray;
+ // Handle the special case of no replaces.
+ if (0 == numReplaces) {
+ return new String[] { this };
}
if (omitEmptyEntries) {