You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Meta-programming using structs for JIT dead code removal and inlining
202
+
## Meta-programming using structs and JIT value type optimization
203
203
204
-
TemplateConcurrentLru features injectable policies defined as structs. Since structs are subject to special JIT optimizations, the implementation is much faster than if these policies were defined as classes. Using this technique, 'Fast' versions without hit counting are within 30% of the speed of a ConcurrentDictionary.
204
+
TemplateConcurrentLru features injectable behaviors defined as structs. Structs are subject to special JIT optimizations, and the .NET JIT compiler can inline, eliminate dead code and propogate JIT time constants based on structs. Using this technique, the TemplateConcurrentLru can be customized to support LRU and TLRU policies without compromising execution speed.
205
205
206
-
Since DateTime.UtcNow is around 4x slower than a ConcurrentDictionary lookup, policies that involve time based expiry are significantly slower. Since these are injected as structs and the slow code is optimized away, it is possible maintain the fastest possible speed for the non-TTL policy.
207
-
208
-
### TemplateConcurrentLru.TryGet
206
+
### Example: TemplateConcurrentLru.TryGet
209
207
210
208
This is the source code for the TryGet method. It calls into two value type generic type arguments: policy (1) and hitcounter (2).
211
209
@@ -236,7 +234,7 @@ public bool TryGet(K key, out V value)
The JITted assembly code for the TryGet method with these value type implementations is 76 bytes:
252
+
The branch and enclosed code for ShouldDiscard are completely eliminated by JIT (1). Since the below code is from FastConcurrentLru, the hit counting calls (2) are also eliminated. The assembly code for the FastConcurrentLru.TryGet method with LruPolicy and NullHitCounter is 76 bytes:
0 commit comments