Use span-based string.Concat to avoid unnecessary allocation

Calling Substring produces a copy of the extracted substring. By using AsSpan instead of Substring and calling the overload of string.Concat that accepts spans, you can eliminate the unnecessary string allocation.
This commit is contained in:
Qstick 2023-01-09 22:18:14 -06:00
parent af28bbad52
commit e8aff90582
4 changed files with 5 additions and 6 deletions

View File

@ -194,7 +194,6 @@ dotnet_diagnostic.CA1819.severity = suggestion
dotnet_diagnostic.CA1822.severity = suggestion
dotnet_diagnostic.CA1823.severity = suggestion
dotnet_diagnostic.CA1824.severity = suggestion
dotnet_diagnostic.CA1845.severity = suggestion
dotnet_diagnostic.CA1846.severity = suggestion
dotnet_diagnostic.CA1847.severity = suggestion
dotnet_diagnostic.CA2000.severity = suggestion

View File

@ -170,7 +170,7 @@ namespace NzbDrone.Common.Http
if (baseSlashIndex >= 0)
{
return basePath.Substring(0, baseSlashIndex) + "/" + relativePath;
return $"{basePath.AsSpan(0, baseSlashIndex)}/{relativePath}";
}
return relativePath;

View File

@ -69,7 +69,7 @@ namespace NzbDrone.Core.Notifications.Discord
case DiscordGrabFieldType.Overview:
var overview = episodes.First().Overview ?? "";
discordField.Name = "Overview";
discordField.Value = overview.Length <= 300 ? overview : overview.Substring(0, 300) + "...";
discordField.Value = overview.Length <= 300 ? overview : $"{overview.AsSpan(0, 300)}...";
break;
case DiscordGrabFieldType.Rating:
discordField.Name = "Rating";
@ -160,7 +160,7 @@ namespace NzbDrone.Core.Notifications.Discord
case DiscordImportFieldType.Overview:
var overview = episodes.First().Overview ?? "";
discordField.Name = "Overview";
discordField.Value = overview.Length <= 300 ? overview : overview.Substring(0, 300) + "...";
discordField.Value = overview.Length <= 300 ? overview : $"{overview.AsSpan(0, 300)}...";
break;
case DiscordImportFieldType.Rating:
discordField.Name = "Rating";

View File

@ -545,7 +545,7 @@ namespace NzbDrone.Core.Parser
var titleWithoutExtension = RemoveFileExtension(title).ToCharArray();
Array.Reverse(titleWithoutExtension);
title = new string(titleWithoutExtension) + title.Substring(titleWithoutExtension.Length);
title = string.Concat(new string(titleWithoutExtension), title.AsSpan(titleWithoutExtension.Length));
Logger.Debug("Reversed name detected. Converted to '{0}'", title);
}
@ -576,7 +576,7 @@ namespace NzbDrone.Core.Parser
var titleWithoutExtension = RemoveFileExtension(title).ToCharArray();
Array.Reverse(titleWithoutExtension);
title = new string(titleWithoutExtension) + title.Substring(titleWithoutExtension.Length);
title = string.Concat(new string(titleWithoutExtension), title.AsSpan(titleWithoutExtension.Length));
Logger.Debug("Reversed name detected. Converted to '{0}'", title);
}