Coder Social home page Coder Social logo

Entry API about lru-rs HOT 9 OPEN

Ralith avatar Ralith commented on September 26, 2024 5
Entry API

from lru-rs.

Comments (9)

jeromefroe avatar jeromefroe commented on September 26, 2024 1

Gotcha, definitely seems useful! I'll take a crack at it when I get some cycles, but it will probably be a larger change so I don't anticipate I'll be able to get to it in the near future.

from lru-rs.

jeromefroe avatar jeromefroe commented on September 26, 2024

Hey @Ralith! I don't think I have a clear grasp of what your use case is. Would you mind providing an example?

From what I do understand, it sounds like you could use an additional method like the put method but which only inserts the value if the key doesn't already exist in the cache and that indicates whether the new value was inserted or not by returning a reference to the value that is stored in the cache? Something like the following perhaps:

use lru::LruCache;
let mut cache = LruCache::new(2);

let first = cache.put_if_missing(1, "a");
assert_eq!(first, &"a");

let second = cache.put_if_missing(1, "b");
assert_eq!(second, &"a");

I don't like the name put_if_missing but couldn't think of anything better right now :)

from lru-rs.

Ralith avatar Ralith commented on September 26, 2024

The use case, and desired semantics, are exactly that of the std HashMap::entry API (see also BTreeMap, etc). put_if_missing isn't a solution because my use of LruCache is to avoid repeating expensive computations, which requires waiting until the entry is known to be missing before computing its value.

from lru-rs.

Stargateur avatar Stargateur commented on September 26, 2024

Actually, this is even more needed cause I can't get a value, return a reference to it or if missing create the value and return it cause it's borrow the cache as mutable more than one. It's quite annoying for a cache to not be able to do this basic operation.

from lru-rs.

eggyal avatar eggyal commented on September 26, 2024

I think one question for this API will be at what point an occupied entry is considered "used" and moved to the back of the queue? I think there are broadly three options:

  1. When OccupiedEntry is created (i.e. when LruCache::entry(&mut self, key: K) is called for an extant key);
  2. When the extant value is obtained from an OccupiedEntry (e.g. when OccupiedEntry::get(&self) etc are invoked); or
  3. When OccupiedEntry is dropped. This has the advantage that no extraneous work is performed if the entry was removed from the cache.

from lru-rs.

tqwewe avatar tqwewe commented on September 26, 2024

Would love to see this implemented!
The stdlib has a method .entry(&key).or_insert(val) which is extremely nice to have. Otherwise I can't think of a better way to write the following:

let value = match cache.get(&key) {
    Some(value) => value,
    None => {
        let val = foo();
        cache.put(key, val);
        cache.get(key).expect("item should exist")
    }
};

It would be much better to do:

let value = cache.entry(key).or_insert_with(foo);

from lru-rs.

gyscos avatar gyscos commented on September 26, 2024

Note that LruCache recently got a get_or_insert method; however that method always require a fully-owned key. This means that when using, for example, String as keys, it's not possible to only allocate when a new entry needs to be inserted.

There's also no get_or_insert_mut that would return a mutable reference to the (possibly newly-inserted) value.

Finally, the entry API has other advantages, like working better when the function to create the missing value can fail.

from lru-rs.

BrynCooke avatar BrynCooke commented on September 26, 2024

I also miss get_or_insert_mut. When trying to use this as a multimap I first have to do a get_or_insert followed by a get_mut, first to populate a vec for a key, and then to actually place a new item in the vec.

from lru-rs.

Ten0 avatar Ten0 commented on September 26, 2024

Another use-case for the entry API is referencing the key in the insert fn without reallocating it:

let k: String = "some-key".into();
let v = match map.entry(k) {
    Entry::Occupied(e) => e.into_mut(),
    Entry::Vacant(v) => {
        v.insert(query_my_server_for_whatever(v.key().as_str())?)
    }
};

from lru-rs.

Related Issues (20)

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.