Giter Club home page Giter Club logo

lfu_vecs's Introduction

lfu_vecs

LFU implemented in very basic (no interior mutability) Rust.

Why and how?

Not so long ago a paper on O(1) LFU design (http://dhruvbird.com/lfu.pdf) was pretty popular in caching world. While LFU isn't (on it's own) that good of a general cache algorithm, people do like bragging about implementing a O(1) algorithm :)

The original paper outlines the algorithm using doubly linked list. That's both interesting as a problem for learning Rust (because doubly linked lists are deemed hard) syntax and a counter example of how to implement something in Rust. I came to that conclusion comparing version with doubly linked lists that requires use of interior mutability pattern in Rust. That in turn means runtime vs. compile time checking and generally makes for worse performance overall due to simple problems such as poor compiler-level optimizations.

So as an exercise in writing idiomatic performant Rust that's easy to read, argue about and still correct I added this version.

The design principles are fairly simple:

  • store data about keys and how frequent they are using FrequencyNode struct
  • store said structs in a Vec<FrequencyNode>
  • at the moment frequency simply maps to index in that list
  • values for keys are stored in a HashMap using Item struct
  • Item itself simply contains the current index of FrequencyNode storing the key and Bytes field for data

Assuming Rust's HashMap get, insert and remove operations are O(1) this boils down to question of what's the added complexity coming from storing frequency data in Vec:

  • get means we need to grab the Item from HashMap, pull key from Vec<FrequencyNode>, assign it to next Vec<FrequencyNode> and incrment it's index
  • set is simply insert into HashMap and add key to Vec<FrequencyNode> at index 0
  • only worrying point at the moment is adding new FrequencyNode> since that's O(m) where m is Vec.len()

So is this any good?

Well, it's written using Vec, HashMap and tokio Bytes. Thus it's limited to being oportunistically (expected/amortized) O(1) rather than guaranteed O(1).

Complexities for Vec and HashMap are sourced from here

At minimum cache has to provide two key functions:

  • get - internally this means one HashMap.get, Vec.get_mut and Vec.push operations
  • insert - internally this means HashMap.get and Vec.get_mut and Vec.push operations

with Vec.get being O(1) and HashMap.get being expected O(1) the only problem is Vec.push which is amortized O(1) in this case.

Interesting reading

To read if HashMap makes sense you can read on it here

lfu_vecs's People

Contributors

mdomans avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.