Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release build much slower than debug build when running on isolates with Command.executeThread() on Android #660

Open
eduribas opened this issue Jul 1, 2024 · 5 comments

Comments

@eduribas
Copy link

eduribas commented Jul 1, 2024

The following simple code runs much slower in release build than in debug build on Android:

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image/image.dart' as im;

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  Duration? _elapsedTime;
  bool _running = false;

  void _processPicture() async {
    final file = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (file != null) {
      setState(() {
        _running = true;
      });
      final bytes = await file.readAsBytes();
      final command = im.Command()
        ..decodeImage(bytes)
        ..encodeJpg();
      final startTime = DateTime.now();
      await command.executeThread();
      setState(() {
        _running = false;
        _elapsedTime = DateTime.now().difference(startTime);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              if (_running)
                const CircularProgressIndicator()
              else
                Text(_elapsedTime != null
                    ? 'Elapsed milliseconds: ${_elapsedTime!.inMilliseconds}'
                    : ''),
              OutlinedButton(
                onPressed: _processPicture,
                child: const Text('Select picture'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
@eduribas eduribas changed the title Release build much slower then debug build when running on isolates with Command.executeThread() on Android Release build much slower than debug build when running on isolates with Command.executeThread() on Android Jul 1, 2024
@eduribas
Copy link
Author

eduribas commented Jul 3, 2024

By further testing I noticed that even if not running in isolates, by changing await command.executeThread(); to await command.execute(); in the code above, it still runs around 25% slower in release builds than in debug builds, in a Pixel 7 Pro.

@brendan-duncan
Copy link
Owner

This kinda feels like a Dart issue, I'm not sure what I'd be able to do about it. It must be hitting some performance penalty case for Dart release optimizations. I agree it's a terrible performance loss, and completely backwards to what you would expect.

@eduribas
Copy link
Author

eduribas commented Jul 4, 2024

This kinda feels like a Dart issue, I'm not sure what I'd be able to do about it. It must be hitting some performance penalty case for Dart release optimizations. I agree it's a terrible performance loss, and completely backwards to what you would expect.

Do you think it is related to this flutter issue: flutter/flutter#140351 ?

As discussed in that issue, the usage of num in computations is really problematic for the AOT compiler used for release builds.

@brendan-duncan
Copy link
Owner

I can take a look at num usage when I get a chance.

@brendan-duncan
Copy link
Owner

I suspect there is is something similar going on, but I don't know if it's num causing the performance hit with the AOT release build. num isn't used in the decoders and encoders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants