Comments (16)
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.
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.
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.
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.
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.
It would be nice to push a PR to improve the PERF
from my.extensions.localization.json.
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.
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.
Thanks @fededim I will try to measure the PERF after your changes
from my.extensions.localization.json.
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.
FYI configuration APIs is already have auto reload options buitl-in
from my.extensions.localization.json.
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.
Thanks for your informative details, I will try to made some changes ASAP
from my.extensions.localization.json.
You are welcome
from my.extensions.localization.json.
@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.
Fixed in #74
from my.extensions.localization.json.
Related Issues (20)
- Missing Localization Behavior
- Remove internal contructor in JsonStringLocalizer HOT 1
- Ability to override methods in JsonStringLocalizer and JsonStringLocalizerFactory HOT 9
- Registering the service one should provide absolute path, otherwise exception is thrown HOT 31
- Localization FallBack Considerations
- Support ResourceLocationAttribute HOT 1
- Support RootNamespaceAttribute
- assign culture in runtime HOT 3
- Have one resource file for project HOT 13
- Any plan to switch the project to a .NET standard class library ? HOT 10
- How to use with DataAnnotation? HOT 17
- Regression Bug HOT 1
- LocalizationSample project is not working HOT 3
- About NET5.0 Blazor WebAssembly Uses My.Extensions.Localization.Json HOT 14
- System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. HOT 11
- Class based localisation only works when the class is in the root namespace HOT 5
- Can you provide an core api example with DataAnnotations? HOT 2
- when using xunit test(SUT),there is an exception throwed! HOT 7
- JsonResourceManager does not dispose of (unix) File System Watches HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from my.extensions.localization.json.