Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

perf(desktop): reduce repeated search row rendering (#219)

Dee12df7c6e7e67
diff --git a/desktop/lib/state/swarm_search.dart b/desktop/lib/state/swarm_search.dart
index 2bdf01114..41155ee3c 100644
--- a/desktop/lib/state/swarm_search.dart
+++ b/desktop/lib/state/swarm_search.dart
@@ -334,8 +334,8 @@ class SwarmSearchController extends ChangeNotifier {
? row != null &&
row.agentId == null &&
split == null &&
- _missingIds(row).length > 1
- ? 'Open ${_missingIds(row).length} Harnesses'
+ _missingCount(row) > 1
+ ? 'Open ${_missingCount(row)} Harnesses'
: primaryAction
: row == null
? 'Go to'
@@ -350,7 +350,7 @@ class SwarmSearchController extends ChangeNotifier {
: selected != null &&
selected!.agentId == null &&
selected!.members.isNotEmpty &&
- _missingIds(selected!).isEmpty
+ !_hasMissing(selected!)
? 'Everything in it is already open here.'
: 'No room for another harness.';
@@ -652,10 +652,9 @@ class SwarmSearchController extends ChangeNotifier {
canOpenSwarmGroup(app, row, destinationSwarmId: targetId)) &&
(row.closedId == null || app.canReopenClosed(row.closedId!));
- Set<String> _missingIds(SwarmDestination row) {
- final members = row.agentId == null ? row.members : {row.id};
- return members.difference(_presentIds);
- }
+ int _missingCount(SwarmDestination row) => row.agentId != null
+ ? (_presentIds.contains(row.id) ? 0 : 1)
+ : row.members.where((id) => !_presentIds.contains(id)).length;
bool _hasMissing(SwarmDestination row) => row.agentId != null
? !_presentIds.contains(row.id)
@@ -672,7 +671,7 @@ class SwarmSearchController extends ChangeNotifier {
row.closedId == null &&
(placement == null || row.agentId != null) &&
(placement != null && alreadyHere(row) ||
- _missingIds(row).isNotEmpty &&
+ _hasMissing(row) &&
(split == null || row.agentId != null) &&
(split == null || app.isPaneSplitCurrent(split!)) &&
(placement == HarnessPlacement.newTab ||
@@ -681,7 +680,7 @@ class SwarmSearchController extends ChangeNotifier {
swarm.id == targetId &&
!swarm.isStore &&
!swarm.isOrchestrator &&
- swarm.panes.length + _missingIds(row).length <=
+ swarm.panes.length + _missingCount(row) <=
AppNotifier.maxPanes,
)));
diff --git a/desktop/lib/widgets/box_chrome.dart b/desktop/lib/widgets/box_chrome.dart
index b25817674..ab43ff751 100644
--- a/desktop/lib/widgets/box_chrome.dart
+++ b/desktop/lib/widgets/box_chrome.dart
@@ -188,6 +188,7 @@ class BoxRowHighlight extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
+ animationDuration: Duration.zero,
color: highlighted
? Colors.white.withValues(alpha: .10)
: Colors.transparent,
diff --git a/desktop/lib/widgets/prompt_context.dart b/desktop/lib/widgets/prompt_context.dart
index 3fa3b245d..7deca1351 100644
--- a/desktop/lib/widgets/prompt_context.dart
+++ b/desktop/lib/widgets/prompt_context.dart
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
+import 'package:flutter/foundation.dart' show listEquals;
+import 'package:flutter/rendering.dart' show OverflowBoxFit;
import 'package:lucide_icons_flutter/lucide_icons.dart';
import '../shared/theme/app_theme.dart' as grid;
@@ -10,7 +12,7 @@ import 'search_result_text.dart';
/// The same compact identity line in the picker, pane header and live preview.
/// Symbols use our bundled icon font; Powerline separators are drawn shapes.
/// Changing the terminal font therefore never requires a patched Nerd Font.
-class PromptContextView extends StatelessWidget {
+class PromptContextView extends StatefulWidget {
const PromptContextView({
super.key,
required this.contextData,
@@ -25,6 +27,44 @@ class PromptContextView extends StatelessWidget {
double get size => grid.AppType.monoLabelSize;
final Iterable<SearchFieldMatch> matches;
+ @override
+ State<PromptContextView> createState() => _PromptContextViewState();
+}
+
+class _PromptContextViewState extends State<PromptContextView> {
+ PromptContext get contextData => widget.contextData;
+ PromptPrefs? get prefs => widget.prefs;
+ AppearancePrefsStore? get store => widget.store;
+ Iterable<SearchFieldMatch> get matches => widget.matches;
+ double get size => widget.size;
+
+ Object? _lineKey;
+ List<SearchFieldMatch> _lineMatches = const [];
+ Widget? _lineWidget;
+
+ Widget _cachedLine(PromptPrefs prefs, double width, TextScaler scaler) {
+ final key = (
+ contextData,
+ prefs,
+ width,
+ scaler,
+ _style(Colors.white),
+ size,
+ grid.AppPalette.swarmAccent,
+ grid.AppPalette.teal,
+ grid.AppPalette.online,
+ );
+ final nextMatches = matches.toList(growable: false);
+ if (_lineKey == key && listEquals(_lineMatches, nextMatches)) {
+ return _lineWidget!;
+ }
+ _lineKey = key;
+ _lineMatches = nextMatches;
+ // Most searches match the title. Keep the breadcrumb's render tree intact
+ // while typing or moving its highlight, until its own content changes.
+ return _lineWidget = _line(prefs, width, scaler);
+ }
+
@override
Widget build(BuildContext context) {
grid.AppTheme.watch(context);
@@ -34,7 +74,7 @@ class PromptContextView extends StatelessWidget {
terminalFontStore,
]),
builder: (context, _) => LayoutBuilder(
- builder: (context, constraints) => _line(
+ builder: (context, constraints) => _cachedLine(
prefs ?? (store ?? appearancePrefsStore).value.prompt,
constraints.maxWidth,
MediaQuery.textScalerOf(context),
@@ -245,9 +285,9 @@ class PromptContextView extends StatelessWidget {
return [for (final width in widths) (width * scale).floorToDouble()];
}
- static final _widths = <(String, double, double), double>{};
+ static final _widths = <(String, TextStyle, TextScaler), double>{};
static double _measure(String text, TextStyle style, TextScaler scaler) {
- final key = (text, style.fontSize ?? 0, scaler.scale(100));
+ final key = (text, style, scaler);
final known = _widths[key];
if (known != null) return known;
if (_widths.length > 512) _widths.clear();
@@ -298,9 +338,11 @@ class _Clipped extends StatelessWidget {
@override
Widget build(BuildContext context) => clip
? ClipRect(
- child: SingleChildScrollView(
- scrollDirection: Axis.horizontal,
- physics: const NeverScrollableScrollPhysics(),
+ child: OverflowBox(
+ fit: OverflowBoxFit.deferToChild,
+ alignment: Alignment.centerLeft,
+ minWidth: 0,
+ maxWidth: double.infinity,
child: child,
),
)
diff --git a/desktop/test/prompt_context_test.dart b/desktop/test/prompt_context_test.dart
index 37cbd05bf..a8941bc6f 100644
--- a/desktop/test/prompt_context_test.dart
+++ b/desktop/test/prompt_context_test.dart
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/shared/theme/prompt_style.dart';
import 'package:harness/widgets/prompt_context.dart';
+import 'package:harness/widgets/search_result_text.dart';
void main() {
Future<void> line(
@@ -73,4 +74,40 @@ void main() {
expect(tester.takeException(), isNull);
}
});
+
+ testWidgets('cached context refreshes identity, matching and text scale', (
+ tester,
+ ) async {
+ const original = PromptContext(machine: 'studio', branch: 'main');
+ Future<void> show({
+ PromptContext data = original,
+ List<SearchFieldMatch> matches = const [],
+ double scale = 1,
+ }) => tester.pumpWidget(
+ MaterialApp(
+ home: MediaQuery(
+ data: MediaQueryData(textScaler: TextScaler.linear(scale)),
+ child: Scaffold(
+ body: SizedBox(
+ width: 500,
+ child: PromptContextView(contextData: data, matches: matches),
+ ),
+ ),
+ ),
+ ),
+ );
+ await show();
+ expect(find.text('main'), findsOneWidget);
+ await show(matches: [(field: 'main', term: 'mai', title: false)]);
+ final highlighted = tester.widget<Text>(find.text('main'));
+ expect(highlighted.textSpan, isNotNull);
+ await show(
+ data: const PromptContext(machine: 'laptop', branch: 'dev'),
+ scale: 2,
+ );
+ expect(find.text('main'), findsNothing);
+ expect(find.text('dev'), findsOneWidget);
+ expect(find.text('laptop'), findsOneWidget);
+ expect(tester.takeException(), isNull);
+ });
}
diff --git a/docs/performance/screenshots/search-context.png b/docs/performance/screenshots/search-context.png
new file mode 100644
index 000000000..9c918140d
Binary files /dev/null and b/docs/performance/screenshots/search-context.png differ