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:
@@ -0,0 +1,18 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/artifacts/ui/artifact_thumbnail_image.dart';
|
||||
|
||||
void main() {
|
||||
test('decodes a large picture just big enough to cover its box', () {
|
||||
// Landscape into a square: the height binds, the width overflows.
|
||||
final wide = coverTargetSize(4000, 2000, width: 200, height: 200);
|
||||
expect((wide.width, wide.height), (400, 200));
|
||||
// Portrait into a 4:3 tile: the width binds.
|
||||
final tall = coverTargetSize(1000, 3000, width: 400, height: 300);
|
||||
expect((tall.width, tall.height), (400, 1200));
|
||||
});
|
||||
|
||||
test('never upscales a picture smaller than its box', () {
|
||||
final small = coverTargetSize(100, 80, width: 200, height: 200);
|
||||
expect((small.width, small.height), (null, null));
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/layout/memo_subtree.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('rebuilds only when the memo changes', (tester) async {
|
||||
var builds = 0;
|
||||
Widget host(Object memo) => Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: MemoSubtree(
|
||||
memo: memo,
|
||||
builder: (_) {
|
||||
builds++;
|
||||
return Text('$memo');
|
||||
},
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'x'}), true)));
|
||||
// An equal map (a new instance) and equal props keep the built child.
|
||||
await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'x'}), true)));
|
||||
expect(builds, 1);
|
||||
await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'y'}), true)));
|
||||
expect(builds, 2);
|
||||
await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'y'}), false)));
|
||||
expect(builds, 3);
|
||||
});
|
||||
|
||||
testWidgets('the built subtree still follows its inherited widgets', (
|
||||
tester,
|
||||
) async {
|
||||
Widget host(Color color) => MaterialApp(
|
||||
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: color)),
|
||||
home: MemoSubtree(
|
||||
memo: 1,
|
||||
builder: (_) => Builder(
|
||||
builder: (context) => ColoredBox(
|
||||
key: const Key('box'),
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
Color shown() =>
|
||||
tester.widget<ColoredBox>(find.byKey(const Key('box'))).color;
|
||||
await tester.pumpWidget(host(Colors.red));
|
||||
final before = shown();
|
||||
await tester.pumpWidget(host(Colors.blue));
|
||||
await tester.pumpAndSettle();
|
||||
expect(shown(), isNot(before));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/layout/sliver_reveal.dart';
|
||||
|
||||
void main() {
|
||||
Widget host({
|
||||
required bool open,
|
||||
Duration duration = kThemeAnimationDuration,
|
||||
}) => MaterialApp(
|
||||
home: CustomScrollView(
|
||||
slivers: [
|
||||
SliverReveal(
|
||||
open: open,
|
||||
duration: duration,
|
||||
curve: Curves.easeInOut,
|
||||
sliver: SliverList.builder(
|
||||
itemCount: 1000,
|
||||
itemBuilder: (_, index) =>
|
||||
SizedBox(height: 50, child: Text('item $index')),
|
||||
),
|
||||
),
|
||||
const SliverToBoxAdapter(child: Text('after')),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
double afterTop(WidgetTester tester) =>
|
||||
tester.getTopLeft(find.text('after')).dy;
|
||||
|
||||
testWidgets('closed builds nothing; open builds only the visible items', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(host(open: false));
|
||||
expect(find.textContaining('item'), findsNothing);
|
||||
expect(afterTop(tester), 0);
|
||||
|
||||
await tester.pumpWidget(host(open: true));
|
||||
await tester.pump(kThemeAnimationDuration ~/ 2);
|
||||
// Part-way open: the items reveal from the top, clipped at the extent.
|
||||
expect(find.text('item 0'), findsOneWidget);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('item 0'), findsOneWidget);
|
||||
expect(find.text('item 999', skipOffstage: false), findsNothing);
|
||||
expect(tester.widgetList(find.textContaining('item')).length, lessThan(40));
|
||||
});
|
||||
|
||||
testWidgets('closing shrinks the extent, then drops the items', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: CustomScrollView(
|
||||
slivers: [
|
||||
SliverReveal(
|
||||
open: true,
|
||||
duration: kThemeAnimationDuration,
|
||||
curve: Curves.linear,
|
||||
sliver: SliverList.list(
|
||||
children: [
|
||||
for (var i = 0; i < 4; i++)
|
||||
SizedBox(height: 50, child: Text('item $i')),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SliverToBoxAdapter(child: Text('after')),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(afterTop(tester), 200);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: CustomScrollView(
|
||||
slivers: [
|
||||
SliverReveal(
|
||||
open: false,
|
||||
duration: kThemeAnimationDuration,
|
||||
curve: Curves.linear,
|
||||
sliver: SliverList.list(
|
||||
children: [
|
||||
for (var i = 0; i < 4; i++)
|
||||
SizedBox(height: 50, child: Text('item $i')),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SliverToBoxAdapter(child: Text('after')),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pump(kThemeAnimationDuration ~/ 2);
|
||||
expect(afterTop(tester), closeTo(100, 1));
|
||||
await tester.pumpAndSettle();
|
||||
expect(afterTop(tester), 0);
|
||||
expect(find.textContaining('item'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('no motion opens and closes at once', (tester) async {
|
||||
await tester.pumpWidget(host(open: false, duration: Duration.zero));
|
||||
await tester.pumpWidget(host(open: true, duration: Duration.zero));
|
||||
await tester.pump();
|
||||
expect(find.text('item 0'), findsOneWidget);
|
||||
await tester.pumpWidget(host(open: false, duration: Duration.zero));
|
||||
await tester.pump();
|
||||
expect(find.textContaining('item'), findsNothing);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user