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

WIP: Optimize CLI startup time #2512

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bartekpacia
Copy link
Contributor

@bartekpacia bartekpacia commented Jan 29, 2025

a stab at fixing #1966

Problem description

The version check is performed synchronously.

This makes shell completions needlessly slow (in case of slow/unreliable network) and kinda defeats the purpose of using them. My bad.

if (_wantsUpdateCheck(commandName)) {
await _checkForUpdate(commandName);
}

Resources

The pub_updater package doesn't provide any way to cancel an update check that is in progress. So it seems to me that the only way to do cancel the version check is to run it in a separate isolate. This seems a bit complex as isolates cannot share memory, but pub_updater.PubUpdater and patrol_cli.Logger must be present in the isolate.

Example 1 (using async, not working as intended)
import 'dart:async';

Future<void> main(List<String> args) async {
  unawaited(_backgroundTask());

  // Simulate main work
  print('start: main task');
  await Future<void>.delayed(const Duration(seconds: 1));
  print('end: main task');
}

/// A low-priority background task.
///  * May take longer than the main task
///  * If it lasts longer than main task, it should be preempted (to not block)
///  * It's low priority so it's fine if it's preempted
Future<void> _backgroundTask() async {
  print('start: background task');
  // Simulate some work
  await Future<void>.delayed(const Duration(seconds: 3));
  print('end: background task');
}
Example 2 (using isolates, working as intended)
import 'dart:async';
import 'dart:isolate' as isolate;

Future<void> main(List<String> args) async {
  unawaited(
    isolate.Isolate.spawn<void>((_) => _backgroundTask(), null),
  );

  // Simulate main work
  print('start: main task');
  await Future<void>.delayed(const Duration(seconds: 1));
  print('end: main task');
}

/// A low-priority background task.
///  * May take longer than the main task
///  * If it lasts longer than main task, it should be preempted (to not block)
///  * It's low priority so it's fine if it's preempted
Future<void> _backgroundTask() async {
  print('start: background task');
  // Simulate some work
  await Future<void>.delayed(const Duration(seconds: 3));
  print('end: background task');
}

Copy link

docs-page bot commented Jan 29, 2025

To view this pull requests documentation preview, visit the following URL:

docs.page/leancodepl/patrol~2512

Documentation is deployed and generated using docs.page.

@github-actions github-actions bot added the package: patrol_cli Related to the patrol_cli package label Jan 29, 2025
@bartekpacia bartekpacia force-pushed the optimization/cli_startup_time branch from f39530d to 2e56598 Compare January 29, 2025 01:42
@bartekpacia bartekpacia changed the title Optimize CLI startup time WIP: Optimize CLI startup time Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: patrol_cli Related to the patrol_cli package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant