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

New SortAndBind operator #878

Merged
merged 11 commits into from
Mar 20, 2024
58 changes: 58 additions & 0 deletions src/DynamicData.Benchmarks/Cache/BindAndSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Linq;
using System.Reactive.Disposables;
using DynamicData.Binding;

namespace DynamicData.Benchmarks.Cache;

[MemoryDiagnoser]
[MarkdownExporterAttribute.GitHub]
public class BindAndSortInitial: IDisposable
{
private readonly Random _random = new();

private record Item(string Name, int Id, int Ranking);

private readonly SortExpressionComparer<Item> _comparer = SortExpressionComparer<Item>.Ascending(i => i.Ranking).ThenByAscending(i => i.Name);


private ISourceCache<Item, int> _sourceOld = null!;
private ISourceCache<Item, int> _sourceNew = null!;

private IDisposable? _cleanUp;

private Item[] _items = null!;


[Params(100, 1_000, 10_000, 50_000)]
public int Count { get; set; }


[GlobalSetup]
public void SetUp()
{
_sourceOld = new SourceCache<Item, int>(i => i.Id);
_sourceNew = new SourceCache<Item, int>(i => i.Id);


_items = Enumerable.Range(1, Count)
.Select(i => new Item($"Item{i}", i, _random.Next(1, 1000)))
.ToArray();

_cleanUp = new CompositeDisposable
(
_sourceNew.Connect().BindAndSort(out var list1, _comparer).Subscribe(),
_sourceOld.Connect().Sort(_comparer).Bind(out var list2).Subscribe()
);
}


[Benchmark(Baseline = true)]
public void Old() => _sourceOld.AddOrUpdate(_items);

[Benchmark]
public void New() => _sourceNew.AddOrUpdate(_items);

public void Dispose() => _cleanUp?.Dispose();
}
57 changes: 57 additions & 0 deletions src/DynamicData.Benchmarks/Cache/BindAndSort_Change.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Linq;
using System.Reactive.Disposables;
using BenchmarkDotNet.Attributes;
using DynamicData.Binding;

namespace DynamicData.Benchmarks.Cache;

[MemoryDiagnoser]
[MarkdownExporterAttribute.GitHub]
public class BindAndSortChange: IDisposable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class name doesn't match file name.

{
private readonly Random _random = new();
private record Item(string Name, int Id, int Ranking);

private readonly SortExpressionComparer<Item> _comparer = SortExpressionComparer<Item>.Ascending(i => i.Ranking).ThenByAscending(i => i.Name);


private ISourceCache<Item, int> _sourceOld = null!;
private ISourceCache<Item, int> _sourceNew = null!;

private IDisposable? _cleanUp;


[Params(100, 1_000, 10_000, 50_000)]
public int Count { get; set; }


[GlobalSetup]
public void SetUp()
{
_sourceOld = new SourceCache<Item, int>(i => i.Id);
_sourceNew = new SourceCache<Item, int>(i => i.Id);


var items = Enumerable.Range(1, Count)
.Select(i => new Item($"Item{i}", i, _random.Next(1, 1000)))
.ToArray();

_sourceNew.AddOrUpdate(items);
_sourceOld.AddOrUpdate(items);

_cleanUp = new CompositeDisposable
(
_sourceNew.Connect().BindAndSort(out var list1, _comparer).Subscribe(),
_sourceOld.Connect().Sort(_comparer).Bind(out var list2).Subscribe()
);
}

[Benchmark(Baseline = true)]
public void Old() => _sourceOld.AddOrUpdate(new Item("A new item", 50, 500));

[Benchmark]
public void New() => _sourceNew.AddOrUpdate(new Item("A new item", 50, 500));

public void Dispose() => _cleanUp?.Dispose();
}
Loading