-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6450 from smoogipoo/bindable-perf-improvement-2
Improve `BindableList` performance + reduce allocations
- Loading branch information
Showing
2 changed files
with
89 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,87 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using osu.Framework.Bindables; | ||
|
||
namespace osu.Framework.Benchmarks | ||
{ | ||
[MemoryDiagnoser] | ||
[WarmupCount(0)] | ||
[IterationCount(2)] | ||
public class BenchmarkBindableList | ||
{ | ||
private readonly BindableList<int> list = new BindableList<int>(); | ||
private IBindableList<int> iList => list; | ||
private static readonly int[] small_data = Enumerable.Range(0, 10).ToArray(); | ||
|
||
[Params(0, 1, 10, 20)] | ||
public int NumBindings { get; set; } | ||
|
||
private BindableList<int>[] lists = null!; | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
for (int i = 0; i < 10; i++) | ||
list.Add(i); | ||
lists = new BindableList<int>[NumBindings + 1]; | ||
|
||
lists[0] = new BindableList<int>(Enumerable.Range(0, 10000).ToArray()); | ||
for (int i = 1; i < lists.Length; i++) | ||
lists[i] = lists[i - 1].GetBoundCopy(); | ||
lists[0].Clear(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void Create() | ||
{ | ||
setupList(); | ||
} | ||
|
||
[Benchmark] | ||
public int Enumerate() | ||
public void Add() | ||
{ | ||
int result = 0; | ||
setupList().Add(1); | ||
} | ||
|
||
for (int i = 0; i < 100; i++) | ||
{ | ||
foreach (int val in list) | ||
result += val; | ||
} | ||
[Benchmark] | ||
public void Remove() | ||
{ | ||
setupList().Remove(0); | ||
} | ||
|
||
return result; | ||
[Benchmark] | ||
public void Clear() | ||
{ | ||
setupList().Clear(); | ||
} | ||
|
||
[Benchmark] | ||
public int EnumerateInterface() | ||
public void AddRange() | ||
{ | ||
setupList().AddRange(small_data); | ||
} | ||
|
||
[Benchmark] | ||
public void SetIndex() | ||
{ | ||
setupList()[0]++; | ||
} | ||
|
||
[Benchmark] | ||
public int Enumerate() | ||
{ | ||
int result = 0; | ||
|
||
for (int i = 0; i < 100; i++) | ||
{ | ||
foreach (int val in iList) | ||
result += val; | ||
} | ||
foreach (int val in setupList()) | ||
result += val; | ||
|
||
return result; | ||
} | ||
|
||
private BindableList<int> setupList() | ||
{ | ||
lists[0].Clear(); | ||
lists[0].Add(0); | ||
return lists[0]; | ||
} | ||
} | ||
} |
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