Browse Source

[Text] Add textblocks virtualization benchmark, rename field

pull/9954/head
Sergey Mikolaitis 3 years ago
parent
commit
56e94e424c
  1. 8
      src/Avalonia.Base/Media/TextFormatting/ShapedBuffer.cs
  2. 21
      tests/Avalonia.Benchmarks/Text/HugeTextLayout.cs

8
src/Avalonia.Base/Media/TextFormatting/ShapedBuffer.cs

@ -8,12 +8,12 @@ namespace Avalonia.Media.TextFormatting
public sealed class ShapedBuffer : IList<GlyphInfo>, IDisposable
{
private static readonly IComparer<GlyphInfo> s_clusterComparer = new CompareClusters();
private bool _rented;
private bool _bufferRented;
public ShapedBuffer(CharacterBufferRange characterBufferRange, int bufferLength, IGlyphTypeface glyphTypeface, double fontRenderingEmSize, sbyte bidiLevel) :
this(characterBufferRange, ArrayPool<GlyphInfo>.Shared.Rent(bufferLength), glyphTypeface, fontRenderingEmSize, bidiLevel)
{
_rented = true;
_bufferRented = true;
Length = bufferLength;
}
@ -268,7 +268,7 @@ namespace Avalonia.Media.TextFormatting
public void Dispose()
{
GC.SuppressFinalize(this);
if (_rented)
if (_bufferRented)
{
GlyphInfos.ReturnRent();
}
@ -276,7 +276,7 @@ namespace Avalonia.Media.TextFormatting
~ShapedBuffer()
{
if (_rented)
if (_bufferRented)
{
GlyphInfos.ReturnRent();
}

21
tests/Avalonia.Benchmarks/Text/HugeTextLayout.cs

@ -1,5 +1,6 @@
using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
using Avalonia.UnitTests;
@ -70,6 +71,26 @@ In respect that the structure of the sufficient amount poses problems and challe
[Benchmark]
public TextLayout[] BuildManySmallTexts() => _manySmallStrings.Select(MakeLayout).ToArray();
[Benchmark]
public void VirtualizeTextBlocks()
{
var blocks = new TextBlock[10];
for (var i = 0; i < blocks.Length; i++)
{
blocks[i] = new TextBlock
{
Width = 120,
Height = 32,
};
}
for (int i = 0, j = 0; i < _manySmallStrings.Length; i++, j = j < blocks.Length - 1 ? j + 1 : 0)
{
blocks[j].Text = _manySmallStrings[i];
blocks[j].Measure(new Size(200, 200));
}
}
private static TextLayout MakeLayout(string str)
{
var layout = new TextLayout(str, Typeface.Default, 12d, Brushes.Black, maxWidth: 120);

Loading…
Cancel
Save