Skip to content

Commit

Permalink
optimize ws lookup when populating exemplars by using a frozen dictio…
Browse files Browse the repository at this point in the history
…nary, and set the MultiString capacity when converting a LcmMultiString to a MiniLcmMultiString
  • Loading branch information
hahn-kev committed Jun 14, 2024
1 parent d3b25f2 commit c351dcd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions backend/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FwDataMiniLcmBridge.Api.UpdateProxy;
using System.Collections.Frozen;
using FwDataMiniLcmBridge.Api.UpdateProxy;
using Microsoft.Extensions.Logging;
using MiniLcm;
using SIL.LCModel;
Expand Down Expand Up @@ -138,7 +139,7 @@ internal void CompleteExemplars(WritingSystems writingSystems)
var wsExemplars = writingSystems.Vernacular.Concat(writingSystems.Analysis)
.Distinct()
.ToDictionary(ws => ws, ws => ws.Exemplars.Select(s => s[0]).ToHashSet());
var wsExemplarsByHandle = wsExemplars.ToDictionary(kv => GetWritingSystemHandle(kv.Key.Id), kv => kv.Value);
var wsExemplarsByHandle = wsExemplars.ToFrozenDictionary(kv => GetWritingSystemHandle(kv.Key.Id), kv => kv.Value);

foreach (var entry in _entriesRepository.AllInstances())
{
Expand Down Expand Up @@ -235,7 +236,7 @@ private ExampleSentence FromLexExampleSentence(ILexExampleSentence sentence)

private MultiString FromLcmMultiString(ITsMultiString multiString)
{
var result = new MultiString();
var result = new MultiString(multiString.StringCount);
for (var i = 0; i < multiString.StringCount; i++)
{
var tsString = multiString.GetStringFromIndex(i, out var ws);
Expand Down
3 changes: 2 additions & 1 deletion backend/FwDataMiniLcmBridge/Api/LcmHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal static bool SearchValue(this ITsMultiString multiString, string value)
'\u0640', // Arabic Tatweel
];

internal static void ContributeExemplars(ITsMultiString multiString, Dictionary<int, HashSet<char>> wsExemplars)
internal static void ContributeExemplars(ITsMultiString multiString, IReadOnlyDictionary<int, HashSet<char>> wsExemplars)
{
for (var i = 0; i < multiString.StringCount; i++)
{
Expand All @@ -62,6 +62,7 @@ internal static void ContributeExemplars(ITsMultiString multiString, Dictionary<
var value = tsString.Text.AsSpan().Trim(WhitespaceAndFormattingChars);
if (!value.IsEmpty && wsExemplars.TryGetValue(ws, out var exemplars))
{
//todo should we upper or lowercase the value?
exemplars.Add(value[0]);
}
}
Expand Down
8 changes: 8 additions & 0 deletions backend/MiniLcm/MultiString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace MiniLcm;
[JsonConverter(typeof(MultiStringConverter))]
public class MultiString: IDictionary
{
public MultiString(int capacity)
{
Values = new MultiStringDict(capacity);
}
public MultiString()
{
Values = new MultiStringDict();
Expand Down Expand Up @@ -41,6 +45,10 @@ private class MultiStringDict : Dictionary<WritingSystemId, string>,
#pragma warning restore CS8644 // Type does not implement interface member. Nullability of reference types in interface implemented by the base type doesn't match.
IDictionary
{
public MultiStringDict(int capacity) : base(capacity)
{

}
public MultiStringDict()
{
}
Expand Down

0 comments on commit c351dcd

Please sign in to comment.