perf(mobile): pulse the Solo working dots at 12 fps on a shared clock (#9554)

Each working row on Home (the three pulsing dots next to a Solo chat
whose agent is working) ran its own 60 fps animation. So an idle Home
with any working agent produced a frame on every vsync. Painting the
dots is cheap. The frame itself is not: each one costs the app,
backboardd and the kernel together, and on the iPhone SE that adds 8.6%
of battery per hour over static dots.

The rows now step from one shared clock: 17 steps per 1400 ms cycle
(about 12 fps), with the same cosine wave. All rows read the same step,
so they stay in phase and repaint in the same frame. The clock runs only
while a row is visible with `TickerMode` on and the app is in the
foreground, so a Home with no working rows, a covered or off-screen
Home, or a backgrounded app has no timer.

### Validation

Idle Home with working agents, 30 s Time Profiler, % of one core, iPhone
12 mini (release builds of main and this branch; real working agents, 3
rows in the before run and 2 in the after run, which doesn't matter
because the cost is per frame, not per row):

| | Buzz | backboardd | kernel | Total |
|---|---|---|---|---|
| Before (60 fps) | 43.3 | 42.0 | 18.9 | 104.2 |
| After (12 fps) | 14.9 | 15.7 | 14.8 | 45.4 |

Earlier battery runs on the iPhone SE (2nd gen), whole-device gauge, 8
min per variant, same setup: 60 fps +600 mW (+8.6%/h) over static dots;
12 fps +109 mW (+1.6%/h).

Two working rows on a simulator:

Before (60 fps):

<video
src="https://github.com/user-attachments/assets/539ce17d-7df4-4508-a981-4d6b31f8f805"
width="400"></video>

After (12 fps):

<video
src="https://github.com/user-attachments/assets/18513348-6f24-48c5-bb3b-4ac328d0c983"
width="400"></video>

- Added tests: `solo_working_dots_test.dart` checks that rows step
together at about 12 fps with no frames scheduled between steps, and
that the shared timer is gone when the last row is removed, when
`TickerMode` is off, and while the app is hidden.
- Full mobile suite passes (4912 tests). The one failure is
`search_channels_journey_test.dart`, which doesn't compile on main;
[#9545](https://github.com/block/buzz/pull/9545) fixes it.
- Automated app run (signed-in simulator, every Solo row forced to
working with a local-only patch): the rows pulse in phase on Home, keep
pulsing after opening a chat and going back, and after backgrounding and
resuming the app.

### Regressions

- The pulse steps at 12 fps instead of animating smoothly. Side by side
in the recordings it reads the same; you notice the steps only if you
look for them.
- Rows are now in phase with each other instead of each starting its
wave when it mounted.
- Reduced motion still shows static dots at full opacity, as before.

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
This commit is contained in:
Tom Brow
2026-11-15 17:05:02 -08:00
committed by GitHub
parent c75d812f80
commit 75d78eab68
2 changed files with 183 additions and 40 deletions

View File

@@ -1,9 +1,15 @@
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
/// Desktop's three quiet opacity pulses. The containing row owns semantics.
///
/// Every visible row steps from one shared clock at about 12 fps instead of
/// animating at display rate. On Home each frame costs the app, backboardd
/// and the kernel far more than painting the dots does, so the frame rate is
/// the cost; sharing the clock also keeps rows in phase in the same frames.
class SoloWorkingDots extends HookWidget {
const SoloWorkingDots({super.key, required this.color});
@@ -12,55 +18,102 @@ class SoloWorkingDots extends HookWidget {
@override
Widget build(BuildContext context) {
final reducedMotion = MediaQuery.disableAnimationsOf(context);
final animation = useAnimationController(
duration: const Duration(milliseconds: 1400),
);
final animate = TickerMode.valuesOf(context).enabled && !reducedMotion;
// A per-row revision, not the shared step: a row that rejoins while
// another row kept the clock running must still rebuild on its next tick.
final revision = useState(0);
useEffect(() {
if (reducedMotion) {
animation.stop();
} else {
animation.repeat();
}
return animation.stop;
}, [animation, reducedMotion]);
if (!animate) return null;
void onStep() => revision.value++;
_PulseClock.instance.add(onStep);
return () => _PulseClock.instance.remove(onStep);
}, [animate]);
final phase = _PulseClock.instance.step / _PulseClock.stepsPerCycle;
return ExcludeSemantics(
child: RepaintBoundary(
child: AnimatedBuilder(
animation: animation,
builder: (context, _) => SizedBox(
height: 20,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
for (var index = 0; index < 3; index++) ...[
if (index > 0) const SizedBox(width: 2),
Opacity(
opacity: reducedMotion
? 1
: 0.35 +
0.65 *
(1 -
math.cos(
2 *
math.pi *
(animation.value - index / 7),
)) /
2,
child: DecoratedBox(
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
),
child: const SizedBox.square(dimension: 4),
child: SizedBox(
height: 20,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
for (var index = 0; index < 3; index++) ...[
if (index > 0) const SizedBox(width: 2),
Opacity(
opacity: reducedMotion
? 1
: 0.35 +
0.65 *
(1 -
math.cos(
2 * math.pi * (phase - index / 7),
)) /
2,
child: DecoratedBox(
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
),
child: const SizedBox.square(dimension: 4),
),
],
),
],
),
],
),
),
),
);
}
}
/// Steps the 1400 ms pulse in 17 steps (about 12 fps) while any dots are
/// visible and the app is in the foreground.
class _PulseClock {
static final instance = _PulseClock();
static const stepsPerCycle = 17;
static const _period = Duration(microseconds: 1400000 ~/ stepsPerCycle);
final _listeners = <VoidCallback>{};
int step = 0;
Timer? _timer;
AppLifecycleListener? _lifecycle;
void add(VoidCallback listener) {
_listeners.add(listener);
_sync();
}
void remove(VoidCallback listener) {
_listeners.remove(listener);
_sync();
}
void _sync() {
if (_listeners.isEmpty) {
_lifecycle?.dispose();
_lifecycle = null;
} else {
_lifecycle ??= AppLifecycleListener(onStateChange: (_) => _sync());
}
final state = WidgetsBinding.instance.lifecycleState;
final foreground =
state == null ||
state == AppLifecycleState.resumed ||
state == AppLifecycleState.inactive;
if (_listeners.isNotEmpty && foreground) {
_timer ??= Timer.periodic(_period, (_) {
step = (step + 1) % stepsPerCycle;
for (final listener in List.of(_listeners)) {
listener();
}
});
} else {
_timer?.cancel();
_timer = null;
}
}
}
/// Whether the shared pulse timer exists, for tests.
@visibleForTesting
bool get debugSoloWorkingDotsClockRunning =>
_PulseClock.instance._timer != null;

