Skip to content

Commit

Permalink
Some work on #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Genbox committed Apr 13, 2017
1 parent 6128076 commit 9331d48
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
9 changes: 9 additions & 0 deletions SourceFiles/Primitives/Optimization/IPoolable.cs
@@ -0,0 +1,9 @@
using System;

namespace VelcroPhysics.Primitives.Optimization
{
public interface IPoolable : IDisposable
{
void Reset();
}
}
51 changes: 51 additions & 0 deletions SourceFiles/Utils/Benchmark.cs
@@ -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);
}
}
}
39 changes: 39 additions & 0 deletions SourceFiles/Utils/BenchmarkRun.cs
@@ -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();
}
}
}
80 changes: 80 additions & 0 deletions SourceFiles/Utils/Pool.cs
@@ -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);
}
}
}
}

0 comments on commit 9331d48

Please sign in to comment.