-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/stateless filtering and transforming (#823)
* Fixed a build defect, present only for Debug builds, within Cache/TransformManyAsyncFixture * Adjusted .editorconfig to allow for multi-line expressions to be passed as method parameters. * Adjusted .editorconfig to re-allow omission of brackets for single-line code blocks. * Consolidated language settings across all projects, and added polyfills to .Benchmarks, to match the other two. * Added new operators `.FilterImmutable()` and `.TransformImmutable()` to provide better performance for static/deterministic scenarios.
- Loading branch information
1 parent
38c6a38
commit d6d748e
Showing
19 changed files
with
1,400 additions
and
11 deletions.
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
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
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,136 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reactive.Subjects; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace DynamicData.Benchmarks.Cache; | ||
|
||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class FilterImmutable | ||
{ | ||
private readonly IReadOnlyList<IChangeSet<Item, int>> _addChangeSets; | ||
private readonly IReadOnlyList<IChangeSet<Item, int>> _replaceChangeSets; | ||
private readonly IReadOnlyList<IChangeSet<Item, int>> _removeChangeSets; | ||
|
||
public FilterImmutable() | ||
{ | ||
var source = new ChangeAwareCache<Item, int>(capacity: 1_000); | ||
|
||
var addChangeSets = new List<IChangeSet<Item, int>>(capacity: 1_000); | ||
for (var id = 1; id <= 1_000; ++id) | ||
{ | ||
source.Add( | ||
item: new Item() | ||
{ | ||
Id = id, | ||
IsIncluded = (id % 2) == 0 | ||
}, | ||
key: id); | ||
addChangeSets.Add(source.CaptureChanges()); | ||
} | ||
_addChangeSets = addChangeSets; | ||
|
||
var replaceChangeSets = new List<IChangeSet<Item, int>>(capacity: 500); | ||
for (var id = 2; id <= 1_000; id += 2) | ||
{ | ||
source.AddOrUpdate( | ||
item: new Item() | ||
{ | ||
Id = id, | ||
IsIncluded = (id % 4) != 0 | ||
}, | ||
key: id); | ||
replaceChangeSets.Add(source.CaptureChanges()); | ||
} | ||
_replaceChangeSets = replaceChangeSets; | ||
|
||
var removeChangeSets = new List<IChangeSet<Item, int>>(capacity: 1_000); | ||
for (var id = 1; id <= 1_000; ++id) | ||
{ | ||
source.Remove(id); | ||
removeChangeSets.Add(source.CaptureChanges()); | ||
} | ||
_removeChangeSets = removeChangeSets; | ||
} | ||
|
||
[Benchmark] | ||
public void Adds() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.FilterImmutable(static item => item.IsIncluded) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _addChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
source.OnCompleted(); | ||
} | ||
|
||
[Benchmark] | ||
public void AddsAndReplacements() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.FilterImmutable(static item => item.IsIncluded) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _addChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
foreach (var changeSet in _replaceChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
source.OnCompleted(); | ||
} | ||
|
||
[Benchmark] | ||
public void AddsAndRemoves() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.FilterImmutable(static item => item.IsIncluded) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _addChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
foreach (var changeSet in _removeChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
source.OnCompleted(); | ||
} | ||
|
||
[Benchmark] | ||
public void AddsReplacementsAndRemoves() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.FilterImmutable(static item => item.IsIncluded) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _addChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
foreach (var changeSet in _replaceChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
foreach (var changeSet in _removeChangeSets) | ||
source.OnNext(changeSet); | ||
|
||
source.OnCompleted(); | ||
} | ||
|
||
private sealed class Item | ||
{ | ||
public required int Id { get; init; } | ||
|
||
public required bool IsIncluded { get; init; } | ||
} | ||
} |
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,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reactive.Subjects; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace DynamicData.Benchmarks.Cache; | ||
|
||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class StatelessFiltering | ||
{ | ||
private readonly IReadOnlyList<IChangeSet<Item, int>> _changeSets; | ||
|
||
public StatelessFiltering() | ||
{ | ||
var source = new ChangeAwareCache<Item, int>(capacity: 1_000); | ||
var changeSets = new List<IChangeSet<Item, int>>(capacity: 2_500); | ||
|
||
for (var id = 1; id <= 1_000; ++id) | ||
{ | ||
source.Add( | ||
item: new Item() | ||
{ | ||
Id = id, | ||
IsIncluded = (id % 2) == 0 | ||
}, | ||
key: id); | ||
changeSets.Add(source.CaptureChanges()); | ||
} | ||
|
||
for (var id = 2; id <= 1_000; id += 2) | ||
{ | ||
source.AddOrUpdate( | ||
item: new Item() | ||
{ | ||
Id = id, | ||
IsIncluded = (id % 4) != 0 | ||
}, | ||
key: id); | ||
changeSets.Add(source.CaptureChanges()); | ||
} | ||
|
||
for (var id = 1; id <= 1_000; ++id) | ||
{ | ||
source.Remove(id); | ||
changeSets.Add(source.CaptureChanges()); | ||
} | ||
|
||
_changeSets = changeSets; | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void Filter() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.Filter(static item => item.IsIncluded) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _changeSets) | ||
source.OnNext(changeSet); | ||
source.OnCompleted(); | ||
} | ||
|
||
[Benchmark] | ||
public void FilterImmutable() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.FilterImmutable(static item => item.IsIncluded) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _changeSets) | ||
source.OnNext(changeSet); | ||
source.OnCompleted(); | ||
} | ||
|
||
private sealed class Item | ||
{ | ||
public required int Id { get; init; } | ||
|
||
public required bool IsIncluded { get; init; } | ||
} | ||
} |
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,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Subjects; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace DynamicData.Benchmarks.Cache; | ||
|
||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class StatelessTransforming | ||
{ | ||
private readonly IReadOnlyList<IChangeSet<Item, int>> _changeSets; | ||
|
||
public StatelessTransforming() | ||
{ | ||
var source = new ChangeAwareCache<Item, int>(capacity: 1_000); | ||
var changeSets = new List<IChangeSet<Item, int>>(capacity: 2_500); | ||
|
||
for (var id = 1; id <= 1_000; ++id) | ||
{ | ||
source.Add( | ||
item: new Item() | ||
{ | ||
Id = id, | ||
Name = $"Item #{id}" | ||
}, | ||
key: id); | ||
changeSets.Add(source.CaptureChanges()); | ||
} | ||
|
||
for (var id = 2; id <= 1_000; id += 2) | ||
{ | ||
source.AddOrUpdate( | ||
item: new Item() | ||
{ | ||
Id = id, | ||
Name = $"Replacement Item #{id}" | ||
}, | ||
key: id); | ||
changeSets.Add(source.CaptureChanges()); | ||
} | ||
|
||
for (var id = 1; id <= 1_000; ++id) | ||
{ | ||
source.Remove(id); | ||
changeSets.Add(source.CaptureChanges()); | ||
} | ||
|
||
_changeSets = changeSets; | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void Transform() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.Transform(static item => item.Name) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _changeSets) | ||
source.OnNext(changeSet); | ||
source.OnCompleted(); | ||
} | ||
|
||
[Benchmark] | ||
public void TransformImmutable() | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.TransformImmutable(static item => item.Name) | ||
.Subscribe(); | ||
|
||
foreach (var changeSet in _changeSets) | ||
source.OnNext(changeSet); | ||
source.OnCompleted(); | ||
} | ||
|
||
private sealed class Item | ||
{ | ||
public required int Id { get; init; } | ||
|
||
public required string Name { get; init; } | ||
} | ||
} |
Oops, something went wrong.