Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
clean & fixes for performance #50
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Teixeira committed May 2, 2019
1 parent b182c37 commit 8f2ba14
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Copyright>Copyright 2018 (c) Askmethat Corporation.</Copyright>
<PackageTags>Json Localizer Globalization netcore netstandard</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.1.6</Version>
<Version>2.1.0</Version>
<RepositoryUrl>https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace Askmethat.Aspnet.JsonLocalizer.Extensions
{
public class JsonLocalizationOptions : LocalizationOptions
{
public JsonLocalizationOptions()
{
SupportedCultureInfos = new HashSet<CultureInfo>();
}

const char PLURAL_SEPARATOR = '|';
const string DEFAULT_RESOURCES = "Resources";
Expand All @@ -33,12 +29,11 @@ public CultureInfo DefaultCulture
{
return defaultCulture;
}
set
set
{
if (value != defaultCulture)
{
defaultCulture = value ?? CultureInfo.InvariantCulture;
AddDefaultCulture();
}
}
}
Expand All @@ -59,7 +54,6 @@ public HashSet<CultureInfo> SupportedCultureInfos
if (value != supportedCultureInfos)
{
supportedCultureInfos = value;
AddDefaultCulture();
}
}
}
Expand All @@ -71,13 +65,5 @@ public HashSet<CultureInfo> SupportedCultureInfos

public bool UseBaseName { get; set; } = false;
public char PluralSeparator { get; set; } = PLURAL_SEPARATOR;

void AddDefaultCulture()
{
if (!SupportedCultureInfos.Contains(defaultCulture) && defaultCulture != null)
{
SupportedCultureInfos.Add(defaultCulture);
}
}
}
}
15 changes: 7 additions & 8 deletions Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ public JsonStringLocalizer(IOptions<JsonLocalizationOptions> localizationOptions
_env = env;
_resourcesRelativePath = GetJsonRelativePath(_localizationOptions.Value.ResourcesPath);

foreach (var ci in localizationOptions.Value.SupportedCultureInfos)
{
InitJsonStringLocalizer(ci);
}

//after initialization, get current ui culture
GetCultureToUse(CultureInfo.CurrentUICulture);
InitJsonStringLocalizer();
}



public LocalizedString this[string name]
{
get
Expand Down Expand Up @@ -111,8 +107,11 @@ string GetString(string name, bool shouldTryDefaultCulture = true)
}

LocalizatedFormat localizedValue = null;
if (currentUsedCulture == _localizationOptions.Value.DefaultCulture || currentUsedCulture != CultureInfo.CurrentUICulture)

if (!shouldTryDefaultCulture && !IsUICultureCurrentCulture(CultureInfo.CurrentUICulture))
{
InitJsonStringLocalizer(CultureInfo.CurrentUICulture);
AddMissingCultureToSupportedCulture(CultureInfo.CurrentUICulture);
GetCultureToUse(CultureInfo.CurrentUICulture);
}

