Files
pipelines/app/lib/ui/layout/memo_subtree.dart
T
alan 7b1455631b 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.
2026-11-02 00:08:29 +00:00

66 lines
1.9 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
/// Builds [builder] once and hands Flutter the same child until [memo]
/// changes, so a list that rebuilds on every notification skips the rows
/// whose own data did not change.
///
/// [memo] must capture everything the built subtree shows or calls: the
/// row's data and every callback or controller its closures use (compare
/// callbacks by `==`, so a tear-off of the same method still matches). The
/// subtree's own inherited dependencies (theme, host, media) still rebuild
/// it directly, as they mark its elements rather than this one.
class MemoSubtree extends StatefulWidget {
const MemoSubtree({super.key, required this.memo, required this.builder});
final Object? memo;
final WidgetBuilder builder;
@override
State<MemoSubtree> createState() => _MemoSubtreeState();
}
class _MemoSubtreeState extends State<MemoSubtree> {
Widget? _child;
@override
void didUpdateWidget(MemoSubtree oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.memo != widget.memo) _child = null;
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// A builder that read an inherited widget through this context.
_child = null;
}
@override
void reassemble() {
super.reassemble();
_child = null;
}
@override
Widget build(BuildContext context) => _child ??= widget.builder(context);
}
/// A JSON row map as a [MemoSubtree] memo: equal when it is the same map or
/// has the same entries (nested values compare by `==`, so a rebuilt nested
/// map counts as a change).
@immutable
class MemoMap {
const MemoMap(this.map);
final Map<String, dynamic>? map;
@override
bool operator ==(Object other) =>
other is MemoMap &&
(identical(map, other.map) || mapEquals(map, other.map));
@override
int get hashCode => map?.length ?? 0;
}