Avalonia Field Guide
About

Module 4 · Lesson 14 of 16

Ten thousand rows, thirty controls

A virtualizing panel creates containers for the viewport, not for the data. Count them instead of trusting the demo.

30 minHeadless-tested

Most "Avalonia is slow" reports are one of three things: too many controls, too much layout, or work on the UI thread. This lesson is about the first one, because it is the easiest to measure.

Screenshot of the performance lesson sample
The lesson's sample, rendered headlessly at 1280×800. Light and dark are both captured.

Virtualization is the default — until it isn't

ListBox uses a virtualizing panel. It creates containers for the rows that fit on screen, plus a small buffer, and recycles them as you scroll.

src/CourseApp/Lessons/16_Performance/PerfView.axaml:20–42
                             ItemsSource="{Binding Rows}"
                             Height="380"
                             BorderThickness="0"
                             Background="Transparent">
                        <ListBox.ItemTemplate>
                            <DataTemplate x:DataType="local:PerfRow">
                                <Grid ColumnDefinitions="72,*,90" Margin="2,2">
                                    <TextBlock Text="{Binding Index}" Classes="caption" />
                                    <TextBlock Grid.Column="1" Text="{Binding Name}" />
                                    <TextBlock Grid.Column="2" Text="{Binding Bucket}" Classes="caption" />
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>
            </Border>

            <StackPanel Spacing="6" MaxWidth="760" HorizontalAlignment="Left">
                <TextBlock Classes="caption" TextWrapping="Wrap"
                           Text="The harness asserts this: 10,000 items in the source, fewer than 200 ListBoxItem containers in the visual tree. Replace the panel with a plain StackPanel and that number becomes 10,000." />
                <TextBlock Classes="code" Text="&lt;ListBox ItemsSource=&quot;{Binding Rows}&quot; /&gt;  // virtualized by default" />
            </StackPanel>
        </StackPanel>

The data is not the cost. Ten thousand records are a few megabytes. Ten thousand ListBoxItem controls, each with a template, a style lookup, and a layout pass, are the cost.

The classic mistake is wrapping an ItemsControl in a ScrollViewer. The scroll viewer measures with infinite height, the panel decides every item fits, and virtualization quietly stops working. If a list is slow, count containers first.

How this page proves it

var realized = view.GetVisualDescendants().OfType<ListBoxItem>().Count();
if (realized > 200)
    throw new InvalidOperationException($"virtualization failed: {realized} containers");

Ten thousand rows, fewer than two hundred containers. That assertion runs on every build, so a change that disables virtualization fails the course rather than shipping.

The other two levers

  • Compiled bindings (default in 12) remove reflection from the binding path. A {Binding} inside a row template runs for every realized row, every update.
  • Layout thrash comes from changing width, margin, or visibility in a loop. Measure once, mutate the model, and let one layout pass do the work.

Prefer ItemsControl when you do not need selection or scrolling — it is lighter than ListBox. Prefer a plain TextBlock over a StackPanel of Runs. Prefer StringFormat over a converter for display-only formatting.

Measure, do not guess

dotnet-counters monitor --process-id <pid> System.Runtime

A Stopwatch around the suspicious method is usually enough to prove where the time goes. Screenshots and vibes are not measurements.

Try it

  1. Replace the ListBox with an ItemsControl inside a ScrollViewer, run the verifier, and watch the container count explode. Revert.
  2. Add a search box that filters the collection and confirm the assertion still passes with 10,000 items.
  3. Time how long the page takes to measure and arrange with 1,000 versus 10,000 rows. The difference should be nearly flat.

Next: proof, not vibes.

Files this lesson draws from

  • src/CourseApp/Lessons/16_Performance/PerfView.axaml
  • src/CourseApp/Lessons/16_Performance/PerfViewModel.cs

VerifiedThe verifier hosts the list, asserts 10,000 source rows, and counts fewer than 200 realized row containers in the visual tree.

Back to top