Timing: Optimize Timestamp Aquisition (#479)

* Timing: Optimize Timestamp Aquisition

Currently, we make use of Environment.TickCount in a number of places. This has some downsides, mainly being that the TickCount is a signed 32-bit integer, and has an effective limit of ~25 days before overflowing and wrapping around. Due to the signed-ness of the value, this also caused issues with negative numbers. This resolves these issues by using a 64-bit tick count obtained from Performance Counters (via the Stopwatch class). This has a beneficial side effect of being significantly more accurate than the TickCount.

* Timing: Rename ElapsedTicks to ElapsedMilliseconds and expose TicksPerX

* Timing: Some style changes

* Timing: Align static variable initialization
This commit is contained in:
jduncanator 2018-10-29 09:31:13 +11:00 committed by gdkchan
parent d69f900f29
commit c2aaf78d4c

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
@ -27,7 +28,7 @@ namespace ChocolArm64
public int Size { get; private set; } public int Size { get; private set; }
public int Timestamp { get; private set; } public long Timestamp { get; private set; }
public CacheBucket(ATranslatedSub Subroutine, LinkedListNode<long> Node, int Size) public CacheBucket(ATranslatedSub Subroutine, LinkedListNode<long> Node, int Size)
{ {
@ -41,7 +42,7 @@ namespace ChocolArm64
{ {
this.Node = Node; this.Node = Node;
Timestamp = Environment.TickCount; Timestamp = GetTimestamp();
} }
} }
@ -122,7 +123,7 @@ namespace ChocolArm64
private void ClearCacheIfNeeded() private void ClearCacheIfNeeded()
{ {
int Timestamp = Environment.TickCount; long Timestamp = GetTimestamp();
while (TotalSize > MaxTotalSize) while (TotalSize > MaxTotalSize)
{ {
@ -137,9 +138,9 @@ namespace ChocolArm64
CacheBucket Bucket = Cache[Node.Value]; CacheBucket Bucket = Cache[Node.Value];
int TimeDelta = RingDelta(Bucket.Timestamp, Timestamp); long TimeDelta = Bucket.Timestamp - Timestamp;
if ((uint)TimeDelta <= (uint)MinTimeDelta) if (TimeDelta <= MinTimeDelta)
{ {
break; break;
} }
@ -154,16 +155,11 @@ namespace ChocolArm64
} }
} }
private static int RingDelta(int Old, int New) private static long GetTimestamp()
{ {
if ((uint)New < (uint)Old) long timestamp = Stopwatch.GetTimestamp();
{
return New + (~Old + 1); return timestamp / (Stopwatch.Frequency / 1000);
}
else
{
return New - Old;
}
} }
} }
} }