From 42e45f93ac3a74f62f1c15992af2ac24530ceaa9 Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 9 Jan 2023 21:36:04 -0600 Subject: [PATCH] Use const where appropriate The value of a const field is computed at compile time and stored in the metadata, which improves run-time performance when it is compared to a static readonly field. --- .editorconfig | 2 -- .../DeleteEpisodeFileFixture.cs | 10 +++++----- src/NzbDrone.Core/Authentication/UserService.cs | 8 ++++---- src/Sonarr.Http/ClientSchema/SchemaBuilder.cs | 2 +- src/Sonarr.Http/Middleware/StartingUpMiddleware.cs | 4 ++-- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/.editorconfig b/.editorconfig index 086b7b022..9f651be38 100644 --- a/.editorconfig +++ b/.editorconfig @@ -183,8 +183,6 @@ dotnet_diagnostic.CA1720.severity = suggestion dotnet_diagnostic.CA1721.severity = suggestion dotnet_diagnostic.CA1724.severity = suggestion dotnet_diagnostic.CA1725.severity = suggestion -dotnet_diagnostic.CA1801.severity = suggestion -dotnet_diagnostic.CA1802.severity = suggestion dotnet_diagnostic.CA1805.severity = suggestion dotnet_diagnostic.CA1806.severity = suggestion dotnet_diagnostic.CA1810.severity = suggestion diff --git a/src/NzbDrone.Core.Test/MediaFiles/MediaFileDeletionService/DeleteEpisodeFileFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/MediaFileDeletionService/DeleteEpisodeFileFixture.cs index 59f86e4e6..6bbd36434 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/MediaFileDeletionService/DeleteEpisodeFileFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/MediaFileDeletionService/DeleteEpisodeFileFixture.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Core.Test.MediaFiles.MediaFileDeletionService [TestFixture] public class DeleteEpisodeFileFixture : CoreTest { - private static readonly string RootFolder = @"C:\Test\TV"; + private const string ROOT_FOLDER = @"C:\Test\TV"; private Series _series; private EpisodeFile _episodeFile; @@ -22,7 +22,7 @@ namespace NzbDrone.Core.Test.MediaFiles.MediaFileDeletionService public void Setup() { _series = Builder.CreateNew() - .With(s => s.Path = Path.Combine(RootFolder, "Series Title")) + .With(s => s.Path = Path.Combine(ROOT_FOLDER, "Series Title")) .Build(); _episodeFile = Builder.CreateNew() @@ -32,7 +32,7 @@ namespace NzbDrone.Core.Test.MediaFiles.MediaFileDeletionService Mocker.GetMock() .Setup(s => s.GetParentFolder(_series.Path)) - .Returns(RootFolder); + .Returns(ROOT_FOLDER); Mocker.GetMock() .Setup(s => s.GetParentFolder(_episodeFile.Path)) @@ -42,14 +42,14 @@ namespace NzbDrone.Core.Test.MediaFiles.MediaFileDeletionService private void GivenRootFolderExists() { Mocker.GetMock() - .Setup(s => s.FolderExists(RootFolder)) + .Setup(s => s.FolderExists(ROOT_FOLDER)) .Returns(true); } private void GivenRootFolderHasFolders() { Mocker.GetMock() - .Setup(s => s.GetDirectories(RootFolder)) + .Setup(s => s.GetDirectories(ROOT_FOLDER)) .Returns(new[] { _series.Path }); } diff --git a/src/NzbDrone.Core/Authentication/UserService.cs b/src/NzbDrone.Core/Authentication/UserService.cs index 7e75d8645..5cc908360 100644 --- a/src/NzbDrone.Core/Authentication/UserService.cs +++ b/src/NzbDrone.Core/Authentication/UserService.cs @@ -23,14 +23,14 @@ namespace NzbDrone.Core.Authentication public class UserService : IUserService, IHandle { + private const int ITERATIONS = 10000; + private const int SALT_SIZE = 128 / 8; + private const int NUMBER_OF_BYTES = 256 / 8; + private readonly IUserRepository _repo; private readonly IAppFolderInfo _appFolderInfo; private readonly IDiskProvider _diskProvider; - private static readonly int ITERATIONS = 10000; - private static readonly int SALT_SIZE = 128 / 8; - private static readonly int NUMBER_OF_BYTES = 256 / 8; - public UserService(IUserRepository repo, IAppFolderInfo appFolderInfo, IDiskProvider diskProvider) { _repo = repo; diff --git a/src/Sonarr.Http/ClientSchema/SchemaBuilder.cs b/src/Sonarr.Http/ClientSchema/SchemaBuilder.cs index 5fd057af9..72c1079af 100644 --- a/src/Sonarr.Http/ClientSchema/SchemaBuilder.cs +++ b/src/Sonarr.Http/ClientSchema/SchemaBuilder.cs @@ -13,7 +13,7 @@ namespace Sonarr.Http.ClientSchema { public static class SchemaBuilder { - private static readonly string PRIVATE_VALUE = "********"; + private const string PRIVATE_VALUE = "********"; private static Dictionary _mappings = new Dictionary(); public static List ToSchema(object model) diff --git a/src/Sonarr.Http/Middleware/StartingUpMiddleware.cs b/src/Sonarr.Http/Middleware/StartingUpMiddleware.cs index e26f48d10..26ef3984f 100644 --- a/src/Sonarr.Http/Middleware/StartingUpMiddleware.cs +++ b/src/Sonarr.Http/Middleware/StartingUpMiddleware.cs @@ -12,9 +12,9 @@ namespace Sonarr.Http.Middleware { public class StartingUpMiddleware { + private const string MESSAGE = "Sonarr is starting up, please try again later"; private readonly RequestDelegate _next; private readonly IRuntimeInfo _runtimeInfo; - private static readonly string MESSAGE = "Sonarr is starting up, please try again later"; public StartingUpMiddleware(RequestDelegate next, IRuntimeInfo runtimeInfo) { @@ -32,7 +32,7 @@ namespace Sonarr.Http.Middleware context.Response.StatusCode = 503; context.Response.ContentType = isJson ? "application/json" : "text/plain"; - await context.Response.Body.WriteAsync(bytes, 0, bytes.Length); + await context.Response.Body.WriteAsync(bytes); return; }