Skip to content

Commit 0fc8e52

Browse files
committed
perf(android): Use HashTableEx in FontHelper
1 parent 81da622 commit 0fc8e52

File tree

2 files changed

+90
-12
lines changed

2 files changed

+90
-12
lines changed

src/Uno.UI/UI/Xaml/Extensions/FontHelper.Android.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515

1616
namespace Windows.UI.Xaml
1717
{
18-
internal class FontHelper
19-
{
20-
internal static readonly Func<FontFamily, FontWeight, TypefaceStyle, Typeface> _fontFamilyToTypeFace;
18+
internal partial class FontHelper
19+
{
2120
private static bool _assetsListed;
2221
private static readonly string DefaultFontFamilyName = "sans-serif";
2322

24-
static FontHelper()
25-
{
26-
_fontFamilyToTypeFace = Funcs
27-
.Create<FontFamily, FontWeight, TypefaceStyle, Typeface>(InternalFontFamilyToTypeFace)
28-
.AsLockedMemoized();
29-
}
30-
3123
internal static Typeface FontFamilyToTypeFace(FontFamily fontFamily, FontWeight fontWeight, TypefaceStyle style = TypefaceStyle.Normal)
32-
{
33-
return _fontFamilyToTypeFace(fontFamily, fontWeight, style);
24+
{
25+
var entry = new FontFamilyToTypeFaceDictionary.Entry(fontFamily.Source, fontWeight, style);
26+
27+
if (!_fontFamilyToTypeFaceDictionary.TryGetValue(entry , out var typeFace))
28+
{
29+
typeFace = InternalFontFamilyToTypeFace(fontFamily, fontWeight, style);
30+
_fontFamilyToTypeFaceDictionary.Add(entry, typeFace);
31+
}
32+
33+
return typeFace;
3434
}
3535

3636
internal static Typeface InternalFontFamilyToTypeFace(FontFamily fontFamily, FontWeight fontWeight, TypefaceStyle style)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Android.App;
2+
using Android.Graphics;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using Uno.Extensions;
7+
using Uno;
8+
using Uno.UI;
9+
using System.Linq;
10+
using Android.OS;
11+
using Windows.UI.Xaml.Media;
12+
using Windows.UI.Text;
13+
using Uno.Logging;
14+
using Microsoft.Extensions.Logging;
15+
using Uno.Collections;
16+
17+
namespace Windows.UI.Xaml
18+
{
19+
internal partial class FontHelper
20+
{
21+
private readonly static FontFamilyToTypeFaceDictionary _fontFamilyToTypeFaceDictionary = new FontFamilyToTypeFaceDictionary();
22+
23+
private class FontFamilyToTypeFaceDictionary
24+
{
25+
public class Entry
26+
{
27+
private readonly int _hashCode;
28+
29+
public Entry(string fontFamily, FontWeight fontWeight, TypefaceStyle style)
30+
{
31+
FontFamily = fontFamily;
32+
FontWeight = fontWeight;
33+
Style = style;
34+
_hashCode = FontFamily.GetHashCode() ^ FontWeight.GetHashCode() ^ Style.GetHashCode();
35+
}
36+
37+
public string FontFamily { get; }
38+
public FontWeight FontWeight { get; }
39+
public TypefaceStyle Style { get; }
40+
41+
public override bool Equals(object other)
42+
{
43+
if (other is Entry otherEntry)
44+
{
45+
return FontFamily == otherEntry.FontFamily
46+
&& FontWeight == otherEntry.FontWeight
47+
&& Style == otherEntry.Style;
48+
}
49+
50+
return false;
51+
}
52+
53+
public override int GetHashCode() => _hashCode;
54+
}
55+
56+
private readonly HashtableEx _entries = new HashtableEx();
57+
58+
internal bool TryGetValue(Entry key, out Typeface result)
59+
{
60+
if (_entries.TryGetValue(key, out var value))
61+
{
62+
result = (Typeface)value;
63+
return true;
64+
}
65+
66+
result = null;
67+
return false;
68+
}
69+
70+
internal void Add(Entry key, Typeface typeFace)
71+
=> _entries.Add(key, typeFace);
72+
73+
internal void Clear()
74+
=> _entries.Clear();
75+
}
76+
77+
}
78+
}

0 commit comments

Comments
 (0)