From 2b355c36ff8328f962f607df4aa305e53f2e003e Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Mon, 3 Aug 2020 20:32:45 +0200 Subject: [PATCH 1/2] Minor improvements OFC I reduced some allocations --- .../Data/SqliteItemRepository.cs | 58 ++++++++++--------- .../Playback/Hls/BaseHlsService.cs | 5 +- .../Playback/Hls/DynamicHlsService.cs | 2 +- MediaBrowser.Api/Subtitles/SubtitleService.cs | 3 +- MediaBrowser.Model/Entities/MediaStream.cs | 4 +- MediaBrowser.Providers/Manager/ImageSaver.cs | 4 +- RSSDP/RSSDP.csproj | 2 - 7 files changed, 41 insertions(+), 37 deletions(-) diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 1c6d3cb94..a24641628 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -978,7 +978,10 @@ namespace Emby.Server.Implementations.Data continue; } - str.Append($"{i.Key}={i.Value}|"); + str.Append(i.Key) + .Append('=') + .Append(i.Value) + .Append('|'); } if (str.Length == 0) @@ -1032,8 +1035,8 @@ namespace Emby.Server.Implementations.Data continue; } - str.Append(ToValueString(i)) - .Append('|'); + AppendItemImageInfo(str, i); + str.Append('|'); } str.Length -= 1; // Remove last | @@ -1067,26 +1070,26 @@ namespace Emby.Server.Implementations.Data item.ImageInfos = list.ToArray(); } - public string ToValueString(ItemImageInfo image) + public void AppendItemImageInfo(StringBuilder bldr, ItemImageInfo image) { - const string Delimeter = "*"; + const char Delimeter = '*'; var path = image.Path ?? string.Empty; var hash = image.BlurHash ?? string.Empty; - return GetPathToSave(path) + - Delimeter + - image.DateModified.Ticks.ToString(CultureInfo.InvariantCulture) + - Delimeter + - image.Type + - Delimeter + - image.Width.ToString(CultureInfo.InvariantCulture) + - Delimeter + - image.Height.ToString(CultureInfo.InvariantCulture) + - Delimeter + - // Replace delimiters with other characters. - // This can be removed when we migrate to a proper DB. - hash.Replace('*', '/').Replace('|', '\\'); + bldr.Append(GetPathToSave(path)) + .Append(Delimeter) + .Append(image.DateModified.Ticks) + .Append(Delimeter) + .Append(image.Type) + .Append(Delimeter) + .Append(image.Width) + .Append(Delimeter) + .Append(image.Height) + .Append(Delimeter) + // Replace delimiters with other characters. + // This can be removed when we migrate to a proper DB. + .Append(hash.Replace('*', '/').Replace('|', '\\')); } public ItemImageInfo ItemImageInfoFromValueString(string value) @@ -5659,10 +5662,10 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type const int Limit = 100; var startIndex = 0; + const string StartInsertText = "insert into ItemValues (ItemId, Type, Value, CleanValue) values "; + var insertText = new StringBuilder(StartInsertText); while (startIndex < values.Count) { - var insertText = new StringBuilder("insert into ItemValues (ItemId, Type, Value, CleanValue) values "); - var endIndex = Math.Min(values.Count, startIndex + Limit); for (var i = startIndex; i < endIndex; i++) @@ -5704,6 +5707,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type } startIndex += Limit; + insertText.Length = StartInsertText.Length; } } @@ -5741,10 +5745,10 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type var startIndex = 0; var listIndex = 0; + const string StartInsertText = "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values "; + var insertText = new StringBuilder(StartInsertText); while (startIndex < people.Count) { - var insertText = new StringBuilder("insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values "); - var endIndex = Math.Min(people.Count, startIndex + Limit); for (var i = startIndex; i < endIndex; i++) { @@ -5778,6 +5782,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type } startIndex += Limit; + insertText.Length = StartInsertText.Length; } } @@ -5893,10 +5898,9 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type const int Limit = 10; var startIndex = 0; + var insertText = new StringBuilder(_mediaStreamSaveColumnsInsertQuery); while (startIndex < streams.Count) { - var insertText = new StringBuilder(_mediaStreamSaveColumnsInsertQuery); - var endIndex = Math.Min(streams.Count, startIndex + Limit); for (var i = startIndex; i < endIndex; i++) @@ -5979,6 +5983,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type } startIndex += Limit; + insertText.Length = _mediaStreamSaveColumnsInsertQuery.Length; } } @@ -6230,10 +6235,9 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type { const int InsertAtOnce = 10; + var insertText = new StringBuilder(_mediaAttachmentInsertPrefix); for (var startIndex = 0; startIndex < attachments.Count; startIndex += InsertAtOnce) { - var insertText = new StringBuilder(_mediaAttachmentInsertPrefix); - var endIndex = Math.Min(attachments.Count, startIndex + InsertAtOnce); for (var i = startIndex; i < endIndex; i++) @@ -6279,6 +6283,8 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type statement.Reset(); statement.MoveNext(); } + + insertText.Length = _mediaAttachmentInsertPrefix.Length; } } diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs index c80e8e64f..3cdd80fad 100644 --- a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs @@ -195,8 +195,9 @@ namespace MediaBrowser.Api.Playback.Hls // Main stream builder.Append("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=") - .AppendLine(paddedBitrate.ToString(CultureInfo.InvariantCulture)); - var playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8"); + .AppendLine(paddedBitrate.ToString(CultureInfo.InvariantCulture)) + .Append("hls/"); + var playlistUrl = Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8"); builder.AppendLine(playlistUrl); return builder.ToString(); diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 661c1ba5f..4d1473bde 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -1037,7 +1037,7 @@ namespace MediaBrowser.Api.Playback.Hls } audioTranscodeParams.Add("-vn"); - return string.Join(" ", audioTranscodeParams.ToArray()); + return string.Join(" ", audioTranscodeParams); } if (EncodingHelper.IsCopyCodec(audioCodec)) diff --git a/MediaBrowser.Api/Subtitles/SubtitleService.cs b/MediaBrowser.Api/Subtitles/SubtitleService.cs index 6a6196d8a..8dd9ca4a8 100644 --- a/MediaBrowser.Api/Subtitles/SubtitleService.cs +++ b/MediaBrowser.Api/Subtitles/SubtitleService.cs @@ -160,8 +160,6 @@ namespace MediaBrowser.Api.Subtitles var mediaSource = await _mediaSourceManager.GetMediaSource(item, request.MediaSourceId, null, false, CancellationToken.None).ConfigureAwait(false); - var builder = new StringBuilder(); - var runtime = mediaSource.RunTimeTicks ?? -1; if (runtime <= 0) @@ -175,6 +173,7 @@ namespace MediaBrowser.Api.Subtitles throw new ArgumentException("segmentLength was not given, or it was given incorrectly. (It should be bigger than 0)"); } + var builder = new StringBuilder(); builder.AppendLine("#EXTM3U") .Append("#EXT-X-TARGETDURATION:") .AppendLine(request.SegmentLength.ToString(CultureInfo.InvariantCulture)) diff --git a/MediaBrowser.Model/Entities/MediaStream.cs b/MediaBrowser.Model/Entities/MediaStream.cs index 1b37cfc93..f9ec0d238 100644 --- a/MediaBrowser.Model/Entities/MediaStream.cs +++ b/MediaBrowser.Model/Entities/MediaStream.cs @@ -233,7 +233,7 @@ namespace MediaBrowser.Model.Entities if (!string.IsNullOrEmpty(Title)) { - var result = new StringBuilder(Title); + var result = new StringBuilder(Title); foreach (var tag in attributes) { // Keep Tags that are not already in Title. @@ -246,7 +246,7 @@ namespace MediaBrowser.Model.Entities return result.ToString(); } - return string.Join(" - ", attributes.ToArray()); + return string.Join(" - ", attributes); } default: diff --git a/MediaBrowser.Providers/Manager/ImageSaver.cs b/MediaBrowser.Providers/Manager/ImageSaver.cs index f655b8edd..32b543fef 100644 --- a/MediaBrowser.Providers/Manager/ImageSaver.cs +++ b/MediaBrowser.Providers/Manager/ImageSaver.cs @@ -125,7 +125,7 @@ namespace MediaBrowser.Providers.Manager // If there are more than one output paths, the stream will need to be seekable var memoryStream = new MemoryStream(); - using (source) + await using (source.ConfigureAwait(false)) { await source.CopyToAsync(memoryStream).ConfigureAwait(false); } @@ -138,7 +138,7 @@ namespace MediaBrowser.Providers.Manager var savedPaths = new List(); - await using (source) + await using (source.ConfigureAwait(false)) { var currentPathIndex = 0; diff --git a/RSSDP/RSSDP.csproj b/RSSDP/RSSDP.csproj index 553693171..664663bd7 100644 --- a/RSSDP/RSSDP.csproj +++ b/RSSDP/RSSDP.csproj @@ -6,9 +6,7 @@ - - From 4980db159489ce009845dd109cb4b91dc809c72c Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Mon, 3 Aug 2020 20:42:01 +0200 Subject: [PATCH 2/2] Fix spelling --- .../Data/SqliteItemRepository.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index a24641628..d11e5e62e 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -1072,21 +1072,21 @@ namespace Emby.Server.Implementations.Data public void AppendItemImageInfo(StringBuilder bldr, ItemImageInfo image) { - const char Delimeter = '*'; + const char Delimiter = '*'; var path = image.Path ?? string.Empty; var hash = image.BlurHash ?? string.Empty; bldr.Append(GetPathToSave(path)) - .Append(Delimeter) + .Append(Delimiter) .Append(image.DateModified.Ticks) - .Append(Delimeter) + .Append(Delimiter) .Append(image.Type) - .Append(Delimeter) + .Append(Delimiter) .Append(image.Width) - .Append(Delimeter) + .Append(Delimiter) .Append(image.Height) - .Append(Delimeter) + .Append(Delimiter) // Replace delimiters with other characters. // This can be removed when we migrate to a proper DB. .Append(hash.Replace('*', '/').Replace('|', '\\'));