Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs/linux-port/investigations/ui-slowness.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# UI slowness investigation (software rendering, headless)

Question: why does the UI feel slow (~2 s per action reported on Debian 13 KDE), and is it fixable? Everything below was
measured in private Xvfb runs (`scripts/linux/headless-run.sh`, software rendering, `nice -n 19`, Debug build, JIT) on a
16-core desktop CPU with low load. A laptop CPU will be roughly 2-3x slower, which matches the reports. Real KDE
compositor/GPU behaviour is not covered.

## Method

- Wall time: `ffmpeg x11grab` at 20 fps on the private display, hashing each frame. For every action (xdotool input,
timestamped by a marker script) the table gives `first` (input to first visual change) and `settle` (input to the last
change before 700 ms of stillness). Resolution is 50 ms.
- UI-thread stalls: `FILES_TRACE=1` now also starts a 10 ms dispatcher timer that logs `[files-ui] stall-ms=N` whenever a
tick arrives more than 50 ms late (`UiResponsivenessProbe`). `max stall` is the longest single block of the UI thread.
- Where the time goes: `dotnet-trace collect --profile dotnet-sampled-thread-time --format Speedscope` around one action,
then inclusive/exclusive time of the UI thread (`Uno_Event_Loop`) from the Speedscope JSON.
- Fixtures: folders with 1,000 and 10,000 empty files of ten extensions, plus a 5-file folder.
- Idle: per-thread CPU ticks over 30 s with no input.

## Results (ms; before -> after the checkbox change; two runs each, they agreed within ~10%)

| Action | settle before | settle after | max UI stall before -> after | Top contributors (UI thread) |
| --- | --- | --- | --- | --- |
| Startup to Home painted (Debug/JIT) | ~4700 | n/a | n/a | first paint 2.0 s; JIT, XAML load, Home page |
| Navigate into 1k folder | 1370 | 1020-1120 | 384 -> 223 | item realization/measure (~45%), Skia picture record on UI thread (~30%), StatusBar forced `UpdateLayout` (~15%) |
| Navigate into 10k folder | 675 | 540-585 | 295 -> 239 | same (virtualized, so ~1k cost) |
| Layout details -> grid (1k) | 762 | 460-560 | 273 -> 118 | realizing the new panel's items |
| Layout grid -> list (1k) | 1109 | 1108-1206 | 1142 -> 585 | item realization: 2.4 s CPU -> 1.1 s CPU in the profile |
| Layout list -> cards (1k) | 1116 | 815-864 | 531 -> 370 | item realization |
| Layout cards -> details (1k) | 524 | 273 | 129 -> 153 | item realization |
| Select all, 1k | 1481 | 378-429 | 147 -> 216 | selection events, checkbox state |
| Select all, 10k | 1771 | 1736-1782 | 796 -> 802 | Uno `SelectedItems.Add` per item: `ContainerFromItem` + locked `BulkConcurrentObservableCollection.Count` (O(n^2)) |
| Sort by column click (1k / 10k) | 190-250 | same | 260 | sort + relayout; 10k sort profiles at ~20 ms CPU |
| Context menu: file / folder / empty | 130-190 / 430 / 70 | same | 70-170 | `context-menu-build` 120-230 ms on the UI thread (trace), flyout open ~100 ms |
| Open Settings | 360 | 330 | 230 | window/page creation |
| New tab / close tab | 410-610 / 870-1070 | 460-860 / 740 | 70-170 / 290 | page construction, item realization of the tab shown again |
| Window resize | 90 / 76 | same | up to 130 | relayout |
| Search box typing (8 chars) | n/a | n/a | no stalls | typing path is cheap; results come from a worker |
| Idle (30 s, no input) | 0 ticks on every thread | | | no repaint loop |

Visual changes at idle: 0 in 5 s. Idle CPU is zero because nothing requests a frame. Caveat found while measuring: simply
subscribing to `CompositionTarget.Rendering` makes Uno render at 60 fps (about 8% CPU idle), so do not add such a probe.

## Findings

