flutterdata / flutter_data

Seamlessly manage persistent data in your Flutter apps
MIT License
410 stars 31 forks source link

High CPU with WatchNotifiers after migrating to Flutter 3.13.0 #254

Open mwcbrent opened 10 months ago

mwcbrent commented 10 months ago

After updating to a newer version of Flutter (3.13.0) our application started to run hot. After profiling in Instruments we identified a process in FD that was unusually high for an idle state. Our app was using 94% of all CPU resources, entering "Fair" thermal state in a minute and "Serious" in about four minutes.

Profiling in the DevTools profiler showed a FD-related timer using a large amount of CPU. Digging into the code it seemed to be the timer used in notifier_extension.dart part of the watcher functionality.

Overriding the timer duration for the graphNotifierThrottle to some amount like 10ms returned the app to its normal CPU and temperature.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await _setupErrorReporting(
    () => runApp(
      ProviderScope(
        child: const MyApp(),
        overrides: [
          // Added this line
          graphNotifierThrottleDurationProvider.overrideWithValue(const Duration(milliseconds: 10)),

          configureRepositoryLocalStorage(
              clear: LocalStorageClearStrategy.whenError),
        ],
      ),
    ),
  );

This was not a problem before the update to 3.13.0. The above code fixes the issue for us.

We make heavy use of the watch functionality in FD throughout our app to always provide updated data. I'm curious if there are other ways to read state and listen for changes with a Stream-like interface directly from FD in some way? I'd propose adding this to the docs unless this is a symptom of a problem that needs to be fixed.

frank06 commented 9 months ago

Appreciate the report! And glad to hear that tweaking graphNotifierThrottle fixes it.

I'm working on a new FD based on Objectbox which has a lot of changes, so I'll make sure I check for this one before releasing