TranslatorCache.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 TranslatorCache
  10. {
  11. //Maximum size of the cache, the unit used is completely arbitrary.
  12. private const int MaxTotalSize = 0x800000;
  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 TranslatedSub 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(TranslatedSub subroutine, LinkedListNode<long> node, int size)
  25. {
  26. Subroutine = subroutine;
  27. Size = size;
  28. UpdateNode(node);
  29. }
  30. public void UpdateNode(LinkedListNode<long> node)
  31. {
  32. Node = node;
  33. Timestamp = GetTimestamp();
  34. }
  35. }
  36. private ConcurrentDictionary<long, CacheBucket> _cache;
  37. private LinkedList<long> _sortedCache;
  38. private int _totalSize;
  39. public TranslatorCache()
  40. {
  41. _cache = new ConcurrentDictionary<long, CacheBucket>();
  42. _sortedCache = new LinkedList<long>();
  43. }
  44. public void AddOrUpdate(long position, TranslatedSub subroutine, int size)
  45. {
  46. ClearCacheIfNeeded();
  47. lock (_sortedCache)
  48. {
  49. _totalSize += size;
  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 TranslatedSub 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. //The bucket value on the dictionary may have changed between the
  76. //time we get the value from the dictionary, and we acquire the
  77. //lock. So we need to ensure we are working with the latest value,
  78. //we can do that by getting the value again, inside the lock.
  79. if (_cache.TryGetValue(position, out CacheBucket latestBucket))
  80. {
  81. latestBucket.CallCount = 0;
  82. _sortedCache.Remove(latestBucket.Node);
  83. latestBucket.UpdateNode(_sortedCache.AddLast(position));
  84. }
  85. }
  86. finally
  87. {
  88. Monitor.Exit(_sortedCache);
  89. }
  90. }
  91. }
  92. subroutine = bucket.Subroutine;
  93. return true;
  94. }
  95. subroutine = default(TranslatedSub);
  96. return false;
  97. }
  98. private void ClearCacheIfNeeded()
  99. {
  100. long timestamp = GetTimestamp();
  101. while (_totalSize > MaxTotalSize)
  102. {
  103. lock (_sortedCache)
  104. {
  105. LinkedListNode<long> node = _sortedCache.First;
  106. if (node == null)
  107. {
  108. break;
  109. }
  110. CacheBucket bucket = _cache[node.Value];
  111. long timeDelta = timestamp - bucket.Timestamp;
  112. if (timeDelta <= MinTimeDelta)
  113. {
  114. break;
  115. }
  116. if (_cache.TryRemove(node.Value, out bucket))
  117. {
  118. _totalSize -= bucket.Size;
  119. _sortedCache.Remove(bucket.Node);
  120. }
  121. }
  122. }
  123. }
  124. private static long GetTimestamp()
  125. {
  126. long timestamp = Stopwatch.GetTimestamp();
  127. return timestamp / (Stopwatch.Frequency / 1000);
  128. }
  129. }
  130. }