1. **Rendering backend.** `RenderingBackendSelector` picks Uno's software renderer when no GPU node exists or
`LIBGL_ALWAYS_SOFTWARE`/llvmpipe is set; headless runs report `Software (LIBGL_ALWAYS_SOFTWARE is set)`. Raster
happens on `X11RenderThread`; it was idle in all profiles. Frame invalidation does not cause continuous full-window
repaints: idle is 0 frames and 0 CPU.
2. **The UI thread, not rasterization, is the bottleneck.** Skia raster runs off-thread. What blocks the UI thread is
(a) creating and measuring item containers (Uno `VirtualizingPanelLayout.FillLayout`, templates, bindings) and (b) Uno's
`RecordPictureAndReturnPath` (picture recording and `SKPath.Op` clip math), which runs on the UI thread. Both are CPU
bound and independent of GPU, so software rendering is not the main cause; a GPU would not remove (a) or (b).
3. **Per-item `CheckBox` cost (fixed here).** Every file item has a selection CheckBox. Uno's default CheckBox template
builds an `AnimatedIcon` with a Lottie visual and expression animations per instance, even though the box is invisible
(`Opacity=0`) until hover/selection. In the profile this was about a third of the time to realize a list/grid/details
panel (AnimatedIcon construct + arrange + animation re-evaluation + the GC pressure they cause). Fixed with a plain
border + path template (`Files.SelectionCheckBoxStyle`).
4. **Icons/thumbnails** do not block the UI: icon resolution and file enumeration run on worker threads
(`folder-enumeration-batch` 0.6-1.5 ms per batch of 25 on the worker). `folder-enumeration` for 10k files was 175-400 ms
wall, off the UI thread.
5. **No sync-over-async stalls were seen** in the traced actions. The only blocked-UI pattern was lock contention
overhead in `BulkConcurrentObservableCollection.Count` during select-all of 10k items.

## Remaining larger ideas (not done)

- Select-all of 10k items is O(n^2) inside Uno's `ListViewBase` (per-item `SelectedItems.Add`). Needs either a bulk
selection path (suppress per-item container lookups; set selection ranges) or a lock-free `Count`/`IndexOf` snapshot.
- Item templates are still heavy (bindings, `x:Bind` updates, `Files.ListViewItemTemplate` build ~200 ms per screenful).
Candidates: fewer elements per row, `x:Phase`/deferred columns, `x:Load` for rarely visible parts (shield icon, tag
badges, rename popup), reuse via a smaller recycling pool.
- `StatusBar.QueueRemeasure` calls the global `UpdateLayout()` from a property-changed handler (about 165-230 ms once per
navigation, because it forces the item layout early). Measuring only the bar's own panels would avoid the forced
global pass.
- Uno picture recording (`RecordPictureAndReturnPath`, `SKPath.Op`) takes 250-400 ms of UI time per navigation. Fixing it
is upstream (dirty-region/clip handling) or by reducing the number of clipped/animated elements.
- Startup: ~4.7 s to a painted Home page in a Debug JIT build; ReadyToRun/AOT builds and trimming the work done before
the first frame (service construction, settings load) should be measured separately on a release build.
- Context menu build runs on the UI thread (120-230 ms); moving the model building to a worker and only creating
flyout items on the UI thread would cut it roughly in half.

## Reproducing

```bash
FILES_TRACE=1 FILES_SANDBOX_SEED=<seed script> FILES_SANDBOX_DRIVES=scripts/linux/showcase-drives.txt \
nice -n 19 scripts/linux/headless-run.sh -s 20 -o /tmp/out -a <actions>
```

where the actions file records the screen (`run <ffmpeg x11grab ... -f framemd5>`), sends xdotool input and writes
timestamp markers; `[files-ui]` lines in `app.log` give stalls. Only the private Xvfb display is used.
6 changes: 6 additions & 0 deletions docs/linux-port/performance-tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ message bodies, credentials, or process output are included.
| `folder-enumeration` | Batch enumerator lifetime, including consumer time between batches. |
| `folder-enumeration-batch` | Directory entries/metadata read on a worker, excluding consumer/UI rendering time. |

