Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weak_table optional dependency with adequate WeakElement implementation #19

Open
AdrienChampion opened this issue Feb 26, 2024 · 2 comments
Labels
enhancement poll asks users for feedback

Comments

@AdrienChampion
Copy link
Owner

We prefer not adding dependencies if we can avoid it, but at least two users expressed interest in integrating with weak_table (#15). While there is a reasonable workaround, actual integration would be much nicer.

This issue is to assess how big the interest for integration is, please let us know here.

@AdrienChampion AdrienChampion added enhancement poll asks users for feedback labels Feb 26, 2024
@asvarga
Copy link

asvarga commented Feb 26, 2024

My use case for this was for memoizing a function that takes an HConsed<T> with a WeakKeyHashMap so the table itself wouldn't contribute to the HConsed<T>::elm's reference count. I'm doing something like:

use once_cell::sync::Lazy;
use std::sync::{RwLock, Weak};
use weak_table::WeakKeyHashMap;

pub fn term_to_output(term: &Term) -> Output {
    static MEMO: Lazy<RwLock<WeakKeyHashMap<Weak<ActualTerm>, Output>>> =
        Lazy::new(|| RwLock::new(WeakKeyHashMap::default()));

    if let Some(ret) = MEMO.read().unwrap().get(&term.0) {
        return *ret;
    }

    let ret: Output = todo!();

    MEMO.write().unwrap().insert(term.0.to_weak_ref().upgrade().unwrap(), ret); // yuck
    ret
}

Questions:

  • Is there a better way to accomplish this with what hashconsing already provides?
  • Can this optional dependency also provide integration with https://crates.io/crates/memoize via weak versions of the structures in hashconsing::hash_coll?

@AdrienChampion
Copy link
Owner Author

Regarding caching using weak references, I actually wrote an example in a branch back when I misunderstood your question in #15. It only uses std.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement poll asks users for feedback
Projects
None yet
Development

No branches or pull requests

2 participants