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

Add a lazy cache for Intl objects on MessageContext #32

Closed
zbraniecki opened this issue Oct 15, 2017 · 7 comments
Closed

Add a lazy cache for Intl objects on MessageContext #32

zbraniecki opened this issue Oct 15, 2017 · 7 comments
Labels
help wanted We need help making decisions or writing PRs for this.
Milestone

Comments

@zbraniecki
Copy link
Collaborator

Now that we have a proper PluralRules object, we should cache it on MessageContext.

@zbraniecki zbraniecki added this to the 0.2 milestone Oct 15, 2017
@zbraniecki
Copy link
Collaborator Author

Ehh, this ends up being a very non trivial task.

I spent way too much time today on #rust trying to understand the limitations. It boils down to - it's not easy/possible to have a single cache for different types. If all our formatters had the same set of methods, we could do this with impl Trait and trait object.
But in the world where we have PluralRules.select and DateTimeFormat.format, it's very hard.

It is possible to use an enum here, in a closed model which will not allow third-party devs to add their own formatters (or at least, they wont be cached).

Unfortunately, that requires us to change the mutability of ctx and env in resolver, because now any call can trigger adding an object to cache, so you have to pass mut env all around.

@stasm - would you be interested in taking a look at this? Maybe you'll come up with some better model for this problem, because I feel like I'm out of ideas :(

@stasm
Copy link
Contributor

stasm commented Oct 17, 2017

We'll need some kind of mutability for env (via mut or RefCell maybe?) for #21, too. I'll first look into #21 and perhaps it will give us some ideas for this issue.

@mmstick
Copy link
Contributor

mmstick commented Jul 26, 2018

What needs to be cached, exactly?

@zbraniecki
Copy link
Collaborator Author

nothing yet for we don't have any intl formatters. It's likely going to be delayed till later.

We're about to switch fluent to use https://github.com/unclenachoduh/pluralrules which will be the first intl formatter and it will not require any caching for now.

@zbraniecki
Copy link
Collaborator Author

but once we get intl date time formatting and number formatting we'll likely want to cache the instantiated structures.

@zbraniecki
Copy link
Collaborator Author

@mmstick with @unclenachoduh landing of the intl-pluralrules crate use, now we have a good candidate.

Every comparison that involves a PLURAL builtin renegotiates locales and regenerates IntlPluralRules instance: https://github.com/projectfluent/fluent-rs/blob/master/fluent/src/types.rs#L60-L67

Instead, we should do what we do in JS: https://github.com/projectfluent/fluent.js/blob/master/fluent/src/context.js#L212-L222

There are subtle differences - JS uses an Options bag, we just pass one argument, JS does language negotiation inside the constructor, we do it externally, but it boils down to the same.

We want to do it once, and for the given PluralRulesType cache the instance on the MessageContext.

The trick is that in the perfect world, we'd like to make the mechanism flexible enough to handle some sort of Intl.DateTimeFormat and Intl.NumberFormat equivalents in the future once Rust gets them.
But I guess we can also start with a cache just for PluralRules for now.

Wanna take a stab at it?

@zbraniecki zbraniecki added the help wanted We need help making decisions or writing PRs for this. label Jul 31, 2018
@zbraniecki
Copy link
Collaborator Author

I was able to fix it trivially for now in 7b0cd65

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted We need help making decisions or writing PRs for this.
Projects
None yet
Development

No branches or pull requests

3 participants