From e8aff90582fb50b2d48dea3a4c2139c2745f1554 Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 9 Jan 2023 22:18:14 -0600 Subject: [PATCH] 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. --- .editorconfig | 1 - src/NzbDrone.Common/Http/HttpUri.cs | 2 +- src/NzbDrone.Core/Notifications/Discord/Discord.cs | 4 ++-- src/NzbDrone.Core/Parser/Parser.cs | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.editorconfig b/.editorconfig index 949cf76a3..65ada0d76 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/src/NzbDrone.Common/Http/HttpUri.cs b/src/NzbDrone.Common/Http/HttpUri.cs index ecd4abf70..d9d915bb8 100644 --- a/src/NzbDrone.Common/Http/HttpUri.cs +++ b/src/NzbDrone.Common/Http/HttpUri.cs @@ -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; diff --git a/src/NzbDrone.Core/Notifications/Discord/Discord.cs b/src/NzbDrone.Core/Notifications/Discord/Discord.cs index f6e22ecb7..0cc6aba08 100644 --- a/src/NzbDrone.Core/Notifications/Discord/Discord.cs +++ b/src/NzbDrone.Core/Notifications/Discord/Discord.cs @@ -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"; diff --git a/src/NzbDrone.Core/Parser/Parser.cs b/src/NzbDrone.Core/Parser/Parser.cs index 683996f53..b6133fe9f 100644 --- a/src/NzbDrone.Core/Parser/Parser.cs +++ b/src/NzbDrone.Core/Parser/Parser.cs @@ -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); }