-
-
Notifications
You must be signed in to change notification settings - Fork 192
Feat: sum benchmarks #1118
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
Open
giard-alexandre
wants to merge
3
commits into
reactivemarbles:main
Choose a base branch
from
giard-alexandre:feat/sum-benchmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−0
Open
Feat: sum benchmarks #1118
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,108 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reactive.Subjects; | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
|
|
||
| using DynamicData.Aggregation; | ||
|
|
||
| namespace DynamicData.Benchmarks.Cache; | ||
|
|
||
| [MemoryDiagnoser] | ||
| [MarkdownExporterAttribute.GitHub] | ||
| public class Sum_Cache | ||
| { | ||
| private IReadOnlyList<IChangeSet<Item, int>> _addChangeSets = null!; | ||
| private IReadOnlyList<IChangeSet<Item, int>> _replaceChangeSets = null!; | ||
| private IReadOnlyList<IChangeSet<Item, int>> _removeChangeSets = null!; | ||
| private IReadOnlyList<IChangeSet<Item, int>> _refreshChangeSets = null!; | ||
|
|
||
| [Params(100, 500, 1_000, 10_000)] | ||
| public int Count { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| var source = new ChangeAwareCache<Item, int>(capacity: Count); | ||
| var items = new Item[Count + 1]; | ||
|
|
||
| var addChangeSets = new List<IChangeSet<Item, int>>(capacity: Count); | ||
| for (var id = 1; id <= Count; ++id) | ||
| { | ||
| var item = new Item() | ||
| { | ||
| Id = id, | ||
| Value = id | ||
| }; | ||
| items[id] = item; | ||
| source.Add(item, key: id); | ||
| addChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _addChangeSets = addChangeSets; | ||
|
|
||
| var replaceChangeSets = new List<IChangeSet<Item, int>>(capacity: Count); | ||
| for (var id = 1; id <= Count; ++id) | ||
| { | ||
| var replacement = new Item() | ||
| { | ||
| Id = id, | ||
| Value = id * 2 | ||
| }; | ||
| items[id] = replacement; | ||
| source.AddOrUpdate(replacement, key: id); | ||
| replaceChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _replaceChangeSets = replaceChangeSets; | ||
|
|
||
| var refreshChangeSets = new List<IChangeSet<Item, int>>(capacity: Count); | ||
| for (var id = 1; id <= Count; ++id) | ||
| { | ||
| // Mutate in place, then refresh - the scenario stateless aggregation cannot currently observe. | ||
| items[id].Value += 1; | ||
| source.Refresh(id); | ||
| refreshChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _refreshChangeSets = refreshChangeSets; | ||
|
|
||
| var removeChangeSets = new List<IChangeSet<Item, int>>(capacity: Count); | ||
| for (var id = 1; id <= Count; ++id) | ||
| { | ||
| source.Remove(id); | ||
| removeChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _removeChangeSets = removeChangeSets; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void Adds() => Run(_addChangeSets); | ||
|
|
||
| [Benchmark] | ||
| public void Replaces() => Run(_replaceChangeSets); | ||
|
|
||
| [Benchmark] | ||
| public void Refreshes() => Run(_refreshChangeSets); | ||
|
|
||
| [Benchmark] | ||
| public void Removes() => Run(_removeChangeSets); | ||
|
|
||
| private static void Run(IReadOnlyList<IChangeSet<Item, int>> changeSets) | ||
| { | ||
| using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
|
||
| using var subscription = source | ||
| .Sum(static item => item.Value) | ||
| .Subscribe(); | ||
|
|
||
| foreach (var changeSet in changeSets) | ||
| source.OnNext(changeSet); | ||
|
|
||
| source.OnCompleted(); | ||
| } | ||
|
|
||
| private sealed class Item | ||
| { | ||
| public required int Id { get; init; } | ||
|
|
||
| public int Value { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or 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,103 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reactive.Subjects; | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
|
|
||
| using DynamicData.Aggregation; | ||
|
|
||
| namespace DynamicData.Benchmarks.List; | ||
|
|
||
| [MemoryDiagnoser] | ||
| [MarkdownExporterAttribute.GitHub] | ||
| public class Sum_List | ||
| { | ||
| private IReadOnlyList<IChangeSet<Item>> _addChangeSets = null!; | ||
| private IReadOnlyList<IChangeSet<Item>> _replaceChangeSets = null!; | ||
| private IReadOnlyList<IChangeSet<Item>> _removeChangeSets = null!; | ||
| private IReadOnlyList<IChangeSet<Item>> _refreshChangeSets = null!; | ||
|
|
||
| [Params(100, 500, 1_000, 10_000)] | ||
| public int Count { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| var source = new ChangeAwareList<Item>(capacity: Count); | ||
|
|
||
| var addChangeSets = new List<IChangeSet<Item>>(capacity: Count); | ||
| for (var id = 1; id <= Count; ++id) | ||
| { | ||
| source.Add(new Item() | ||
| { | ||
| Id = id, | ||
| Value = id | ||
| }); | ||
| addChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _addChangeSets = addChangeSets; | ||
|
|
||
| var replaceChangeSets = new List<IChangeSet<Item>>(capacity: Count); | ||
| for (var index = 0; index < Count; ++index) | ||
| { | ||
| source[index] = new Item() | ||
| { | ||
| Id = index + 1, | ||
| Value = (index + 1) * 2 | ||
| }; | ||
| replaceChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _replaceChangeSets = replaceChangeSets; | ||
|
|
||
| var refreshChangeSets = new List<IChangeSet<Item>>(capacity: Count); | ||
| for (var index = 0; index < Count; ++index) | ||
| { | ||
| // Mutate in place, then refresh - the scenario stateless aggregation cannot currently observe. | ||
| source[index].Value += 1; | ||
| source.RefreshAt(index); | ||
| refreshChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _refreshChangeSets = refreshChangeSets; | ||
|
|
||
| var removeChangeSets = new List<IChangeSet<Item>>(capacity: Count); | ||
| for (var id = 1; id <= Count; ++id) | ||
| { | ||
| source.RemoveAt(source.Count - 1); | ||
| removeChangeSets.Add(source.CaptureChanges()); | ||
| } | ||
| _removeChangeSets = removeChangeSets; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void Adds() => Run(_addChangeSets); | ||
|
|
||
| [Benchmark] | ||
| public void Replaces() => Run(_replaceChangeSets); | ||
|
|
||
| [Benchmark] | ||
| public void Refreshes() => Run(_refreshChangeSets); | ||
|
|
||
| [Benchmark] | ||
| public void Removes() => Run(_removeChangeSets); | ||
|
|
||
| private static void Run(IReadOnlyList<IChangeSet<Item>> changeSets) | ||
| { | ||
| using var source = new Subject<IChangeSet<Item>>(); | ||
|
|
||
| using var subscription = source | ||
| .Sum(static item => item.Value) | ||
| .Subscribe(); | ||
|
|
||
| foreach (var changeSet in changeSets) | ||
| source.OnNext(changeSet); | ||
|
|
||
| source.OnCompleted(); | ||
| } | ||
|
|
||
| private sealed class Item | ||
| { | ||
| public required int Id { get; init; } | ||
|
|
||
| public int Value { get; set; } | ||
| } | ||
| } |
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.
You've generated an invalid sequence of changes for all of these, except for
_addChangeSets.source.CaptureChanges()also clears out the changes buffer, so when you get around to sending_replaceChangeSetsto your operator, what you've got is nothing butReplacechanges that refer to items that never had anAdd. Apparently, the existing implementation of.Sum()doesn't care, but your new one might.