projectfluent / fluent-rs

Rust implementation of Project Fluent
https://projectfluent.org
Apache License 2.0
1.08k stars 97 forks source link

Intl memoizer #148

Closed zbraniecki closed 4 years ago

zbraniecki commented 4 years ago

@Manishearth - here's what I got to. Not sure about the naming, but it mostly works with bumping/dropping refcounter.

The one thing that I failed to find how to do is how to remove all cached elements from all types when a rc for lang goes to 0. Can you help me with that?

zbraniecki commented 4 years ago

Addresses #149

Manishearth commented 4 years ago

@zbraniecki you can use a Weak instead of an Rc in the map itself. this will not remove map entries, but it will remove the actual data being cached

zbraniecki commented 4 years ago

@zbraniecki you can use a Weak instead of an Rc in the map itself. this will not remove map entries, but it will remove the actual data being cached

The way its meant to work is that the IntlMemoizer stores the entries between the first get and until the last FluentBundle of a given lang calls drop_rc.

If I use Weak, I'll need to have additional Map on each FluentBundle to store Rc<T> on it from the first time the bundle needs a formatter, until the bundle drops, right?

I'd prefer to only drop the entry in drop_rc. Is that possible?

Manishearth commented 4 years ago

Hmm, the design constraints seem more complicated than i thought :/

zbraniecki commented 4 years ago

@Manishearth - that's what we do I believe! Example:

       let mut memoizer = IntlMemoizer::default();
       let en_memoizer = memoizer.get_for_lang(lang);
       let mut en_memoizer_borrow = en_memoizer.borrow_mut();

       let cb = en_memoizer_borrow
            .try_get::<PluralRules>((PluralRuleType::CARDINAL,))
            .unwrap();

Is that different from what you suggested?

Manishearth commented 4 years ago

Oh, huh, I feel like I read it the other way around at some point. r=me