Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce some allocations with the magic of spans etc. #5938

Merged
merged 7 commits into from May 5, 2021
Merged
Changes from 1 commit
Commits
File filter
Filter file types
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

rename to SplitEnumerator and fix test
  • Loading branch information
cvium committed May 3, 2021
commit c0feb3694b86ccc242a637468070daff9120e631
@@ -1082,7 +1082,7 @@ private void AppendItemImageInfo(StringBuilder bldr, ItemImageInfo image)
}
}

private ItemImageInfo ItemImageInfoFromValueString(ReadOnlySpan<char> value)
internal ItemImageInfo ItemImageInfoFromValueString(ReadOnlySpan<char> value)
{
var nextSegment = value.IndexOf('*');
if (nextSegment == -1)
@@ -1103,7 +1103,7 @@ private ItemImageInfo ItemImageInfoFromValueString(ReadOnlySpan<char> value)
nextSegment = value.IndexOf('*');
if (nextSegment == -1)
{
return null;
nextSegment = value.Length;
}

ReadOnlySpan<char> imageType = value[..nextSegment];
@@ -1128,13 +1128,18 @@ private ItemImageInfo ItemImageInfoFromValueString(ReadOnlySpan<char> value)
{
value = value[(nextSegment + 1)..];
nextSegment = value.IndexOf('*');
if (nextSegment == -1 || nextSegment == value.Length)
{
return image;
}

ReadOnlySpan<char> widthSpan = value[..nextSegment];

value = value[(nextSegment + 1)..];
nextSegment = value.IndexOf('*');
if (nextSegment == -1)
{
return image;
nextSegment = value.Length;
}

ReadOnlySpan<char> heightSpan = value[..nextSegment];
@@ -1146,10 +1151,9 @@ private ItemImageInfo ItemImageInfoFromValueString(ReadOnlySpan<char> value)
image.Height = height;
}

nextSegment += 1;
if (nextSegment < value.Length - 1)
{
value = value[nextSegment..];
value = value[(nextSegment + 1)..];
var length = value.Length;

Span<char> blurHashSpan = stackalloc char[length];
@@ -33,33 +33,33 @@ namespace MediaBrowser.Common.Extensions
/// <summary>
/// Extension class for splitting lines without unnecessary allocations.
/// </summary>
public static class SplitLinesStringExtensions
public static class SplitStringExtensions
{
/// <summary>
/// Creates a new line split enumerator.
/// Creates a new string split enumerator.
/// </summary>
/// <param name="str">The string to split.</param>
/// <param name="separator">The separator to split on.</param>
/// <returns>The enumerator struct.</returns>
[Pure]
public static LineSplitEnumerator SpanSplit(this string str, char separator) => new (str.AsSpan(), separator);
public static SplitEnumerator SpanSplit(this string str, char separator) => new (str.AsSpan(), separator);

/// <summary>
/// Creates a new line split enumerator.
/// Creates a new span split enumerator.
/// </summary>
/// <param name="str">The span to split.</param>
/// <param name="separator">The separator to split on.</param>
/// <returns>The enumerator struct.</returns>
[Pure]
public static LineSplitEnumerator Split(this ReadOnlySpan<char> str, char separator) => new (str, separator);
public static SplitEnumerator Split(this ReadOnlySpan<char> str, char separator) => new (str, separator);

[StructLayout(LayoutKind.Auto)]
public ref struct LineSplitEnumerator
public ref struct SplitEnumerator
{
private readonly char _separator;
private ReadOnlySpan<char> _str;

public LineSplitEnumerator(ReadOnlySpan<char> str, char separator)
public SplitEnumerator(ReadOnlySpan<char> str, char separator)
{
_str = str;
_separator = separator;
@@ -68,7 +68,7 @@ public LineSplitEnumerator(ReadOnlySpan<char> str, char separator)

public ReadOnlySpan<char> Current { get; private set; }

public readonly LineSplitEnumerator GetEnumerator() => this;
public readonly SplitEnumerator GetEnumerator() => this;

public bool MoveNext()
{
@@ -37,7 +37,7 @@ public static IEnumerable<object[]> ItemImageInfoFromValueString_Valid_TestData(
yield return new object[]
{
"/mnt/series/Family Guy/Season 1/Family Guy - S01E01-thumb.jpg*637452096478512963*Primary*1920*1080*WjQbtJtSO8nhNZ%L_Io#R/oaS6o}-;adXAoIn7j[%hW9s:WGw[nN",
new ItemImageInfo()
new ItemImageInfo
{
Path = "/mnt/series/Family Guy/Season 1/Family Guy - S01E01-thumb.jpg",
Type = ImageType.Primary,
@@ -51,7 +51,27 @@ public static IEnumerable<object[]> ItemImageInfoFromValueString_Valid_TestData(
yield return new object[]
{
"https://image.tmdb.org/t/p/original/zhB5CHEgqqh4wnEqDNJLfWXJlcL.jpg*0*Primary*0*0",
new ItemImageInfo()
new ItemImageInfo
{
Path = "https://image.tmdb.org/t/p/original/zhB5CHEgqqh4wnEqDNJLfWXJlcL.jpg",
Type = ImageType.Primary,
}
};

yield return new object[]
{
"https://image.tmdb.org/t/p/original/zhB5CHEgqqh4wnEqDNJLfWXJlcL.jpg*0*Primary",
new ItemImageInfo
{
Path = "https://image.tmdb.org/t/p/original/zhB5CHEgqqh4wnEqDNJLfWXJlcL.jpg",
Type = ImageType.Primary,
}
};

yield return new object[]
{
"https://image.tmdb.org/t/p/original/zhB5CHEgqqh4wnEqDNJLfWXJlcL.jpg*0*Primary*600",
new ItemImageInfo
{
Path = "https://image.tmdb.org/t/p/original/zhB5CHEgqqh4wnEqDNJLfWXJlcL.jpg",
Type = ImageType.Primary,
@@ -61,7 +81,7 @@ public static IEnumerable<object[]> ItemImageInfoFromValueString_Valid_TestData(
yield return new object[]
{
"%MetadataPath%/library/68/68578562b96c80a7ebd530848801f645/poster.jpg*637264380567586027*Primary*600*336",
new ItemImageInfo()
new ItemImageInfo
{
Path = "/meta/data/path/library/68/68578562b96c80a7ebd530848801f645/poster.jpg",
Type = ImageType.Primary,
@@ -76,7 +96,7 @@ public static IEnumerable<object[]> ItemImageInfoFromValueString_Valid_TestData(
[MemberData(nameof(ItemImageInfoFromValueString_Valid_TestData))]
public void ItemImageInfoFromValueString_Valid_Success(string value, ItemImageInfo expected)
{
var result = _sqliteItemRepository.ItemImageInfoFromValueString(value);
var result = _sqliteItemRepository.ItemImageInfoFromValueString(value.AsSpan());
This conversation was marked as resolved by Bond-009

This comment has been minimized.

@Bond-009

Bond-009 May 4, 2021
Member

The AsSpan shouldn't be needed, right?

Assert.Equal(expected.Path, result.Path);
Assert.Equal(expected.Type, result.Type);
Assert.Equal(expected.DateModified, result.DateModified);
@@ -88,9 +108,10 @@ public void ItemImageInfoFromValueString_Valid_Success(string value, ItemImageIn
[Theory]
[InlineData("")]
[InlineData("*")]
[InlineData("https://image.tmdb.org/t/p/original/zhB5CHEgqqh4wnEqDNJLfWXJlcL.jpg*0")]
public void ItemImageInfoFromValueString_Invalid_Null(string value)
{
Assert.Null(_sqliteItemRepository.ItemImageInfoFromValueString(value));
Assert.Null(_sqliteItemRepository.ItemImageInfoFromValueString(value.AsSpan()));
}

public static IEnumerable<object[]> DeserializeImages_Valid_TestData()
ProTip! Use n and p to navigate between commits in a pull request.