ATranslatorCache.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading;
  7. namespace ChocolArm64
  8. {
  9. class ATranslatorCache
  10. {
  11. //Maximum size of the cache, in bytes, measured in ARM code size.
  12. private const int MaxTotalSize = 4 * 1024 * 256;
  13. //Minimum time required in milliseconds for a method to be eligible for deletion.
  14. private const int MinTimeDelta = 2 * 60000;
  15. //Minimum number of calls required to update the timestamp.
  16. private const int MinCallCountForUpdate = 250;
  17. private class CacheBucket
  18. {
  19. public ATranslatedSub Subroutine { get; private set; }
  20. public LinkedListNode<long> Node { get; private set; }
  21. public int CallCount { get; set; }
  22. public int Size { get; private set; }
  23. public long Timestamp { get; private set; }
  24. public CacheBucket(ATranslatedSub Subroutine, LinkedListNode<long> Node, int Size)
  25. {
  26. this.Subroutine = Subroutine;
  27. this.Size = Size;
  28. UpdateNode(Node);
  29. }
  30. public void UpdateNode(LinkedListNode<long> Node)
  31. {
  32. this.Node = Node;
  33. Timestamp = GetTimestamp();
  34. }
  35. }
  36. private ConcurrentDictionary<long, CacheBucket> Cache;
  37. private LinkedList<long> SortedCache;
  38. private int TotalSize;
  39. public ATranslatorCache()
  40. {
  41. Cache = new ConcurrentDictionary<long, CacheBucket>();
  42. SortedCache = new LinkedList<long>();
  43. }
  44. public void AddOrUpdate(long Position, ATranslatedSub Subroutine, int Size)
  45. {
  46. ClearCacheIfNeeded();
  47. TotalSize += Size;
  48. lock (SortedCache)
  49. {
  50. LinkedListNode<long> Node = SortedCache.AddLast(Position);
  51. CacheBucket NewBucket = new CacheBucket(Subroutine, Node, Size);
  52. Cache.AddOrUpdate(Position, NewBucket, (Key, Bucket) =>
  53. {
  54. TotalSize -= Bucket.Size;
  55. SortedCache.Remove(Bucket.Node);
  56. return NewBucket;
  57. });
  58. }
  59. }
  60. public bool HasSubroutine(long Position)
  61. {
  62. return Cache.ContainsKey(Position);
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public bool TryGetSubroutine(long Position, out ATranslatedSub Subroutine)
  66. {
  67. if (Cache.TryGetValue(Position, out CacheBucket Bucket))
  68. {
  69. if (Bucket.CallCount++ > MinCallCountForUpdate)
  70. {
  71. if (Monitor.TryEnter(SortedCache))
  72. {
  73. try
  74. {
  75. Bucket.CallCount = 0;
  76. SortedCache.Remove(Bucket.Node);
  77. Bucket.UpdateNode(SortedCache.AddLast(Position));
  78. }
  79. finally
  80. {
  81. Monitor.Exit(SortedCache);
  82. }
  83. }
  84. }
  85. Subroutine = Bucket.Subroutine;
  86. return true;
  87. }
  88. Subroutine = default(ATranslatedSub);
  89. return false;
  90. }
  91. private void ClearCacheIfNeeded()
  92. {
  93. long Timestamp = GetTimestamp();
  94. while (TotalSize > MaxTotalSize)
  95. {
  96. lock (SortedCache)
  97. {
  98. LinkedListNode<long> Node = SortedCache.First;
  99. if (Node == null)
  100. {
  101. break;
  102. }
  103. CacheBucket Bucket = Cache[Node.Value];
  104. long TimeDelta = Timestamp - Bucket.Timestamp;
  105. if (TimeDelta <= MinTimeDelta)
  106. {
  107. break;
  108. }
  109. if (Cache.TryRemove(Node.Value, out Bucket))
  110. {
  111. TotalSize -= Bucket.Size;
  112. SortedCache.Remove(Bucket.Node);
  113. }
  114. }
  115. }
  116. }
  117. private static long GetTimestamp()
  118. {
  119. long timestamp = Stopwatch.GetTimestamp();
  120. return timestamp / (Stopwatch.Frequency / 1000);
  121. }
  122. }
  123. }