Skip to content

Commit 457b4f7

Browse files
fix bug with lru cache
1 parent 8333e26 commit 457b4f7

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

Flamui/Drawing/FontLoader.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,13 @@ public class FontAtlas
110110

111111
public GpuTexture GpuTexture;
112112

113-
public required LRUCache<char, AtlasGlyphInfo> Table;
113+
public required LRUCache<int, AtlasGlyphInfo> Table;
114114

115115
public unsafe AtlasGlyphInfo FindGlyphEntry(char c, float resolutionMultiplier)
116116
{
117-
if (Table.TryGet(c, out var entry))
117+
var hash = HashCode.Combine(c, resolutionMultiplier);
118+
119+
if (Table.TryGet(hash, out var entry))
118120
{
119121
return entry;
120122
}
@@ -160,7 +162,7 @@ public unsafe AtlasGlyphInfo FindGlyphEntry(char c, float resolutionMultiplier)
160162
Scale = Font.Scale
161163
};
162164

163-
Table.Add(c, info);
165+
Table.Add(hash, info);
164166

165167
return info;
166168
}
@@ -230,13 +232,13 @@ public static unsafe Font LoadFont(string name)
230232

231233
public static FontAtlas CreateFontAtlas(ScaledFont scaledFont)
232234
{
233-
var table = new LRUCache<char, AtlasGlyphInfo>(10*10);
235+
var table = new LRUCache<int, AtlasGlyphInfo>(10*10);
234236

235237
for (int i = 1; i < 11; i++)
236238
{
237239
for (int j = 1; j < 11; j++)
238240
{
239-
table.Add((char)(int.MaxValue - i - 100 * j), default(AtlasGlyphInfo) with
241+
table.Add((int.MaxValue - i - 100 * j).GetHashCode(), default(AtlasGlyphInfo) with
240242
{
241243
AtlasX = i * 100,
242244
AtlasY = j * 100,

Flamui/Drawing/GlCanvas.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Diagnostics;
22
using System.Numerics;
3-
using System.Text.Json;
43
using Silk.NET.Maths;
54
using Silk.NET.OpenGL;
65

@@ -36,6 +35,8 @@ public void DrawText(ReadOnlySpan<char> text, float x, float y)
3635
{
3736
var resolutionMultiplier = new Vector2(1, 1).Multiply(MeshBuilder.Matrix.GetScale()).Y;
3837

38+
Console.WriteLine(resolutionMultiplier);
39+
3940
var fontAtlas = _renderer.GetFontAtlas(Paint.Font);
4041

4142
var xCoord = x;

Flamui/LRUCache.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public TValue GetLeastUsed()
3030
}
3131

3232
public bool TryGet(TKey key, out TValue value) {
33+
3334
if(dictionary.TryGetValue(key, out var node))
3435
{
3536
Remove(node);
@@ -91,23 +92,35 @@ private void Remove(LRUCacheEntry entry)
9192
{
9293
if (entry == head)
9394
{
94-
entry.Next.Prev = entry.Prev;
95+
head = entry.Next;
96+
head.Prev = entry.Prev;
97+
98+
entry.Prev = null;
99+
entry.Next = null;
100+
95101
return;
96102
}
97103

104+
98105
entry.Prev.Next = entry.Next;
99106
if (entry.Next != null)
100107
{
101108
entry.Next.Prev = entry.Prev;
102109
}
103110
else
104111
{
112+
entry.Prev.Next = null;
105113
head.Prev = entry.Prev;
106114
}
115+
116+
entry.Prev = null;
117+
entry.Next = null;
118+
107119
}
108120

109121
private LRUCacheEntry AddFirst(TKey key, TValue value)
110122
{
123+
111124
var entry = new LRUCacheEntry
112125
{
113126
Key = key,
@@ -118,4 +131,29 @@ private LRUCacheEntry AddFirst(TKey key, TValue value)
118131

119132
return entry;
120133
}
134+
135+
private void ValidateTable()
136+
{
137+
if (head == null)
138+
return;
139+
140+
var current = head;
141+
int counter = 1;
142+
143+
while (true)
144+
{
145+
current = current.Next;
146+
147+
if (current == null)
148+
{
149+
// Debug.Assert(counter == capacity);
150+
break;
151+
}
152+
153+
if(counter > capacity)
154+
Debug.Assert(false);
155+
156+
counter++;
157+
}
158+
}
121159
}

0 commit comments

Comments
 (0)