-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/jitbit/FastCache
- Loading branch information
Showing
2 changed files
with
2 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,3 @@ | ||
# Atomicness | ||
|
||
When it comes ot atomicness, the biggest challenge is to check "item exists" and "item not expired" in one go. | ||
|
||
When an item was "found but is expired" - we need to treat this as "not found" and discard it. For that we either need to use a lock | ||
so that the the three steps "exist? expired? remove!" are performed atomically. Otherwise another tread might chip in, | ||
and ADD a non-expired item with the same key while we're evicting it. And we'll be removing a non-expired key taht was just added | ||
|
||
OR instead of using locks we can remove _by key AND by value_. So if another thread has just rushed in | ||
and added another item with the same key - that other item won't be removed. | ||
|
||
basically, instead of doing this | ||
|
||
``` | ||
lock { | ||
exists? | ||
expired? | ||
remove by key! | ||
} | ||
``` | ||
|
||
we do this | ||
|
||
``` | ||
exists? (if yes returns the value) | ||
expired? | ||
remove by key AND value | ||
``` | ||
|
||
If another thread chipped in while we were in the middle of checking if it's expired or not, and recorded a new value - we won't remove it. | ||
|
||
Locks suck becasue add extra 50ns to benchmark, so it becomes 110ns instead of 70ns which sucks. | ||
So - no locks then! | ||
The article has been moved [here](https://www.jitbit.com/alexblog/fast-memory-cache/#perf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters