GithubHelp home page GithubHelp logo

Comments (16)

fededim avatar fededim commented on July 20, 2024 1

Fine, but you need still a callback to update the Dictionary when json files are reloaded (maybe you can use directly the IConfigurationRoot returned from Build command instead of using a Dictionary, but I do not know its performance). In this case you also need synchronized Dictionary (like the ConcurrentDictionary you use) whereas if you don't use autorefresh you can use standard Dictionary which is faster. IMHO autorefresh is not needed for Localization.

from my.extensions.localization.json.

fededim avatar fededim commented on July 20, 2024 1

I checked ConfigurationRoot, every provider uses a dictionary to store the key-values, even though it is not synchronized (this could raise the issue of not finding a key during a reload). If you want you can use also a Dictionary<string, ConfigurationRoot> exploiting its autoreload.

from my.extensions.localization.json.

fededim avatar fededim commented on July 20, 2024

It would be better because I noticed that the localization of a string takes around 250ms which is extremely slow (I have a JSON with 2600 keys)! The problem is in the string localizer where you use a Dictionary only for indexing culture and not keys (it's an enumerable which has be scanned for the search of each key, if you have a lot of keys it's very slow). It would be better to use a double dictionary, e.g.

Change from

private readonly ConcurrentDictionary<string, IEnumerable<KeyValuePair<string, string>>> _resourcesCache = new ConcurrentDictionary<string, IEnumerable<KeyValuePair<string, string>>>();

To

private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, string>> _resourcesCache = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>();

Moreover the cache has to be built only once at the beginning and not at every keyword search.

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

Thanks @fededim for reporting this, just one question did the PERF improved after you did your changes?

I will try to finish the benchmarking and modify some places in the code to improve the PERF

from my.extensions.localization.json.

fededim avatar fededim commented on July 20, 2024

I ended up writing my own library which takes around 0ms (so 250 times less). The speed greatly improves because for an access Dictionary has O(1) time complexity (e.g. constant in the dictionary size) whereas IEnumerable has O(n) time complexity (e.g. linear in the number of elements of IEnumerable).

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

It would be nice to push a PR to improve the PERF

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

Moreover the cache has to be built only once at the beginning and not at every keyword search.

As I remembered I did this to make the localization keys updated whenever I modify the json file, probably I need to revise everything in there

Thanks

from my.extensions.localization.json.

fededim avatar fededim commented on July 20, 2024

I do not have time for a PR (I have already written my own library), the changes are not difficult just change the variable I pointed out and fix compilation errors. It is just an advice of how to improve the speed of your library.

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

Thanks @fededim I will try to measure the PERF after your changes

from my.extensions.localization.json.

fededim avatar fededim commented on July 20, 2024

Moreover the cache has to be built only once at the beginning and not at every keyword search.

As I remembered I did this to make the localization keys updated whenever I modify the json file, probably I need to revise everything in there

Thanks

Ok this is a good point, but do you need it ? Usually localization happens rarely: when you support a new language you translate all strings and create a new json file which remains more or less the same. There might be some corrections, but in that case you can change the file and restart the website/application pool to reload the new changes.
If you really want an autorefresh the best way would be to provide a InvalidateCache method and set a watcher on the resource directory which calls the InvalidateCache method whenever any file changes. The string operator of IStringLocalizer should reload the cache only if it has been invalidated.

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

FYI configuration APIs is already have auto reload options buitl-in

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

IMHO autorefresh is not needed for Localization.

Yes & No, it's needed to refresh the added values at runtime, but not for a new added culture

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

Thanks for your informative details, I will try to made some changes ASAP

from my.extensions.localization.json.

fededim avatar fededim commented on July 20, 2024

You are welcome

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

@fededim could you please share your json file if you don't mind, I need to measure the PERF one more time

from my.extensions.localization.json.

hishamco avatar hishamco commented on July 20, 2024

Fixed in #74

from my.extensions.localization.json.

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.