-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
36f9f4e
Bind / sort proto type
RolandPheasant 56fc77a
BindAndSort operator
RolandPheasant 5a89976
Make style cop happy
RolandPheasant 2e12b7c
Make style cop happy
RolandPheasant 1f30d61
Fix tests
RolandPheasant cbf5520
Change BindAndSort to SortAndBind + respond to PR feedback
RolandPheasant 30b406e
Merge branch 'main' into feature/BindSort
RolandPheasant fd169a9
Revert accidental check in
RolandPheasant 01d1361
Remove unused code
RolandPheasant 96d0ba2
Get rid of ResetOnFirstTimeLoad, and locking for SortAndBind + tests …
RolandPheasant ea9b004
Get rid of ResetOnFirstTimeLoad, and locking for SortAndBind + tests …
RolandPheasant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System; | ||
using System.Linq; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Subjects; | ||
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); | ||
|
||
|
||
Subject<IChangeSet<Item, int>> _oldSubject = new(); | ||
Subject<IChangeSet<Item, int>> _newSubject = new(); | ||
|
||
private IDisposable? _cleanUp; | ||
private ChangeSet<Item, int>? _changeSet; | ||
|
||
|
||
[Params(10, 100, 1_000, 10_000, 50_000)] | ||
public int Count { get; set; } | ||
|
||
|
||
[GlobalSetup] | ||
public void SetUp() | ||
{ | ||
_oldSubject = new Subject<IChangeSet<Item, int>>(); | ||
_newSubject = new Subject<IChangeSet<Item, int>>(); | ||
|
||
var changeSet = new ChangeSet<Item, int>(Count); | ||
foreach (var i in Enumerable.Range(1, Count)) | ||
{ | ||
var item = new Item($"Item{i}", i, _random.Next(1, 1000)); | ||
changeSet.Add(new Change<Item, int>(ChangeReason.Add, i, item)); | ||
} | ||
|
||
_changeSet = changeSet; | ||
|
||
_cleanUp = new CompositeDisposable | ||
( | ||
_newSubject.BindAndSort(out var list1, _comparer).Subscribe(), | ||
_oldSubject.Sort(_comparer).Bind(out var list2).Subscribe() | ||
); | ||
} | ||
|
||
|
||
[Benchmark(Baseline = true)] | ||
public void Old() => _oldSubject.OnNext(_changeSet!); | ||
|
||
[Benchmark] | ||
public void New() => _newSubject.OnNext(_changeSet!); | ||
|
||
public void Dispose() => _cleanUp?.Dispose(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Subjects; | ||
using BenchmarkDotNet.Attributes; | ||
using DynamicData.Binding; | ||
|
||
namespace DynamicData.Benchmarks.Cache; | ||
|
||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class BindAndSortChange: IDisposable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
Subject<IChangeSet<Item, int>> _oldSubject = new(); | ||
Subject<IChangeSet<Item, int>> _newSubject = new(); | ||
|
||
private IDisposable? _cleanUp; | ||
|
||
private ReadOnlyObservableCollection<Item>? _newList; | ||
private ReadOnlyObservableCollection<Item>? _oldList; | ||
|
||
|
||
[Params(10, 100, 1_000, 10_000, 50_000)] | ||
public int Count { get; set; } | ||
|
||
|
||
[GlobalSetup] | ||
public void SetUp() | ||
{ | ||
_oldSubject = new Subject<IChangeSet<Item, int>>(); | ||
_newSubject = new Subject<IChangeSet<Item, int>>(); | ||
|
||
var options = BindAndSortOptions.Default with | ||
{ | ||
InitialCapacity = Count, | ||
UseBinarySearch = true | ||
}; | ||
|
||
_cleanUp = new CompositeDisposable | ||
( | ||
_newSubject.BindAndSort(out var list1, _comparer).Subscribe(), | ||
_oldSubject.Sort(_comparer).Bind(out var list2).Subscribe() | ||
); | ||
|
||
_newList = list1; | ||
_oldList = list2; | ||
|
||
|
||
|
||
var changeSet = new ChangeSet<Item, int>(Count); | ||
foreach (var i in Enumerable.Range(1, Count)) | ||
{ | ||
var item = new Item($"Item{i}", i, _random.Next(1, 1000)); | ||
changeSet.Add(new Change<Item, int>(ChangeReason.Add, i, item)); | ||
} | ||
|
||
_newSubject.OnNext(changeSet); | ||
_oldSubject.OnNext(changeSet); | ||
|
||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void Old() | ||
{ | ||
var original = _oldList![Count / 2]; | ||
var updated = original with { Ranking = _random.Next(1, 1000) }; | ||
|
||
_oldSubject.OnNext(new ChangeSet<Item, int> | ||
{ | ||
new(ChangeReason.Update, original.Id, updated, original) | ||
}); | ||
} | ||
|
||
[Benchmark] | ||
public void New() | ||
{ | ||
var original = _newList![Count / 2]; | ||
var updated = original with { Ranking = _random.Next(1, 1000) }; | ||
|
||
_newSubject.OnNext(new ChangeSet<Item, int> | ||
{ | ||
new(ChangeReason.Update, original.Id, updated, original) | ||
}); | ||
} | ||
|
||
public void Dispose() => _cleanUp?.Dispose(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BindAndSort_Initial
to matchBindAndSort_Change
?