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

background compile scripts. #96

Open
bsutton opened this issue Aug 14, 2020 · 14 comments
Open

background compile scripts. #96

bsutton opened this issue Aug 14, 2020 · 14 comments

Comments

@bsutton
Copy link
Collaborator

bsutton commented Aug 14, 2020

Currently our start up time for non-compiled scripts is slower than bash.

We could improve this by doing a background compile of a script when it is launched.

On first launch we run the script using the standard jit launch.
However we also start a background compile.

Next time the script is launched we check the timestamp on the script against the time stamp of the compile.
If the compile is more recent then we launch from the compiled version.

We still have the over head of dshell being loaded so this may not end up improving the launch time.
Worth an experiment.

An AOT snapshot doesn’t include the Dart runtime. Consider using snapshots if you’re distributing multiple programs and disk space is limited.

@whoizit
Copy link
Contributor

whoizit commented Aug 23, 2021

~ > time dcli scripts/footq.dart
dcli scripts/footq.dart  13.12s user 2.94s system 72% cpu 22.127 total
~ > time dcli scripts/footq.dart
dcli scripts/footq.dart  13.34s user 2.74s system 121% cpu 13.190 total
~ > time dcli scripts/footq.dart
dcli scripts/footq.dart  13.11s user 2.58s system 135% cpu 11.604 total

~ > time .dcli/bin/footq
.dcli/bin/footq  0.08s user 0.05s system 46% cpu 0.296 total

13 seconds is too long for my small script https://codeberg.org/dnkl/foot/issues/604#issuecomment-249112

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 23, 2021

So why are you not simply using the compiled versions?

dcli install
dcli compile --install --overwrite scripts/footq.dart
footq

Running 'dcli install' adds the ~/.dcli/bin directory to you PATH.

Here is a slightly simplified version of your script using dcli's existing functions:

#! /usr/bin/env dcli
import 'package:dcli/dcli.dart';

void main() {
  var sock_path =
      '${env["XDG_RUNTIME_DIR"]}/foot-${env["WAYLAND_DISPLAY"]}.sock';

  if (processExist('foot')) {
    'footclient --no-wait'.run;
  } else {
    if (exists(sock_path)) {
      delete(sock_path);
    }
    'foot --server --hold'.start(detached: true);

    while (true) {
      if (processExist('foot')) {
        'footclient --no-wait'.run;
        break;
      } else {
        sleep(100, interval: Interval.millseconds);
      }
    }
  }
}

bool processExist(String name) {
  var ph = ProcessHelper();

  for (var pd in ph.getProcesses()) {
    if (pd.name == name) {
      return true;
    }
  }

  return false;
}

I've added a method ProcessHelper().isProcessRunning(String name) which will be part of the next release of dcli.

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 23, 2021

Also my times are much faster:

time ./footq.dart

real 0m3.309s
user 0m4.616s
sys 0m0.686s

try adding a setVerbose(enabled: true) at the top of your script to see where it might be stalling.

@whoizit
Copy link
Contributor

whoizit commented Aug 24, 2021

Thank you very much for your fixes. The code has become very nice.
With your fixes it is 5 seconds faster. I have old CPU (AMD FX-4300)
Of course I am using the compiled version and it's very fast.
Thaks for isProcessRunning()

~ > time dcli ./footq2.dart
info: client.c:420: exit-code=0
dcli ./footq2.dart  8.78s user 1.77s system 150% cpu 6.992 total

~ > time footq
info: client.c:420: exit-code=0
footq  0.06s user 0.05s system 9% cpu 1.088 total

@whoizit
Copy link
Contributor

whoizit commented Aug 24, 2021

@bsutton quick question. Сall function ProcessHelper().isProcessRunning(String name) is too long, how-to do it shorter?
I do:

  var ph = ProcessHelper();
  if (ph.isProcessRunning('foot')) {

But I'm not sure this is a good way to do it

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 24, 2021 via email

@whoizit
Copy link
Contributor

whoizit commented Aug 24, 2021

practically the same thing dmypy does for mypy.

I think this should be implemented as an optional daemon that would compile scripts in the background if they change.

https://mypy.readthedocs.io/en/stable/mypy_daemon.html
https://manpages.debian.org/testing/mypy/dmypy.1.en.html
https://command-not-found.com/dmypy

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 25, 2021 via email

@whoizit
Copy link
Contributor

whoizit commented Aug 25, 2021

on step 1 we should start compiling after running (in background), not in the same time. because it can slow down startup time for jit.

on step 2, we should store the timestamp when the original file (for example /home/user/myscript.dart) was modified. For example into /home/user/.dcli/home/user/myscript.dart.timestamp.

on step 3 we should compare modified time for script /home/user/myscript.dart with timestamp stored in /home/user/.dcli/home/user/myscript.dart.timestamp

@whoizit
Copy link
Contributor

whoizit commented Aug 25, 2021

sounds pretty simple

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 25, 2021 via email

@whoizit
Copy link
Contributor

whoizit commented Aug 25, 2021

the time of modification of the original file will be different from the time of creation/modification of the compiled file. I don't know what you mean.

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 25, 2021 via email

@whoizit
Copy link
Contributor

whoizit commented Aug 25, 2021 via email

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