Skip to content

Is there a reason the weigher only supports u32, while max capacity is u64? #409

Answered by tatsuya6502
GalLalouche asked this question in Q&A
Discussion options

You must be logged in to vote

Thank you for using moka.

It uses u32 for entry weight rather than u64 to reduce memory overhead. A cache instance must hold the weight values of all entries so a small data type (u32 4 bytes) is preferable than a large data type (u64 8 bytes). It uses u64 for the max capacity and weighted_size (the sum of the weight values of all entries) to avoid arithmetic overflow (e.g. u32::MAX + 1u32) . The memory overhead are small for them because a cache instance only needs to hold one u64 for each.

Entry weights are relative values so you can scale your original value to fit in the 2^32 range, and scale the max capacity as well.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by GalLalouche
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants