From 11cddd0eff920152206345acfa827df9fa855ecc Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Fri, 3 Apr 2015 12:19:11 -0700 Subject: String.Split optimization - Use EmptyArray.Value instead of new String[0] on .NET Core - Return early before allocating internal array(s) when count == 1 --- src/mscorlib/src/System/String.cs | 37 +++++++++++++++++++++++++++---------- 1 file 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.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.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) { -- cgit v1.2.3