With tracing enabled, a 10 ms dispatcher timer queues `[files-ui] t=<unix ms> stall-ms=<n>` whenever tick lateness
(the interval between ticks minus 10 ms) exceeds 50 ms. A worker writes these timing-only records to stderr;
its separate buffer holds at most 128 records and drops new records when full. Tick lateness includes timer scheduling
delays and is not a direct measurement of time spent blocking the UI thread. Disabled startup still calls the probe
and initializes the trace flag, but creates no probe closure, timer, logging worker or buffer.

`completed-or-unwound` means the scope ended, including exception/cancellation paths; it does not claim success.
D-Bus records explicitly report `reply`/`fault`. Correlate caller logs to distinguish cancellations/timeouts.

Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Platforms/Desktop/App.Desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async Task ActivateAsync()

MainWindow.Instance.RestorePlacement(Ioc.Default.GetRequiredService<Files.Platform.Abstractions.ILocalSettingsStore>());
MainWindow.Instance.Activate();
UiResponsivenessProbe.Start(UiDispatcher!);

MainWindow.Instance.Closed += Window_Closed;
MainWindow.Instance.Activated += Window_Activated;
Expand Down
66 changes: 66 additions & 0 deletions src/Files.App/Platforms/Desktop/UiResponsivenessProbe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using Microsoft.UI.Dispatching;
using System.Diagnostics;
using System.Threading.Channels;

namespace Files.App
{
/// <summary>
/// Opt-in (FILES_TRACE=1) probe that reports dispatcher tick lateness on a background worker.
/// It observes only timing, never content, and avoids CompositionTarget.Rendering, which makes Uno render continuously.
/// </summary>
internal static class UiResponsivenessProbe
{
private const int TickMs = 10;
private const double StallThresholdMs = 50;

private static DispatcherQueueTimer? timer;

public static void Start(DispatcherQueue queue)
{
if (!Files.Platform.Abstractions.Diagnostics.PerformanceTrace.Enabled || timer is not null)
return;

StartEnabled(queue);
}

private static void StartEnabled(DispatcherQueue queue)
{
var records = Channel.CreateBounded<(long Timestamp, double Lateness)>(new BoundedChannelOptions(128)
{
FullMode = BoundedChannelFullMode.DropWrite,
SingleReader = true,
SingleWriter = true,
AllowSynchronousContinuations = false,
});
_ = Task.Run(async () =>
{
await foreach (var record in records.Reader.ReadAllAsync().ConfigureAwait(false))
{
try
{
Console.Error.WriteLine(FormattableString.Invariant($"[files-ui] t={record.Timestamp} stall-ms={record.Lateness:F0}"));
}
catch (System.IO.IOException) { }
catch (ObjectDisposedException) { }
}
});

var last = Stopwatch.GetTimestamp();
timer = queue.CreateTimer();
timer.Interval = TimeSpan.FromMilliseconds(TickMs);
timer.IsRepeating = true;
timer.Tick += (_, _) =>
{
var now = Stopwatch.GetTimestamp();
var lateness = Stopwatch.GetElapsedTime(last, now).TotalMilliseconds - TickMs;
last = now;
if (lateness > StallThresholdMs)
records.Writer.TryWrite((DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), lateness));
};
timer.Start();
}
}
}
138 changes: 138 additions & 0 deletions src/Files.App/Styles/LinuxDesktopResources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,144 @@
</Grid>
</ControlTemplate>

