Skip to content

Commit

Permalink
improve dictionary performance/memory footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
aspriddell committed May 22, 2024
1 parent 34045cc commit 02a2fe6
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion DragonFruit.OnionFruit.Web.Worker/CountryMap.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// OnionFruit™ Web Copyright DragonFruit Network <inbox@dragonfruit.network>
// Licensed under Apache-2. Refer to the LICENSE file for more info

using System.Collections.Frozen;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace DragonFruit.OnionFruit.Web.Worker
{
public class CountryMap
public class CountryMap : IJsonOnDeserialized
{
static CountryMap()
{
Expand All @@ -34,5 +35,14 @@ static CountryMap()
/// Gets the country name associated with the provided <see cref="code"/>, or <c>null</c> if not found.
/// </summary>
public string GetCountryName(string code) => CodeMap.GetValueOrDefault(code.ToUpperInvariant());

void IJsonOnDeserialized.OnDeserialized()
{
// convert the dictionary to a frozen dictionary if it's not already
if (CodeMap.GetType().GetGenericTypeDefinition() != typeof(FrozenDictionary<,>))
{
CodeMap = CodeMap.ToFrozenDictionary();
}
}
}
}

0 comments on commit 02a2fe6

Please sign in to comment.