perf(ui): retain shaped report text
This commit is contained in:
parent
c0bec56dfe
commit
f2a015be8e
11 changed files with 525 additions and 38 deletions
79
tests/AcDream.App.Tests/UI/Layout/UiTextLayoutCacheTests.cs
Normal file
79
tests/AcDream.App.Tests/UI/Layout/UiTextLayoutCacheTests.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using AcDream.App.UI;
|
||||
using AcDream.App.UI.Layout;
|
||||
|
||||
namespace AcDream.App.Tests.UI.Layout;
|
||||
|
||||
public sealed class UiTextLayoutCacheTests
|
||||
{
|
||||
[Fact]
|
||||
public void Stable_content_and_layout_borrow_same_shaped_lines()
|
||||
{
|
||||
var text = new UiText { Width = 120f };
|
||||
int shapes = 0;
|
||||
var cache = new UiTextLayoutCache<string>(
|
||||
text,
|
||||
(target, value) =>
|
||||
{
|
||||
shapes++;
|
||||
return IndicatorDetailText.Shape(target, value);
|
||||
},
|
||||
"alpha beta gamma",
|
||||
StringComparer.Ordinal);
|
||||
|
||||
IReadOnlyList<UiText.Line> first = cache.GetLines();
|
||||
IReadOnlyList<UiText.Line> second = cache.GetLines();
|
||||
|
||||
Assert.Same(first, second);
|
||||
Assert.Equal(1, shapes);
|
||||
|
||||
cache.SetValue(new string("alpha beta gamma".ToCharArray()));
|
||||
Assert.Same(first, cache.GetLines());
|
||||
Assert.Equal(1, shapes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Content_width_and_font_inputs_invalidate_shaping()
|
||||
{
|
||||
var text = new UiText { Width = 120f };
|
||||
int shapes = 0;
|
||||
var cache = new UiTextLayoutCache<string>(
|
||||
text,
|
||||
(target, value) =>
|
||||
{
|
||||
shapes++;
|
||||
return IndicatorDetailText.Shape(target, value);
|
||||
},
|
||||
"alpha beta gamma",
|
||||
StringComparer.Ordinal);
|
||||
|
||||
IReadOnlyList<UiText.Line> first = cache.GetLines();
|
||||
cache.SetValue("delta epsilon");
|
||||
IReadOnlyList<UiText.Line> changed = cache.GetLines();
|
||||
text.Width = 48f;
|
||||
IReadOnlyList<UiText.Line> resized = cache.GetLines();
|
||||
|
||||
Assert.NotSame(first, changed);
|
||||
Assert.NotSame(changed, resized);
|
||||
Assert.Equal(3, shapes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Stable_provider_poll_allocates_no_managed_memory()
|
||||
{
|
||||
var text = new UiText { Width = 120f };
|
||||
var cache = new UiTextLayoutCache<string>(
|
||||
text,
|
||||
static (target, value) => IndicatorDetailText.Shape(target, value),
|
||||
"alpha beta gamma",
|
||||
StringComparer.Ordinal);
|
||||
Func<IReadOnlyList<UiText.Line>> provider = cache.Provider;
|
||||
_ = provider();
|
||||
|
||||
long before = GC.GetAllocatedBytesForCurrentThread();
|
||||
for (int i = 0; i < 1_000; i++)
|
||||
_ = provider();
|
||||
long allocated = GC.GetAllocatedBytesForCurrentThread() - before;
|
||||
|
||||
Assert.Equal(0L, allocated);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue