BitFaster.Caching
BitFaster.Caching copied to clipboard
Mitigate LFU struct tearing (new node)
trafficstars
When updating a large struct, instead of updating in place, we can instead create a new node. Since nodes are not updated, no tearing can occur.
Time based expiry is a special case that is handled as follows:
- The new
INodePolicy.Clonemethod creates the replacement node to insert into the dictionary. ForExpireAfterPolicythis avoids calling expiry calculator on create, and we set current expiry to match old node. We also set GetNextInTimeOrder == self, this is a hack so thatExpireAfterPolicy.OnWriteknows the node was not created and is currently detached from theTimerWheel. - In
ExpireAfterPolicy.OnWrite, check if next == self to schedule/reschedule. Schedule will just overwrite next to a valid node. - Add note to GetNextInTimeOrder to explain
TODO:
- Unit test
- Soak test
- Verify TLFU logic