slang-i18n / slang

Type-safe i18n for Dart and Flutter
https://pub.dev/packages/slang
MIT License
474 stars 39 forks source link

add cache translate_var variable #147

Closed heralight closed 1 year ago

heralight commented 1 year ago

Hi!

Thank you for this great library!

I've encountered a performance problem due to the fact that 'translate_var' is recalculated on each call, the locale is extracted each time.

Testing,

       for (var i = 0; i < 500000; i++) {
            var tr = t.somelabel;          
        }

Time taken: 2628 ms vs:

       final itl = t;
       for (var i = 0; i < 500000; i++) {
            var tr = itl.somelabel;          
        }

Time taken: 1595 ms

For my part, I use the "translation_class_visibility: public" flag to create an intermediate cache, but doing so or proposing it at library level would be a plus.

So, in this merge request, I'm adding a private cache variable. Its reset on setLocale() calls is missing, hence the partial nature of this merge request, as I don't know what would be the best approach.

I look forward to hearing from you soon and thank you,

Alexandre

Tienisto commented 1 year ago

It is better to make the caching optional (opt-in) like adding an optional named parameter for setLocale "cache" or something like that.

The reason is that this library has multi-package support so the locale can be changed everywhere.

heralight commented 1 year ago

Ok, noted. I close the mr.

Thank you!