View File

@@ -0,0 +1,90 @@
import 'package:buzz/features/channels/me/solo_working_dots.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
List<double> opacities(WidgetTester tester, [int row = 0]) => tester
.widgetList<Opacity>(
find.descendant(
of: find.byType(SoloWorkingDots).at(row),
matching: find.byType(Opacity),
),
)
.map((dot) => dot.opacity)
.toList();
Widget dots({bool ticking = true, int rows = 1}) => Directionality(
textDirection: TextDirection.ltr,
child: TickerMode(
enabled: ticking,
child: Column(
children: [
for (var i = 0; i < rows; i++)
const SoloWorkingDots(color: Color(0xFF000000)),
],
),
),
);
testWidgets('rows step together at about 12 fps with no frames between', (
tester,
) async {
await tester.pumpWidget(dots(rows: 2));
expect(debugSoloWorkingDotsClockRunning, isTrue);
final start = opacities(tester);
expect(tester.binding.hasScheduledFrame, isFalse);
// Check before pumping: pump draws any frame scheduled in the interval.
await tester.binding.delayed(const Duration(milliseconds: 80));
expect(tester.binding.hasScheduledFrame, isFalse);
await tester.pump();
expect(opacities(tester), start);
await tester.binding.delayed(const Duration(milliseconds: 3));
expect(tester.binding.hasScheduledFrame, isTrue);
await tester.pump();
final next = opacities(tester);
expect(next, isNot(start));
expect(opacities(tester, 1), next);
expect(tester.binding.hasScheduledFrame, isFalse);
await tester.pumpWidget(const SizedBox());
expect(debugSoloWorkingDotsClockRunning, isFalse);
});
testWidgets('the clock stops while TickerMode is off and resumes after', (
tester,
) async {
await tester.pumpWidget(dots(ticking: false));
expect(debugSoloWorkingDotsClockRunning, isFalse);
final start = opacities(tester);
await tester.pump(const Duration(seconds: 1));
expect(opacities(tester), start);
expect(tester.binding.hasScheduledFrame, isFalse);
await tester.pumpWidget(dots());
expect(debugSoloWorkingDotsClockRunning, isTrue);
await tester.pump(const Duration(milliseconds: 83));
expect(opacities(tester), isNot(start));
await tester.pumpWidget(dots(ticking: false));
expect(debugSoloWorkingDotsClockRunning, isFalse);
});
testWidgets('the clock stops while the app is in the background', (
tester,
) async {
await tester.pumpWidget(dots());
void go(AppLifecycleState state) =>
tester.binding.handleAppLifecycleStateChanged(state);
go(AppLifecycleState.inactive);
expect(debugSoloWorkingDotsClockRunning, isTrue);
go(AppLifecycleState.hidden);
expect(debugSoloWorkingDotsClockRunning, isFalse);
go(AppLifecycleState.inactive);
go(AppLifecycleState.resumed);
expect(debugSoloWorkingDotsClockRunning, isTrue);
await tester.pumpWidget(const SizedBox());
expect(debugSoloWorkingDotsClockRunning, isFalse);
});
}