Comments (2)
Hi.
It's going to be something like
size_of(key) + size_of(value) + overhead
. What is the overhead? And is this the right idea?
Yes, it is the right idea.
Overhead per Entry
The overhead per cache entry will be the followings on 64-bit platforms:
Write-order queue? | Overhead (bytes per entry) *2 |
---|---|
Disabled | 152 |
Enabled (*1) | 184 |
*1: Write-order queue is enabled if one or both of the following CacheBuilder
method was/were called:
time_to_live
support_invalidation_closures
*2: Overhead becomes bigger than the numbers listed here if the crate feature quanta
and/or atomic64
is disabled. (They are enabled by default)
Overhead of The Whole Cache
The LFU Filter (CountMin Sketch)
When the cache becomes half-full, the LFU filter (aka CountMin Sketch) will be created for the cache.
The size of the filter is decided by an estimated max number of entries in the cache. Cache
uses the following code to estimate:
let estimated_max_num_entries: u64 = ((current_num_entries as f64
* (current_total_weighted_size as f64 / max_capacity as f64))
as u64)
.max(128); // minimum 128 entries.
The byte size of the LFU filter can be calculated by the following code:
// maximum 8GiB on 64-bit platform.
let byte_size = 8 *
estimated_max_num_entries
.min(2u64.pow(30))
.next_power_of_two();
Here are some examples:
Estimated max number of entries | Byte Size | MiB |
---|---|---|
1 million | 8,388,624 | 8MiB |
5 millions | 67,108,880 | 64MiB |
10 millions | 134,217,744 | 128MiB |
EDIT: Fixed the formula for the LFU filter. Forgot to multiply by 8 as the internal table is Box<[u64]>
, not Box<[u8]>
.
from moka.
Also my Cache's Values are json_serde::Value and I'm not sure how to calculate their size in memory. Maybe that's a better question for the serde issue tracker though.
If I understand the internal data structure of serde_json::Value
correctly, the following function can calculate the byte size of a value. You may want to ask serde_json maintainers if this function makes sense.
use serde_json::Value;
fn size_of_json_val(json: &Value) -> usize {
use serde_json::Value::*;
use std::mem::size_of;
let v_size = size_of::<Value>();
match json {
Null | Bool(_) | Number(_) => v_size,
String(str) => v_size + str.capacity(),
Array(vec) => {
v_size
+ vec.iter().map(size_of_json_val).sum::<usize>()
+ v_size * (vec.capacity() - vec.len())
}
Object(map) => {
let s_size = size_of::<std::string::String>();
v_size
+ map
.iter()
.map(|(k, v)| s_size + k.capacity() + size_of_json_val(v))
.sum::<usize>()
// Can't do the following because `serde_json::Map` does not have `capacity` method.
// + (s_size + v_size) * (map.capacity() - map.len())
}
}
}
EDIT: Fixed the formula for Object(map)
by adding s_size
.
from moka.
Related Issues (20)
- Cache with max capacity of zero should not cache anything
- Proposal: support insert key with expire time? HOT 16
- CI: Disable external CI services on temporary merge-queue branches?
- Statistics for metrics HOT 3
- `get_with` deadlock HOT 13
- `.blocking().invalidate(key)` doesn't trigger eviction listener HOT 1
- RUSTSEC-2020-0168: mach is unmaintained HOT 2
- Can the eviction strategy be configured? HOT 8
- Upgrade the cache policy from TinyLFU to W-TinyLFU
- Bump the MSRV to 1.60
- optionally_try_get_with in moka::future::Cache HOT 1
- feature request: remove method HOT 2
- Use `std::thread::available_parallelism()` instead of `num_cpus` dependency
- Eviction listener not always called. HOT 2
- Cache housekeeper can panic (seen after 50M+ insertions) HOT 4
- CI: Miri test causes a complie error with Rust nightly-2023-05-27 HOT 2
- Implement Serialize trait for debugging webserver? HOT 2
- Data race found by `miri test` HOT 9
- CI: Enable Miri tests on `moka::cht::*` modules
- Memory corruption observed when using Moka v0.9.6 HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moka.