<!--
Selection CheckBox of file items. Uno's default CheckBox template builds an AnimatedIcon (Lottie visual) per instance,
which was about a third of the time spent realizing item containers in software rendering; this one is a plain box and glyph.
-->
<ControlTemplate x:Key="Files.SelectionCheckBoxTemplate" TargetType="CheckBox">
<Grid x:Name="RootGrid" Background="Transparent">
<Border
x:Name="CheckBoxBorder"
Width="20"
Height="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="{ThemeResource CheckBoxCheckBackgroundFillUnchecked}"
BorderBrush="{ThemeResource CheckBoxCheckBackgroundStrokeUnchecked}"
BorderThickness="1"
CornerRadius="4" />
<Path
x:Name="CheckGlyph"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M0,5 L4,9 L12,0"
IsHitTestVisible="False"
Opacity="0"
Stroke="{ThemeResource CheckBoxCheckGlyphForegroundChecked}"
StrokeEndLineCap="Round"
StrokeLineJoin="Round"
StrokeStartLineCap="Round"
StrokeThickness="1.5" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="UncheckedNormal">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillUnchecked}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeUnchecked}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundUnchecked}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="UncheckedPointerOver">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillUncheckedPointerOver}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeUncheckedPointerOver}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundUncheckedPointerOver}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="UncheckedPressed">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillUncheckedPressed}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeUncheckedPressed}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundUncheckedPressed}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="UncheckedDisabled">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillUncheckedDisabled}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeUncheckedDisabled}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundUncheckedDisabled}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="CheckedNormal">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillChecked}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeChecked}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundChecked}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="CheckedPointerOver">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillCheckedPointerOver}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeCheckedPointerOver}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundCheckedPointerOver}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="CheckedPressed">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillCheckedPressed}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeCheckedPressed}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundCheckedPressed}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="CheckedDisabled">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillCheckedDisabled}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeCheckedDisabled}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundCheckedDisabled}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="IndeterminateNormal">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillIndeterminate}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeIndeterminate}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundIndeterminate}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
<Setter Target="CheckGlyph.Data" Value="M0,0 L12,0" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="IndeterminatePointerOver">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillIndeterminatePointerOver}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeIndeterminatePointerOver}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundIndeterminatePointerOver}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
<Setter Target="CheckGlyph.Data" Value="M0,0 L12,0" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="IndeterminatePressed">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillIndeterminatePressed}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeIndeterminatePressed}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundIndeterminatePressed}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
<Setter Target="CheckGlyph.Data" Value="M0,0 L12,0" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="IndeterminateDisabled">
<VisualState.Setters>
<Setter Target="CheckBoxBorder.Background" Value="{ThemeResource CheckBoxCheckBackgroundFillIndeterminateDisabled}" />
<Setter Target="CheckBoxBorder.BorderBrush" Value="{ThemeResource CheckBoxCheckBackgroundStrokeIndeterminateDisabled}" />
<Setter Target="CheckGlyph.Stroke" Value="{ThemeResource CheckBoxCheckGlyphForegroundIndeterminateDisabled}" />
<Setter Target="CheckGlyph.Opacity" Value="1" />
<Setter Target="CheckGlyph.Data" Value="M0,0 L12,0" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>

<Style x:Key="Files.SelectionCheckBoxStyle" TargetType="CheckBox">
<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="FocusVisualPrimaryBrush" Value="{ThemeResource SystemControlFocusVisualPrimaryBrush}" />
<Setter Property="FocusVisualSecondaryBrush" Value="{ThemeResource SystemControlFocusVisualSecondaryBrush}" />
<Setter Property="Template" Value="{StaticResource Files.SelectionCheckBoxTemplate}" />
</Style>

<ResourceDictionary.ThemeDictionaries>
<!-- No Mica on Linux: the window backdrop is a solid fill equal to the Mica base colour, so the translucent Files layers on top keep their look -->
<ResourceDictionary x:Key="Light">
Expand Down
5 changes: 5 additions & 0 deletions src/Files.App/Views/Layouts/BaseLayoutPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ namespace Files.App.Views.Layouts
/// </summary>
public abstract partial class BaseLayoutPage : Page, IBaseLayoutPage, INotifyPropertyChanged
{
public static Style? SelectionCheckBoxStyle =>
OperatingSystem.IsLinux()
? (Style)Application.Current.Resources["Files.SelectionCheckBoxStyle"]
: null;

// Dependency injections

protected IFileTagsSettingsService FileTagsSettingsService { get; } = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Views/Layouts/DetailsLayoutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@
<!-- Selection Check Box -->
<CheckBox
x:Name="SelectionCheckbox"
Style="{x:Bind local:BaseLayoutPage.SelectionCheckBoxStyle}"
Width="20"
MinWidth="0"
HorizontalAlignment="Center"
Expand Down
Loading
Loading