Expand Down
48 changes: 38 additions & 10 deletions Askmethat.Aspnet.JsonLocalizer/Localizer/JsonStringLocalizerBase.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Askmethat.Aspnet.JsonLocalizer.Extensions;
using Askmethat.Aspnet.JsonLocalizer.Format;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace Askmethat.Aspnet.JsonLocalizer.Localizer
{
Expand All @@ -22,7 +21,7 @@ internal class JsonStringLocalizerBase

protected readonly TimeSpan _memCacheDuration;
protected const string CACHE_KEY = "LocalizationBlob";
protected CultureInfo currentUsedCulture;
protected const string CURRENT_USED_CULTURE = "CurrentUsedCulture";

public JsonStringLocalizerBase(IOptions<JsonLocalizationOptions> localizationOptions, string baseName = null)
{
Expand All @@ -34,22 +33,51 @@ public JsonStringLocalizerBase(IOptions<JsonLocalizationOptions> localizationOpt
}

string GetCacheKey(CultureInfo ci) => $"{CACHE_KEY}_{ci.DisplayName}";
void SetCurrentCultureToCache(CultureInfo ci) => _memCache.Set(CURRENT_USED_CULTURE, ci.Name);
protected bool IsUICultureCurrentCulture(CultureInfo ci) {
string currentCulture = string.Empty;
_memCache.TryGetValue(CURRENT_USED_CULTURE, out currentCulture);

return string.Equals(currentCulture, ci.Name, StringComparison.InvariantCultureIgnoreCase);
}

protected void GetCultureToUse(CultureInfo cultureToUse)
{
if (!_memCache.TryGetValue(GetCacheKey(cultureToUse), out localization))
{
if (_memCache.TryGetValue(GetCacheKey(cultureToUse.Parent), out localization))
{
currentUsedCulture = cultureToUse.Parent;
SetCurrentCultureToCache(cultureToUse.Parent);
}
else
{
_memCache.TryGetValue(GetCacheKey(cultureToUse), out localization);
currentUsedCulture = _localizationOptions.Value.DefaultCulture;
SetCurrentCultureToCache(_localizationOptions.Value.DefaultCulture);
}
}
currentUsedCulture = cultureToUse;
SetCurrentCultureToCache(cultureToUse);
}

protected void InitJsonStringLocalizer()
{
AddMissingCultureToSupportedCulture(CultureInfo.CurrentUICulture);
AddMissingCultureToSupportedCulture(_localizationOptions.Value.DefaultCulture);

foreach (CultureInfo ci in _localizationOptions.Value.SupportedCultureInfos)
{
InitJsonStringLocalizer(ci);
}

//after initialization, get current ui culture
GetCultureToUse(CultureInfo.CurrentUICulture);
}

protected void AddMissingCultureToSupportedCulture(CultureInfo cultureInfo)
{
if (!_localizationOptions.Value.SupportedCultureInfos.Contains(cultureInfo))
{
_localizationOptions.Value.SupportedCultureInfos.Add(cultureInfo);
}
}

protected void InitJsonStringLocalizer(CultureInfo currentCulture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.3" />
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

BenchmarkDotNet=v0.11.3, OS=macOS Mojave 10.14.4 (18E226) [Darwin 18.5.0]
Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cores
.NET Core SDK=2.2.106
[Host] : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT
DefaultJob : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT
BenchmarkDotNet=v0.11.5, OS=Windows 10.0.17134.706 (1803/April2018Update/Redstone4)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328125 Hz, Resolution=300.4695 ns, Timer=TSC
.NET Core SDK=2.2.103
[Host] : .NET Core 2.2.1 (CoreCLR 4.6.27207.03, CoreFX 4.6.27207.03), 64bit RyuJIT
DefaultJob : .NET Core 2.2.1 (CoreCLR 4.6.27207.03, CoreFX 4.6.27207.03), 64bit RyuJIT


Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
------------------------------------------------ |--------------:|--------------:|--------------:|--------------:|--------------:|---------:|--------:|------------:|------------:|------------:|--------------------:|
JsonLocalizer | 68.83 ns | 0.1259 ns | 0.1116 ns | 68.58 ns | 68.95 ns | 0.63 | 0.00 | 0.0228 | - | - | 48 B |
JsonLocalizerWithCreation | 512,929.54 ns | 1,795.6669 ns | 1,679.6679 ns | 510,100.03 ns | 515,499.84 ns | 4,678.08 | 13.62 | 83.0078 | 29.2969 | 4.8828 | 174968 B |
JsonLocalizerWithCreationAndExternalMemoryCache | 4,930.42 ns | 20.7277 ns | 18.3746 ns | 4,888.34 ns | 4,956.98 ns | 44.98 | 0.18 | 1.7624 | 0.8774 | - | 3712 B |
Localizer | 109.62 ns | 0.1763 ns | 0.1563 ns | 109.48 ns | 110.05 ns | 1.00 | 0.00 | - | - | - | - |
Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
------------------------------------------------ |--------------:|--------------:|--------------:|--------------:|--------------:|----------:|---------:|--------:|--------:|-------:|----------:|
JsonLocalizer | 55.49 ns | 1.176 ns | 2.454 ns | 52.55 ns | 61.80 ns | 1.00 | 0.00 | 0.0114 | - | - | 48 B |
Localizer | 98.09 ns | 1.176 ns | 1.042 ns | 96.39 ns | 100.23 ns | 1.78 | 0.06 | - | - | - | - |
JsonLocalizerWithCreation | 647,389.65 ns | 19,441.235 ns | 56,402.548 ns | 573,985.62 ns | 814,434.86 ns | 11,620.42 | 1,268.33 | 41.0156 | 20.5078 | 1.9531 | 174688 B |
JsonLocalizerWithCreationAndExternalMemoryCache | 5,120.16 ns | 99.768 ns | 152.356 ns | 4,962.78 ns | 5,522.11 ns | 91.02 | 5.12 | 0.9384 | 0.4654 | - | 3944 B |
JsonLocalizerDefault | 268.60 ns | 1.787 ns | 1.492 ns | 266.84 ns | 271.32 ns | 4.88 | 0.15 | 0.0892 | - | - | 376 B |
LocalizerDefault | 316.64 ns | 4.932 ns | 4.614 ns | 311.53 ns | 326.27 ns | 5.73 | 0.19 | 0.0777 | - | - | 328 B |
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
``` ini

BenchmarkDotNet=v0.11.3, OS=macOS Mojave 10.14.4 (18E226) [Darwin 18.5.0]
Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cores
.NET Core SDK=2.2.106
[Host] : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT
DefaultJob : .NET Core 2.2.4 (CoreCLR 4.6.27521.02, CoreFX 4.6.27521.01), 64bit RyuJIT
BenchmarkDotNet=v0.11.5, OS=Windows 10.0.17134.706 (1803/April2018Update/Redstone4)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328125 Hz, Resolution=300.4695 ns, Timer=TSC
.NET Core SDK=2.2.103
[Host] : .NET Core 2.2.1 (CoreCLR 4.6.27207.03, CoreFX 4.6.27207.03), 64bit RyuJIT
DefaultJob : .NET Core 2.2.1 (CoreCLR 4.6.27207.03, CoreFX 4.6.27207.03), 64bit RyuJIT


```
| Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
|------------------------------------------------ |--------------:|--------------:|--------------:|--------------:|--------------:|---------:|--------:|------------:|------------:|------------:|--------------------:|
| JsonLocalizer | 68.83 ns | 0.1259 ns | 0.1116 ns | 68.58 ns | 68.95 ns | 0.63 | 0.00 | 0.0228 | - | - | 48 B |
| JsonLocalizerWithCreation | 512,929.54 ns | 1,795.6669 ns | 1,679.6679 ns | 510,100.03 ns | 515,499.84 ns | 4,678.08 | 13.62 | 83.0078 | 29.2969 | 4.8828 | 174968 B |
| JsonLocalizerWithCreationAndExternalMemoryCache | 4,930.42 ns | 20.7277 ns | 18.3746 ns | 4,888.34 ns | 4,956.98 ns | 44.98 | 0.18 | 1.7624 | 0.8774 | - | 3712 B |
| Localizer | 109.62 ns | 0.1763 ns | 0.1563 ns | 109.48 ns | 110.05 ns | 1.00 | 0.00 | - | - | - | - |
| Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------------------------------------ |--------------:|--------------:|--------------:|--------------:|--------------:|----------:|---------:|--------:|--------:|-------:|----------:|
| JsonLocalizer | 55.49 ns | 1.176 ns | 2.454 ns | 52.55 ns | 61.80 ns | 1.00 | 0.00 | 0.0114 | - | - | 48 B |
| Localizer | 98.09 ns | 1.176 ns | 1.042 ns | 96.39 ns | 100.23 ns | 1.78 | 0.06 | - | - | - | - |
| JsonLocalizerWithCreation | 647,389.65 ns | 19,441.235 ns | 56,402.548 ns | 573,985.62 ns | 814,434.86 ns | 11,620.42 | 1,268.33 | 41.0156 | 20.5078 | 1.9531 | 174688 B |
| JsonLocalizerWithCreationAndExternalMemoryCache | 5,120.16 ns | 99.768 ns | 152.356 ns | 4,962.78 ns | 5,522.11 ns | 91.02 | 5.12 | 0.9384 | 0.4654 | - | 3944 B |
| JsonLocalizerDefault | 268.60 ns | 1.787 ns | 1.492 ns | 266.84 ns | 271.32 ns | 4.88 | 0.15 | 0.0892 | - | - | 376 B |
| LocalizerDefault | 316.64 ns | 4.932 ns | 4.614 ns | 311.53 ns | 326.27 ns | 5.73 | 0.19 | 0.0777 | - | - | 328 B |
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Method Job AnalyzeLaunchVariance EvaluateOverhead MaxAbsoluteError MaxRelativeError MinInvokeCount MinIterationTime OutlierMode Affinity EnvironmentVariables Jit Platform Runtime AllowVeryLargeObjects Concurrent CpuGroups Force HeapAffinitizeMask HeapCount NoAffinitize RetainVm Server Arguments BuildConfiguration Clock EngineFactory NuGetReferences Toolchain IsMutator InvocationCount IterationCount IterationTime LaunchCount MaxIterationCount MaxWarmupIterationCount MinIterationCount MinWarmupIterationCount RunStrategy UnrollFactor WarmupCount Mean Error StdDev Min Max Ratio RatioSD Gen 0/1k Op Gen 1/1k Op Gen 2/1k Op Allocated Memory/Op
JsonLocalizer Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default 68.83 ns 0.1259 ns 0.1116 ns 68.58 ns 68.95 ns 0.63 0.00 0.0228 - - 48 B
JsonLocalizerWithCreation Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default "512,929.54 ns" "1,795.6669 ns" "1,679.6679 ns" "510,100.03 ns" "515,499.84 ns" "4,678.08" 13.62 83.0078 29.2969 4.8828 174968 B
JsonLocalizerWithCreationAndExternalMemoryCache Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default "4,930.42 ns" 20.7277 ns 18.3746 ns "4,888.34 ns" "4,956.98 ns" 44.98 0.18 1.7624 0.8774 - 3712 B
Localizer Default False Default Default Default Default Default Default 0000 Empty RyuJit X64 Core False True False True Default Default False False False Default Default Default Default Default Default Default 1 Default Default Default Default Default Default Default Default 16 Default 109.62 ns 0.1763 ns 0.1563 ns 109.48 ns 110.05 ns 1.00 0.00 - - - -
Method;Job;AnalyzeLaunchVariance;EvaluateOverhead;MaxAbsoluteError;MaxRelativeError;MinInvokeCount;MinIterationTime;OutlierMode;Affinity;EnvironmentVariables;Jit;Platform;Runtime;AllowVeryLargeObjects;Concurrent;CpuGroups;Force;HeapAffinitizeMask;HeapCount;NoAffinitize;RetainVm;Server;PowerPlan;Arguments;BuildConfiguration;Clock;EngineFactory;NuGetReferences;Toolchain;IsMutator;InvocationCount;IterationCount;IterationTime;LaunchCount;MaxIterationCount;MaxWarmupIterationCount;MinIterationCount;MinWarmupIterationCount;RunStrategy;UnrollFactor;WarmupCount;Mean;Error;StdDev;Min;Max;Ratio;RatioSD;Gen 0;Gen 1;Gen 2;Allocated
JsonLocalizer;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;55.49 ns;1.176 ns;2.454 ns;52.55 ns;61.80 ns;1.00;0.00;0.0114;-;-;48 B
Localizer;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;98.09 ns;1.176 ns;1.042 ns;96.39 ns;100.23 ns;1.78;0.06;-;-;-;-
JsonLocalizerWithCreation;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;"647,389.65 ns";"19,441.235 ns";"56,402.548 ns";"573,985.62 ns";"814,434.86 ns";"11,620.42";"1,268.33";41.0156;20.5078;1.9531;174688 B
JsonLocalizerWithCreationAndExternalMemoryCache;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;"5,120.16 ns";99.768 ns;152.356 ns;"4,962.78 ns";"5,522.11 ns";91.02;5.12;0.9384;0.4654;-;3944 B
JsonLocalizerDefault;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;268.60 ns;1.787 ns;1.492 ns;266.84 ns;271.32 ns;4.88;0.15;0.0892;-;-;376 B
LocalizerDefault;Default;False;Default;Default;Default;Default;Default;Default;11111111;Empty;RyuJit;X64;Core;False;True;False;True;Default;Default;False;False;False;Default;Default;Default;Default;Default;Default;Default;Default;1;Default;Default;Default;Default;Default;Default;Default;Default;16;Default;316.64 ns;4.932 ns;4.614 ns;311.53 ns;326.27 ns;5.73;0.19;0.0777;-;-;328 B

0 comments on commit 8f2ba14

Please sign in to comment.