Minor improvements

OFC I reduced some allocations
pull/3809/head
Bond_009 4 years ago
parent 5b4863c65b
commit 2b355c36ff

@ -978,7 +978,10 @@ namespace Emby.Server.Implementations.Data
continue; continue;
} }
str.Append($"{i.Key}={i.Value}|"); str.Append(i.Key)
.Append('=')
.Append(i.Value)
.Append('|');
} }
if (str.Length == 0) if (str.Length == 0)
@ -1032,8 +1035,8 @@ namespace Emby.Server.Implementations.Data
continue; continue;
} }
str.Append(ToValueString(i)) AppendItemImageInfo(str, i);
.Append('|'); str.Append('|');
} }
str.Length -= 1; // Remove last | str.Length -= 1; // Remove last |
@ -1067,26 +1070,26 @@ namespace Emby.Server.Implementations.Data
item.ImageInfos = list.ToArray(); 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 path = image.Path ?? string.Empty;
var hash = image.BlurHash ?? string.Empty; var hash = image.BlurHash ?? string.Empty;
return GetPathToSave(path) + bldr.Append(GetPathToSave(path))
Delimeter + .Append(Delimeter)
image.DateModified.Ticks.ToString(CultureInfo.InvariantCulture) + .Append(image.DateModified.Ticks)
Delimeter + .Append(Delimeter)
image.Type + .Append(image.Type)
Delimeter + .Append(Delimeter)
image.Width.ToString(CultureInfo.InvariantCulture) + .Append(image.Width)
Delimeter + .Append(Delimeter)
image.Height.ToString(CultureInfo.InvariantCulture) + .Append(image.Height)
Delimeter + .Append(Delimeter)
// Replace delimiters with other characters. // Replace delimiters with other characters.
// This can be removed when we migrate to a proper DB. // This can be removed when we migrate to a proper DB.
hash.Replace('*', '/').Replace('|', '\\'); .Append(hash.Replace('*', '/').Replace('|', '\\'));
} }
public ItemImageInfo ItemImageInfoFromValueString(string value) 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; const int Limit = 100;
var startIndex = 0; var startIndex = 0;
const string StartInsertText = "insert into ItemValues (ItemId, Type, Value, CleanValue) values ";
var insertText = new StringBuilder(StartInsertText);
while (startIndex < values.Count) while (startIndex < values.Count)
{ {
var insertText = new StringBuilder("insert into ItemValues (ItemId, Type, Value, CleanValue) values ");
var endIndex = Math.Min(values.Count, startIndex + Limit); var endIndex = Math.Min(values.Count, startIndex + Limit);
for (var i = startIndex; i < endIndex; i++) 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; 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 startIndex = 0;
var listIndex = 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) 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); var endIndex = Math.Min(people.Count, startIndex + Limit);
for (var i = startIndex; i < endIndex; i++) 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; 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; const int Limit = 10;
var startIndex = 0; var startIndex = 0;
var insertText = new StringBuilder(_mediaStreamSaveColumnsInsertQuery);
while (startIndex < streams.Count) while (startIndex < streams.Count)
{ {
var insertText = new StringBuilder(_mediaStreamSaveColumnsInsertQuery);
var endIndex = Math.Min(streams.Count, startIndex + Limit); var endIndex = Math.Min(streams.Count, startIndex + Limit);
for (var i = startIndex; i < endIndex; i++) 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; 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; const int InsertAtOnce = 10;
var insertText = new StringBuilder(_mediaAttachmentInsertPrefix);
for (var startIndex = 0; startIndex < attachments.Count; startIndex += InsertAtOnce) for (var startIndex = 0; startIndex < attachments.Count; startIndex += InsertAtOnce)
{ {
var insertText = new StringBuilder(_mediaAttachmentInsertPrefix);
var endIndex = Math.Min(attachments.Count, startIndex + InsertAtOnce); var endIndex = Math.Min(attachments.Count, startIndex + InsertAtOnce);
for (var i = startIndex; i < endIndex; i++) 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.Reset();
statement.MoveNext(); statement.MoveNext();
} }
insertText.Length = _mediaAttachmentInsertPrefix.Length;
} }
} }

@ -195,8 +195,9 @@ namespace MediaBrowser.Api.Playback.Hls
// Main stream // Main stream
builder.Append("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=") builder.Append("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=")
.AppendLine(paddedBitrate.ToString(CultureInfo.InvariantCulture)); .AppendLine(paddedBitrate.ToString(CultureInfo.InvariantCulture))
var playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8"); .Append("hls/");
var playlistUrl = Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8");
builder.AppendLine(playlistUrl); builder.AppendLine(playlistUrl);
return builder.ToString(); return builder.ToString();

@ -1037,7 +1037,7 @@ namespace MediaBrowser.Api.Playback.Hls
} }
audioTranscodeParams.Add("-vn"); audioTranscodeParams.Add("-vn");
return string.Join(" ", audioTranscodeParams.ToArray()); return string.Join(" ", audioTranscodeParams);
} }
if (EncodingHelper.IsCopyCodec(audioCodec)) if (EncodingHelper.IsCopyCodec(audioCodec))

@ -160,8 +160,6 @@ namespace MediaBrowser.Api.Subtitles
var mediaSource = await _mediaSourceManager.GetMediaSource(item, request.MediaSourceId, null, false, CancellationToken.None).ConfigureAwait(false); var mediaSource = await _mediaSourceManager.GetMediaSource(item, request.MediaSourceId, null, false, CancellationToken.None).ConfigureAwait(false);
var builder = new StringBuilder();
var runtime = mediaSource.RunTimeTicks ?? -1; var runtime = mediaSource.RunTimeTicks ?? -1;
if (runtime <= 0) 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)"); 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") builder.AppendLine("#EXTM3U")
.Append("#EXT-X-TARGETDURATION:") .Append("#EXT-X-TARGETDURATION:")
.AppendLine(request.SegmentLength.ToString(CultureInfo.InvariantCulture)) .AppendLine(request.SegmentLength.ToString(CultureInfo.InvariantCulture))

@ -233,7 +233,7 @@ namespace MediaBrowser.Model.Entities
if (!string.IsNullOrEmpty(Title)) if (!string.IsNullOrEmpty(Title))
{ {
var result = new StringBuilder(Title); var result = new StringBuilder(Title);
foreach (var tag in attributes) foreach (var tag in attributes)
{ {
// Keep Tags that are not already in Title. // Keep Tags that are not already in Title.
@ -246,7 +246,7 @@ namespace MediaBrowser.Model.Entities
return result.ToString(); return result.ToString();
} }
return string.Join(" - ", attributes.ToArray()); return string.Join(" - ", attributes);
} }
default: default:

@ -125,7 +125,7 @@ namespace MediaBrowser.Providers.Manager
// If there are more than one output paths, the stream will need to be seekable // If there are more than one output paths, the stream will need to be seekable
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
using (source) await using (source.ConfigureAwait(false))
{ {
await source.CopyToAsync(memoryStream).ConfigureAwait(false); await source.CopyToAsync(memoryStream).ConfigureAwait(false);
} }
@ -138,7 +138,7 @@ namespace MediaBrowser.Providers.Manager
var savedPaths = new List<string>(); var savedPaths = new List<string>();
await using (source) await using (source.ConfigureAwait(false))
{ {
var currentPathIndex = 0; var currentPathIndex = 0;

@ -6,9 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj" />
<ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj" /> <ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj" />
<ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj" />
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>

Loading…
Cancel
Save