immudb icon indicating copy to clipboard operation
immudb copied to clipboard

LRUCache optimizations

Open arturmelanchyk opened this issue 1 year ago • 2 comments

  • validate input params before obtaining mutex lock
  • replace sync.Mutex with sync.RWMutex

arturmelanchyk avatar Jan 28 '24 12:01 arturmelanchyk

Ok, as it turns out, Size() is never used and EntriesCount() is only used in TBtree::nodeAt() which itself obtains exclusive lock, so there is no much sense in RWMutex here, unfortunately. Reverted the commit.

arturmelanchyk avatar Feb 05 '24 20:02 arturmelanchyk

I don't have full context where this particular cache is used but in general this is a common and good approach to use RW locks for caches. When you mention using RW lock makes sens only if there is a good read vs write ratio this means you have to think twice if you really need cache.

I'm not generalizing because there are a couple use cases where having more writes than reads make sense but in most cases if there are more writes than reads then cache doesn't bring much value.

If you really really need high possible parallelism for writes then I guess the only reasonable ways are having many locks per hash pools or best way to implement lock-free list with CAS. Not sure if there are existing and maintained lock-free list implementations in Go.

prog8 avatar Feb 07 '24 14:02 prog8

Coverage Status

coverage: 89.503% (-0.02%) from 89.519% when pulling 2c44fa20513a9150f8c6c32e77a098d6a7c550fb on arturmelanchyk:lru-cache-optimizations into 65a25a5b71de2522d93ea0c5f5fc585c9a7a9f69 on codenotary:master.

coveralls avatar Mar 01 '24 14:03 coveralls