ATranslatorCache.cs 4.4 KB

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