Review usage of string.Substring (part 1)

Reduced allocations by replacing string.Substring with ReadOnlySpan<char>.Slice
This commit is contained in:
Bond_009
2020-07-22 13:34:51 +02:00
parent 0750357916
commit febb6bced6
25 changed files with 124 additions and 99 deletions

View File

@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
@@ -105,7 +106,12 @@ namespace Emby.Server.Implementations.Services
}
var expectedMethodName = actionName.Substring(0, 1) + actionName.Substring(1).ToLowerInvariant();
throw new NotImplementedException(string.Format("Could not find method named {1}({0}) or Any({0}) on Service {2}", requestDto.GetType().GetMethodName(), expectedMethodName, serviceType.GetMethodName()));
throw new NotImplementedException(
string.Format(
CultureInfo.InvariantCulture,
"Could not find method named {1}({0}) or Any({0}) on Service {2}",
requestDto.GetType().GetMethodName(),
expectedMethodName, serviceType.GetMethodName()));
}
private static async Task<object> GetTaskResult(Task task)

View File

@@ -44,7 +44,7 @@ namespace Emby.Server.Implementations.Services
var pos = pathInfo.LastIndexOf('.');
if (pos != -1)
{
var format = pathInfo.Substring(pos + 1);
var format = pathInfo.AsSpan().Slice(pos + 1);
contentType = GetFormatContentType(format);
if (contentType != null)
{
@@ -55,15 +55,18 @@ namespace Emby.Server.Implementations.Services
return pathInfo;
}
private static string GetFormatContentType(string format)
private static string GetFormatContentType(ReadOnlySpan<char> format)
{
// built-in formats
switch (format)
if (format.Equals("json", StringComparison.Ordinal))
{
case "json": return "application/json";
case "xml": return "application/xml";
default: return null;
return "application/json";
}
else if (format.Equals("xml", StringComparison.Ordinal))
{
return "application/xml";
}
return null;
}
public async Task ProcessRequestAsync(HttpListenerHost httpHost, IRequest httpReq, HttpResponse httpRes, ILogger logger, CancellationToken cancellationToken)

View File

@@ -156,7 +156,7 @@ namespace Emby.Server.Implementations.Services
{
var component = components[i];
if (component.StartsWith(VariablePrefix))
if (component.StartsWith(VariablePrefix, StringComparison.Ordinal))
{
var variableName = component.Substring(1, component.Length - 2);
if (variableName[variableName.Length - 1] == WildCardChar)