Skip to content

Commit d136f4c

Browse files
committed
Change SplitOnFirst/SplitOnLast to use new LeftPart/LastRightPart APIs to reduce allocs
1 parent 77d11b1 commit d136f4c

25 files changed

Lines changed: 67 additions & 65 deletions

src/ServiceStack.Client/ContentFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static bool IsBinary(this string contentType)
7676
return true;
7777
}
7878

79-
var primaryType = realContentType.SplitOnFirst('/')[0];
79+
var primaryType = realContentType.LeftPart('/');
8080
switch (primaryType)
8181
{
8282
case "image":

src/ServiceStack.Client/Serialization/StringMapTypeDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public object PopulateFromMap(object instance, IDictionary<string, string> keyVa
130130
if (propertySerializerEntry.PropertyType == typeof (bool))
131131
{
132132
//InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
133-
propertyTextValue = propertyTextValue.SplitOnFirst(',').First();
133+
propertyTextValue = propertyTextValue.LeftPart(',');
134134
}
135135

136136
var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);

src/ServiceStack.Client/UrlExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static string GetMetadataPropertyType(this Type type)
110110
public static string GetOperationName(this Type type)
111111
{
112112
var typeName = type.FullName != null //can be null, e.g. generic types
113-
? type.FullName.SplitOnFirst("[[")[0] //Generic Fullname
113+
? type.FullName.LeftPart("[[") //Generic Fullname
114114
.Replace(type.Namespace + ".", "") //Trim Namespaces
115115
.Replace("+", ".") //Convert nested into normal type
116116
: type.Name;
@@ -131,7 +131,7 @@ public static string ExpandTypeName(this Type type)
131131

132132
public static string ExpandGenericTypeName(Type type)
133133
{
134-
var nameOnly = type.Name.SplitOnFirst('`')[0];
134+
var nameOnly = type.Name.LeftPart('`');
135135

136136
var sb = StringBuilderCache.Allocate();
137137
foreach (var arg in type.GetGenericArguments())

src/ServiceStack.Razor/Compilation/CodeTransformers/DefaultCodeTransformers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class SetBaseType : RazorCodeTransformerBase
9393
private readonly string _typeName;
9494
public SetBaseType(string typeName, bool? isGenericType = null)
9595
{
96-
_typeName = typeName.SplitOnLast("`")[0]; //get clean generic name without 'GenericType`1' n args suffix
96+
_typeName = typeName.LastLeftPart("`"); //get clean generic name without 'GenericType`1' n args suffix
9797
this.isGenericType = isGenericType ?? typeName.Contains("`");
9898
}
9999

src/ServiceStack.Razor/Managers/RazorViewManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ protected virtual RazorPage AddPage(RazorPage page, string pagePath = null)
209209
//Views should be uniquely named and stored in any deep folder structure
210210
if (pagePath.StartsWithIgnoreCase("views/") && !pagePath.EndsWithIgnoreCase(DefaultLayoutFile))
211211
{
212-
var viewName = pagePath.SplitOnLast('.').First().SplitOnLast('/').Last();
212+
var viewName = pagePath.LastLeftPart('.').LastRightPart('/');
213213
ViewNamesMap[viewName] = pagePath;
214214
}
215215

@@ -260,7 +260,7 @@ public virtual RazorPage GetLayoutPage(string layoutName, RazorPage page, IReque
260260
string contextParentDir = contextRelativePath;
261261
do
262262
{
263-
contextParentDir = (contextParentDir ?? "").SplitOnLast('/').First();
263+
contextParentDir = (contextParentDir ?? "").LastLeftPart('/');
264264

265265
var path = CombinePaths(contextParentDir, layoutFile);
266266
var layoutPage = GetPage(path);

src/ServiceStack.Razor/RazorFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void Register(IAppHost appHost)
123123
var loadedAssemblyNames = CompilerServices
124124
.GetLoadedAssemblies()
125125
.Where(x => !x.IsDynamic)
126-
.Map(x => x.FullName.SplitOnFirst(',')[0]);
126+
.Map(x => x.FullName.LeftPart(','));
127127

128128
foreach (var razorNamespace in appHost.Config.RazorNamespaces)
129129
{

src/ServiceStack.Server/AutoQueryFeature.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private static void AppendTypedQueries(SqlExpression<From> q, IQueryDb dto, Dict
727727
{
728728
foreach (var entry in PropertyGetters)
729729
{
730-
var name = entry.Key.SplitOnFirst('#')[0];
730+
var name = entry.Key.LeftPart('#');
731731

732732
QueryDbFieldAttribute implicitQuery;
733733
QueryFieldMap.TryGetValue(name, out implicitQuery);
@@ -824,7 +824,7 @@ private static void AppendUntypedQueries(SqlExpression<From> q, Dictionary<strin
824824
{
825825
foreach (var entry in dynamicParams)
826826
{
827-
var name = entry.Key.SplitOnFirst('#')[0];
827+
var name = entry.Key.LeftPart('#');
828828

829829
var match = GetQueryMatch(q, name, options, aliases);
830830
if (match == null)

src/ServiceStack/AutoQueryDataFeature.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ private static void AppendTypedQueries(IDataQuery q, IQueryData dto, Dictionary<
11501150
{
11511151
foreach (var entry in RequestPropertyGetters)
11521152
{
1153-
var name = entry.Key.SplitOnFirst('#')[0];
1153+
var name = entry.Key.LeftPart('#');
11541154

11551155
QueryDataField attr;
11561156
if (QueryFieldMap.TryGetValue(name, out attr))
@@ -1210,7 +1210,7 @@ private static void AppendUntypedQueries(IDataQuery q, Dictionary<string, string
12101210
{
12111211
foreach (var entry in dynamicParams)
12121212
{
1213-
var name = entry.Key.SplitOnFirst('#')[0];
1213+
var name = entry.Key.LeftPart('#');
12141214

12151215
var match = GetQueryMatch(q, name, options, aliases);
12161216
if (match == null)

src/ServiceStack/Host/Handlers/SoapHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ protected string GetAction(Message requestMsg, string xml)
329329
}
330330

331331
return xml.StartsWith("<")
332-
? xml.Substring(1, xml.IndexOf(" ") - 1).SplitOnFirst(':').Last()
332+
? xml.Substring(1, xml.IndexOf(" ") - 1).RightPart(':')
333333
: null;
334334
}
335335

src/ServiceStack/Host/ServiceMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ private static void AddReferencedTypes(MetadataType metadataType, MetadataTypes
451451
}
452452
else if (p.IsArray())
453453
{
454-
var elType = p.Type.SplitOnFirst('[')[0];
454+
var elType = p.Type.LeftPart('[');
455455
type = FindMetadataType(metadataTypes, elType);
456456
if (type != null && !types.Contains(type))
457457
{

0 commit comments

Comments
 (0)