-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify LruCache, expose the entry API instead of a
produce
API
There is a problem with the entry API which also exists with the `produce` API, and that is it is difficult to maintain the correct cache size when producing a new value. When implemented naively, after the mutable reference to newly produced value is obtained, the LruCache must be shrunk to fit the space requirements. However, this is not safe to do, as the returned reference must not be mutated or moved while it is alive, so we cannot (in general, safely) shrink the underlying map after obtaining this reference. Less naively, you could hash the provided key and use the raw entry API to look up the key, return an entry wrapping a raw entry if it is found, and otherwise shrink the map by 1 and use the raw api to look up an entry again (using the same computed hash). This way, you would not exceed the cache capacity and hits would be very fast, but a miss would incur two hash lookups (but not necessarily two key hashes due to the raw API allowing pre-computed hashes). The same basic issue exists if you try to do this with a `produce` style API. This is also problematic, because trying to do this safely runs into rust-lang/rust#40307 The simplest thing to do is simply let the cache grow by 1 too many elements and document the behavior.
- Loading branch information
Showing
2 changed files
with
51 additions
and
122 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
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