TranslatorCache.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 TranslatorCache
  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 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. _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 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. 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(TranslatedSub);
  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. }