Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace VelcroPhysics.Primitives.Optimization | ||
{ | ||
public interface IPoolable : IDisposable | ||
{ | ||
void Reset(); | ||
} | ||
} |
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,51 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace VelcroPhysics.Utils | ||
{ | ||
public static class Benchmark | ||
{ | ||
private static readonly Dictionary<string, long> _results = new Dictionary<string, long>(); | ||
private static readonly Pool<BenchmarkRun> _runPool = new Pool<BenchmarkRun>(1); | ||
|
||
public static BenchmarkRun StartRun(string area) | ||
{ | ||
BenchmarkRun run = _runPool.GetFromPool(); | ||
run.SetData(area); | ||
return run; | ||
} | ||
|
||
public static void RecordResults(string area, long results) | ||
{ | ||
if (!Settings.EnableDiagnostics) | ||
return; | ||
|
||
if (!_results.ContainsKey(area)) | ||
_results.Add(area, results); | ||
else | ||
_results[area] += results; | ||
} | ||
|
||
public static long ResetResults(string area) | ||
{ | ||
if (!Settings.EnableDiagnostics) | ||
return 0; | ||
|
||
long oldVal = _results[area]; | ||
_results[area] = 0; | ||
return oldVal; | ||
} | ||
|
||
public static long GetResults(string area) | ||
{ | ||
if (!Settings.EnableDiagnostics) | ||
return 0; | ||
|
||
return _results.TryGetValue(area, out long value) ? value : 0; | ||
} | ||
|
||
internal static void ReturnToPool(BenchmarkRun run) | ||
{ | ||
_runPool.ReturnToPool(run); | ||
} | ||
} | ||
} |
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,39 @@ | ||
using System.Diagnostics; | ||
using VelcroPhysics.Primitives.Optimization; | ||
|
||
namespace VelcroPhysics.Utils | ||
{ | ||
public class BenchmarkRun : IPoolable | ||
{ | ||
private string _area; | ||
private readonly Stopwatch _stopwatch = new Stopwatch(); | ||
private bool _resultsRecorded; | ||
|
||
public void SetData(string area) | ||
{ | ||
_area = area; | ||
_stopwatch.Start(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
RecordResults(); | ||
Benchmark.ReturnToPool(this); | ||
} | ||
|
||
public void RecordResults() | ||
{ | ||
if (_resultsRecorded) | ||
return; | ||
|
||
_stopwatch.Stop(); | ||
Benchmark.RecordResults(_area, _stopwatch.ElapsedTicks); | ||
_resultsRecorded = true; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_stopwatch.Reset(); | ||
} | ||
} | ||
} |
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,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using VelcroPhysics.Primitives.Optimization; | ||
|
||
namespace VelcroPhysics.Utils | ||
{ | ||
public class Pool<T> where T : class, IPoolable, new() | ||
{ | ||
private readonly Queue<T> _queue; | ||
|
||
public Pool(int capacity, bool createInstances = true) | ||
{ | ||
_queue = new Queue<T>(capacity); | ||
|
||
if (createInstances) | ||
{ | ||
for (int i = 0; i < capacity; i++) | ||
{ | ||
_queue.Enqueue(new T()); | ||
} | ||
} | ||
} | ||
|
||
public T GetFromPool() | ||
{ | ||
if (_queue.Count <= 0) | ||
return new T(); | ||
|
||
T obj = _queue.Dequeue(); | ||
obj.Reset(); | ||
return obj; | ||
} | ||
|
||
public IEnumerable<T> GetManyFromPool(int count) | ||
{ | ||
int diff = count - _queue.Count; | ||
|
||
if (diff >= 0) | ||
{ | ||
//We have all in queue | ||
for (int i = 0; i < diff; i++) | ||
{ | ||
T obj = _queue.Dequeue(); | ||
obj.Reset(); | ||
yield return obj; | ||
} | ||
} | ||
else | ||
{ | ||
//We need to return what is in queue and then make more | ||
for (int i = 0; i < _queue.Count; i++) | ||
{ | ||
T obj = _queue.Dequeue(); | ||
obj.Reset(); | ||
yield return obj; | ||
} | ||
|
||
int remaining = Math.Abs(diff); | ||
|
||
for (int i = 0; i < remaining; i++) | ||
{ | ||
yield return new T(); | ||
} | ||
} | ||
} | ||
|
||
public void ReturnToPool(T obj) | ||
{ | ||
_queue.Enqueue(obj); | ||
} | ||
|
||
public void ReturnToPool(IEnumerable<T> objs) | ||
{ | ||
foreach (T obj in objs) | ||
{ | ||
_queue.Enqueue(obj); | ||
} | ||
} | ||
} | ||
} |