Rebuild only changed list rows and lay out big artifact sections lazily

Runs and chat session rows are memoized on their own data (MemoSubtree), so
a run event or a chat's running change rebuilds one row instead of every
built row. The artifact gallery's open sections are lazy slivers revealed
by a sliver open/close animation, and thumbnails decode at display size.
Benches in test/perf measure both.
This commit is contained in:
2026-11-02 00:08:29 +00:00
parent b3b761c56d
commit 7b1455631b
16 changed files with 889 additions and 152 deletions
@@ -80,6 +80,9 @@ List<JsonMap> _runs(int count, DateTime now) => [
},
];
// The app passes a stable tear-off (`_select`), never a fresh closure.
void _open(num _) {}
void main() {
late _Gateway gateway;
late RunsController controller;
@@ -105,7 +108,7 @@ void main() {
AppDestination.runs: (_) => AnimatedBuilder(
animation: controller,
builder: (_, _) =>
RunsList(controller: controller, onSelected: (_) {}),
RunsList(controller: controller, onSelected: _open),
),
AppDestination.pipelines: (_) => const Center(child: Text('pipelines')),
},
@@ -234,6 +237,9 @@ void main() {
);
// Only the ten frames that advance a step change anything visible.
expect(notifications, lessThanOrEqualTo(10));
// Each notification rebuilds the one row whose run changed, not every
// built row (16 rows rebuilt per notification before memoized rows).
expect(rebuilds.of(RunListRow), lessThanOrEqualTo(notifications));
await tester.pumpWidget(const SizedBox());
});
}
@@ -0,0 +1,170 @@
// Measures the work a list does for data that did not change: the chat
// sessions list on a running-state change, and the artifact gallery opening a
// visit with hundreds of artifacts. Each scenario prints `BENCH ...` numbers
// and asserts the budget the memoized rows and lazy gallery keep.
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_tile.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_body.dart';
import 'package:pipelines_app/features/chat/ui/sessions/session_row_content.dart';
class _Rebuilds {
final Map<Type, int> counts = {};
void install() => debugOnRebuildDirtyWidget = (element, _) => counts.update(
element.widget.runtimeType,
(n) => n + 1,
ifAbsent: () => 1,
);
void uninstall() => debugOnRebuildDirtyWidget = null;
int of(Type type) => counts[type] ?? 0;
int get total => counts.values.fold(0, (a, b) => a + b);
}
int _elements(WidgetTester tester) {
var count = 0;
void visit(Element element) {
count++;
element.visitChildren(visit);
}
tester.binding.rootElement!.visitChildren(visit);
return count;
}
// Stable callbacks, as the screen passes its own methods.
void _session(JsonMap _) {}
void _clear() {}
bool _unseen(JsonMap _) => false;
String? _noError(String _) => null;
Future<ArtifactFetchResult> _fetch(Uri _) async =>
ArtifactFetchResult(status: 404, bytes: Uint8List(0));
void main() {
Future<void> sizeView(WidgetTester tester) async {
tester.view.physicalSize = const Size(400, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
}
testWidgets('chat sessions list: 50 running-state changes', (tester) async {
await sizeView(tester);
final sessions = ValueNotifier<List<JsonMap>>([
for (var i = 0; i < 200; i++)
{
'id': 's$i',
'title': 'Chat $i',
'directory': '/repo/${i % 3}',
'harness': 'claude',
'running': false,
'updated_at': DateTime.now()
.subtract(Duration(minutes: i))
.toIso8601String(),
},
]);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ValueListenableBuilder(
valueListenable: sessions,
builder: (_, list, _) => ChatSessionsBody(
sessions: list,
query: '',
loading: false,
error: null,
onOpen: _session,
onRename: _session,
onClose: _session,
onReopen: _session,
onStar: _session,
onHide: _session,
onClearSearch: _clear,
isUnseen: _unseen,
errorFor: _noError,
),
),
),
),
);
await tester.pump();
final built = find.byType(ChatSessionRow).evaluate().length;
final rebuilds = _Rebuilds()..install();
for (var i = 0; i < 50; i++) {
// The controller replaces only the changed row's map.
final id = i % 5;
sessions.value = [
for (final s in sessions.value)
s['id'] == 's$id' ? {...s, 'running': s['running'] != true} : s,
];
await tester.pump();
}
rebuilds.uninstall();
// ignore: avoid_print
print(
'BENCH chatSessions200 builtRows=$built '
'rowRebuilds=${rebuilds.of(ChatSessionRow)} '
'totalRebuilds=${rebuilds.total}',
);
// One row per change, not all 200 (10,000 row rebuilds before).
expect(rebuilds.of(ChatSessionRow), 50);
});
testWidgets('gallery: open a visit with 500 artifacts', (tester) async {
await sizeView(tester);
final group = ArtifactGroup(
stageKey: 'render',
visit: 1,
artifacts: [
for (var i = 0; i < 500; i++)
ArtifactRecord.fromJson({
'name': 'frame-$i.png',
'url': '/artifacts/1/frame-$i.png',
'content_type': 'image/png',
'size_bytes': 1000 + i,
}),
],
);
for (final mode in ArtifactGalleryMode.values) {
ArtifactVisitList.lastMode = mode;
final watch = Stopwatch()..start();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ArtifactVisitList(
key: ValueKey(mode),
groups: [group],
initiallyCollapsed: false,
fetch: _fetch,
),
),
),
);
await tester.pumpAndSettle();
final settle = watch.elapsedMilliseconds;
final tiles =
find.byType(ArtifactTile).evaluate().length +
find.byType(ArtifactThumbnail).evaluate().length;
final elements = _elements(tester);
// A scroll frame through the open section.
watch.reset();
await tester.drag(find.byType(CustomScrollView), const Offset(0, -2000));
await tester.pump();
final scroll = watch.elapsedMicroseconds;
// ignore: avoid_print
print(
'BENCH gallery500 mode=${mode.name} builtItems=$tiles '
'elements=$elements openMs=$settle scrollFrameUs=$scroll',
);
// Only the visible tiles are built (all 500 were before).
expect(tiles, lessThan(100));
await tester.pumpWidget(const SizedBox());
}
ArtifactVisitList.lastMode = ArtifactGalleryMode.grid;
});
}