Skip to content

Commit

Permalink
Optimize (?) ScreenshotsLibrary.ClearExceed
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Aug 24, 2024
1 parent 7bae77d commit a651720
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
27 changes: 27 additions & 0 deletions SightKeeper.Domain/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,31 @@ public static ImmutableHashSet<T> ToImmutableHashSetThrowOnDuplicate<T>(this IEn
Guard.IsTrue(enumerable.All(tagsBuilder.Add));
return tagsBuilder.ToImmutable();
}

public static IEnumerable<(ImmutableArray<T>, int start, int end)> ToRanges<T>(this IEnumerable<T> enumerable, Func<T, int> indexSelector)
{
using var enumerator = enumerable.GetEnumerator();
if (!enumerator.MoveNext())
yield break;
var initialItem = enumerator.Current;
int previousIndex = indexSelector(initialItem);
int currentRangeStart = previousIndex;
var itemsBuilder = ImmutableArray.CreateBuilder<T>();
itemsBuilder.Add(initialItem);
while (enumerator.MoveNext())
{
var item = enumerator.Current;
var index = indexSelector(item);
if (index != previousIndex + 1)
{
yield return (itemsBuilder.DrainToImmutable(), currentRangeStart, previousIndex);
currentRangeStart = index;
}
itemsBuilder.Add(item);
previousIndex = index;
}

if (itemsBuilder.Count != 0)
yield return (itemsBuilder.ToImmutable(), currentRangeStart, previousIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public ScreenshotsLibrary(DataSet dataSet)
public override Screenshot<TAsset> CreateScreenshot(DateTime creationDate, out ImmutableArray<Screenshot> removedScreenshots)
{
Screenshot<TAsset> screenshot = new(this, creationDate);
Guard.IsTrue(_screenshots.Add(screenshot));
Guard.IsGreaterThan(creationDate, _screenshots[^1].CreationDate);
_screenshots.Add(screenshot);
removedScreenshots = ClearExceed();
return screenshot;
}
Expand All @@ -60,14 +61,22 @@ private ImmutableArray<Screenshot> ClearExceed()
if (exceedAmount <= 0)
return ImmutableArray<Screenshot>.Empty;
var builder = ImmutableArray.CreateBuilder<Screenshot>(exceedAmount);
var screenshotsToDelete = _screenshots.Where(screenshot => screenshot.Asset == null).Take(exceedAmount);
foreach (var screenshot in screenshotsToDelete)
var screenshotsToDelete =
_screenshots
.Select((screenshot, index) => (screenshot, index))
.Where(tuple => tuple.screenshot.Asset == null)
.Take(exceedAmount)
.ToRanges(tuple => tuple.index)
.OrderByDescending(tuple => tuple.start);
foreach (var (tuples, start, end) in screenshotsToDelete)
{
Guard.IsTrue(_screenshots.Remove(screenshot));
builder.Add(screenshot);
_screenshots.RemoveRange(start, end);
builder.Capacity += tuples.Length;
foreach (var (screenshot, _) in tuples)
builder.Add(screenshot);
}
return builder.ToImmutable();
}

private readonly SortedSet<Screenshot<TAsset>> _screenshots = new(ScreenshotsDateComparer.Instance);
private readonly List<Screenshot<TAsset>> _screenshots = new();
}

0 comments on commit a651720

Please sign in to comment.