Skip to content
This repository was archived by the owner on Mar 18, 2021. It is now read-only.
This repository was archived by the owner on Mar 18, 2021. It is now read-only.

Support for AOT compilation #669

Open

Description

@sriio

With Dart 2.3, you can compile your backend ahead of time but it doesn't work with aqueduct ...

Steps to reproduce:

Download Dart 2.3 here : http://gsdview.appspot.com/dart-archive/channels/stable/raw/2.3.0/sdk/

Create a new aqueduct project with aqueduct create test_project

Go to test_project and run dart2aot bin/main.dart bin/main.dart.aot

Problem:

You get this error :

 dart2aot bin/main.dart bin/main.dart.aot
error: import of dart:mirrors with --enable-mirrors=false

Cause:

Like the case of Flutter, the package 'mirrors' doesn't work with aot compilation ... No workaround, just find a way to replace it (with reflectable for example which is ok with aot)

Activity

  1. itsjoeconway commented on May 7, 2019

    @itsjoeconway
    Contributor

    What would be the benefit of AOT compilation for an Aqueduct application?

  2. sriio commented on May 7, 2019

    @sriio
    Author

    Many :

    • Reduce memory (example : with an Angel application, before ~250mb after ~30mb)
    • Reduce startup time
    • Better performance overall
    • Easier deployement
    • Lighter docker image
    • ...

    Documentation in relation : https://github.com/dart-lang/site-www/blob/a66d468245b84451aa40574db4ba81ee09bd9515/src/_tutorials/server/get-started.md#8-compile-for-production

  3. itsjoeconway commented on May 7, 2019

    @itsjoeconway
    Contributor

    How does this compare to snapshotting? A simple, snapshotted Aqueduct app is showing up as 39.6MB of runtime memory usage.

    What is the actual reduction in startup time? How is deployment easier? How is the Docker image 'lighter'? How is the overall performance improved compared to JIT for a long-running process like a server application?

  4. sriio commented on May 7, 2019

    @sriio
    Author

    How does this compare to snapshotting? A simple, snapshotted Aqueduct app is showing up as 39.6MB of runtime memory usage.

    Memory usage are similar between snapshot jit and aot. Maybe not the best argument.

    What is the actual reduction in startup time?

    App-JIT snapshot vs App-AOT snapshot : https://github.com/dart-lang/sdk/wiki/Snapshots

    How is deployment easier?

    No runtime, only dartaotruntime executable, your dart.aot and dart.aot.dill files (plus your assets if needed)

    How is the Docker image 'lighter'?

    Cause of previous point, we don't need google dart image (with runtime) and we can use alpine image for example (maybe scratch with some effort ?)

    How is the overall performance improved compared to JIT for a long-running process like a server application?

    Tests on my computer show me better performance overall for my simple REST API (response time, processing data). I don't have enough back to argue about long-running process (cause yes I know we loose one of the advantage of JIT on this point).

  5. itsjoeconway commented on May 7, 2019

    @itsjoeconway
    Contributor

    I just fired up a snapshotted Aqueduct application and startup time was effectively instantaneous - I'm not sure applications that run for days/weeks/months at a time need a millisecond-order-of-magnitude improvement at startup.

    Is deployment really easier? If you're containerizing, I don't see much of a difference here?

    Is there a way to measure the size of a Docker image for an AOT vs. JIT compiled images? What are the actual savings quantified? Is there an example of this Dockerfile available?

    In your performance testing, have you accounted for JIT? I'm not sure how runtime performance would be any different once the VM is warmed up. What are the actual tests & their measurements you are evaluating here?

  6. sriio commented on May 7, 2019

    @sriio
    Author

    Is deployment really easier? If you're containerizing, I don't see much of a difference here?

    In my case, I containerize my app but maybe not everybody ...

    Is there a way to measure the size of a Docker image for an AOT vs. JIT compiled images? What are the actual savings quantified? Is there an example of this Dockerfile available?

    I created a sample angel application with 3 docker images (jit, snapshot & aot) (i'm using the official google dart image, aot use ubuntu image) : https://github.com/sriio/sample-dart-hello.

    Building images :

    docker build -f Dockerfile.jit -t sample-dart-jit .
    docker build -f Dockerfile.snapshot -t sample-dart-snapshot .
    docker build -f Dockerfile.aot -t sample-dart-aot .
    

    Images size :

    docker images
    REPOSITORY                        TAG                            IMAGE ID            CREATED              SIZE
    sample-dart-aot                   latest                         d68f6c7606a3        About a minute ago   117MB
    sample-dart-jit                   latest                         8415ce919ec0        About an hour ago    637MB
    sample-dart-snapshot              latest                         aa7545a373b6        About an hour ago    640MB
    

    Stats :

    docker stats
    CONTAINER ID        NAME                       CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
    8ceb16264950        sample-dart-aot-app        0.00%               16.81MiB / 1.934GiB   0.85%               788B / 0B           0B / 0B             2
    e9775c0f7255        sample-dart-snapshot-app   0.00%               45.92MiB / 1.934GiB   2.32%               2.02kB / 0B         2.54MB / 0B         6
    a59faf594521        sample-dart-jit-app        0.00%               131.9MiB / 1.934GiB   6.66%               2.2kB / 0B          2.38MB / 0B         7
    

    In your performance testing, have you accounted for JIT? I'm not sure how runtime performance would be any different once the VM is warmed up. What are the actual tests & their measurements you are evaluating here?

    As I said before, I don't have enough back to argue much more on performance. Dart 2.3 is not actually official, I only tested with my case (REST API without templating, etc ...). I can't give you full bench or other. Btw, compare VM warmed up (optimized only for some critical paths and after a huge? delay and amount of calls) against an aot build is not the best idea imo.

  7. itsjoeconway commented on May 7, 2019

    @itsjoeconway
    Contributor

    The disk size of the image is significant and worth looking at. There is not a way to disable mirrors right now. We will look at solutions for producing AOT builds. Thanks for putting together the numbers.

  8. sriio commented on May 8, 2019

    @sriio
    Author

    Ok, good to hear.

    I updated the aot docker image in my sample project. It's even better

    docker images                                                
    REPOSITORY                        TAG                                  IMAGE ID            CREATED             SIZE
    sample-dart-snapshot              latest                               240ef99f0d4e        45 seconds ago      640MB
    sample-dart-jit                   latest                               1a556a3f72fd        2 minutes ago       637MB
    sample-dart-aot                   latest                               c36a0f6bd7d0        16 minutes ago      32.5MB
    
  9. listepo commented on May 23, 2019

    @listepo

    It would be great to have AOT compilation for the Aqueduct

  10. itsjoeconway commented on May 24, 2019

    @itsjoeconway
    Contributor

    AOT for Aqueduct is currently under development.

  11. Hell0wor1d commented on May 28, 2019

    @Hell0wor1d

    good job, thanks for sharing.

  12. dunsunik commented on May 29, 2019

    @dunsunik

    It's gone be a killer feature for us !
    Thanks.

  13. itsjoeconway commented on Jun 27, 2019

    @itsjoeconway
    Contributor

    Status update:

    The first phase of this effort is complete. The effort removed all reflection from the framework and moved it to a separate runtime library (that is used by default when running your application).

    The next phase will add a aqueduct build command that 1) generates a runtime library that doesn't use reflection specific to your project and 2) compiles an AOT build of your project that links to that runtime library instead of the reflection-based runtime.

    The end result is that you'll be able to run/debug quickly using the same way you do currently (using reflection). When you are ready to ship, you will run aqueduct build and it will emit a single binary of your application. edit: The compiled binary will take the same arguments as aqueduct serve.

  14. WNemencha commented on Aug 18, 2019

    @WNemencha

    Hello, any news on this?

  15. itsjoeconway commented on Aug 18, 2019

    @itsjoeconway
    Contributor

    Sure thing. Remaining effort:

    • Moving the runtime library generation out of Aqueduct into its own package; this is critical since safe_config is also mirrors-based and needs to provide its own runtime.
    • Creating tooling to generate runtimes and swap them in for Aqueduct test files; this is how we'll get to the same test coverage for mirror/generated runtimes.
    • Provide generated runtimes that pass tests
  16. 32 remaining items

  17. nuriamp95 commented on May 1, 2020

    @nuriamp95

    What is the status of this? Thank you for the work

  18. sjapps commented on May 17, 2020

    @sjapps

    I'm using this framework for a backend i'm writing. I've been following this thread and I'm hoping to launch my backend service with this. Please tell me this effort isn't abandoned!

  19. lukepighetti commented on May 17, 2020

    @lukepighetti

    Not sure why people think this is abandoned... this is one of the best maintained backend packages I've ever seen.

  20. itsjoeconway commented on May 18, 2020

    @itsjoeconway
    Contributor

    It's not abandoned, but it has certainly slowed down as we grapple with a post-Covid business landscape. This feature does exist in the 3.3.0-b1 release, but the dependency on analyzer caused issues with dependency resolution for non-prerelease users. I'd like to push a 4.0.0-b1 with this feature to pub this week.

  21. renlite commented on May 20, 2020

    @renlite

    Why don't you use a builder (like json_serializable) for the ORM during dev-process. So it would be possible to start the server with >dart "appName" (JIT) or to compile the server with >dart2native + start. No more mirrors/reflections.

  22. dxps commented on Jun 2, 2020

    @dxps

    4.0.0-b1 update: on macOS (10.15.5), aqueduct build creates a working executable! ✨

    $ aqueduct build
    -- Aqueduct CLI Version: 4.0.0-b1
    -- Aqueduct project version: 4.0.0-b1
    Resolving ASTs...
    Generating runtime...
    Generated runtime at 'file:///{path-to}/heroes/build/generated_runtime/'.
    Compiling package 'runtime'...
    Package 'runtime compiled to 'file:///{path-to}/heroes/build/packages/runtime/'.
    Compiling package 'aqueduct'...
    Package 'aqueduct compiled to 'file:///{path-to}/heroes/build/packages/aqueduct/'.
    Compiling package 'safe_config'...
    Package 'safe_config compiled to 'file:///{path-to}/heroes/build/packages/safe_config/'.
    Copying application package (from 'file:///{path-to}/heroes/')...
    Application packaged copied to 'file:///{path-to}/heroes/build/packages/heroes/'.
    Fetching dependencies (--offline --no-precompile)...
    Finished fetching dependencies.
    Compiling...
    Generating AOT kernel dill.
    Generating AOT snapshot.
    Generating executable.
    Marking binary executable.
    Generated: /{path-to}/heroes/heroes.aot
    
    Success. Executable is located at 'file:///{path-to}/heroes/heroes.aot'.
    $

    Very small size, instant startup time, fully working:

    $ du -hs heroes.aot
    7.2M	heroes.aot
    $
    $ file heroes.aot
    heroes.aot: Mach-O 64-bit executable x86_64
    $
    $ ./heroes.aot
    [INFO] aqueduct: Server aqueduct/1 started.
    [INFO] aqueduct: PostgreSQL connecting, heroes@localhost:54322/heroes.
    [INFO] aqueduct: GET /heroes?name=ap 48ms 200
    [INFO] aqueduct: GET /heroes 4ms 200
  23. agordeev commented on Jun 25, 2020

    @agordeev

    Can aqueduct: 4.0.0-b1 be used as a dependency for Flutter project? I'm trying to use it, but still getting Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime

  24. dxps commented on Jun 25, 2020

    @dxps

    IMO you cannot. And I don't even see it as a healthy approach.

    That error is simply related to the fact that currently Aqueduct uses dart:mirrors, and during the AOT build it generates the artifacts (instead of doing the reflection at runtime). That is one of the main features of 4.0.0-b1 version.
    Flutter and Aqueduct have different build processes.

    Related to the need:

    • If you want to share some models (and logic), an external model lib might help.
    • Still, you may want to have the back-end and front-end sides totally independent, thus giving more flexibility and freedom.
  25. agordeev commented on Jun 25, 2020

    @agordeev

    Ah, such a pity. Aqueduct ORM's Serialize doesn't integrate with json_serializable at all, so I thought I could use models on both sides.
    It seems the only way is to implement toJson/fromJson on the client side manually, which is a huge drawback.

  26. Jonas-Sander commented on Jul 1, 2020

    @Jonas-Sander

    Hey, I'm using local packages which I'm importing with path and getting this error:

    *** Uncaught error
        Bad state: Bad state: 'pub get' failed with the following message: Because every version of [my_aqueduct_package] from path depends on [local_package_via_path] from path which is forbidden, [my_aqueduct_package] from path is forbidden.
    So, because runtime_target depends on [my_aqueduct_package] from path, version solving failed.
    
      **** Stacktrace
      * #0      Build.getDependencies (package:runtime/src/build.dart:120:7)
      * <asynchronous suspension>
      * #1      Build.execute (package:runtime/src/build.dart:100:11)
      * <asynchronous suspension>
      * #2      BuildExecutable.execute (<data:application/dart>:13:259)
      * #3      main (<data:application/dart>:9:35)
      * #4      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:298:32)
      * #5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:12)
      ****
    

    This package is 2 directives up, so it is specified in pubspec.yaml like this:

      my_local_package:
        path: ../../my_local_package
    

    Any help?

  27. 7-Mav-7 commented on Jul 31, 2020

    @7-Mav-7

    Hey, I'm using local packages which I'm importing with path and getting this error:

    *** Uncaught error
        Bad state: Bad state: 'pub get' failed with the following message: Because every version of [my_aqueduct_package] from path depends on [local_package_via_path] from path which is forbidden, [my_aqueduct_package] from path is forbidden.
    So, because runtime_target depends on [my_aqueduct_package] from path, version solving failed.
    
      **** Stacktrace
      * #0      Build.getDependencies (package:runtime/src/build.dart:120:7)
      * <asynchronous suspension>
      * #1      Build.execute (package:runtime/src/build.dart:100:11)
      * <asynchronous suspension>
      * #2      BuildExecutable.execute (<data:application/dart>:13:259)
      * #3      main (<data:application/dart>:9:35)
      * #4      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:298:32)
      * #5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:12)
      ****
    

    This package is 2 directives up, so it is specified in pubspec.yaml like this:

      my_local_package:
        path: ../../my_local_package
    

    Any help?

    Ran into this issue myself today, replacing the local packages with absolute paths in pubspec.yaml fixed it for me.

  28. maurobotta commented on Jul 31, 2020

    @maurobotta

    When will available 4.0 version (usable) ?

  29. jayjah commented on Sep 28, 2020

    @jayjah

    Dart 2.9.2 and Aqueduct 4.0.0-b1 ist not compatible. The aqueduct build command crashed with the following error:

    -- Aqueduct CLI Version: 4.0.0-b1
    -- Aqueduct project version: 4.0.0-b1
    Resolving ASTs...
    Generating runtime...
    *** Uncaught error
    Bad state: Failed assertion: boolean expression must not be null
    **** Stacktrace


    Are you aware of these error? Do I have do downgrade dart to 2.8 to successfully build an aot snapshot of my server???

  30. BenjaminThong commented on Oct 30, 2020

    @BenjaminThong

    Dart 2.9.2 and Aqueduct 4.0.0-b1 ist not compatible. The aqueduct build command crashed with the following error:

    -- Aqueduct CLI Version: 4.0.0-b1
    -- Aqueduct project version: 4.0.0-b1
    Resolving ASTs...
    Generating runtime...
    *** Uncaught error
    Bad state: Failed assertion: boolean expression must not be null
    **** Stacktrace

    Are you aware of these error? Do I have do downgrade dart to 2.8 to successfully build an aot snapshot of my server???

    Same issue with dart 2.10.2

  31. tusharsadhwani commented on Nov 10, 2020

    @tusharsadhwani

    ^ same issue with 2.10.3 and 2.11 beta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions