Guide how to compile (AOT) of *.dart in Dart 2.0

AOT compilation was possible on Dart 1.24 through CLI with using appropriated app-aot value for --snapshot-kind, but I’ve recently tried to compile *.dart source code on Dart 2.0 like this:

$ dart --snapshot-kind=app-aot --snapshot=app.snapshot example/http_handler.dart 

and it returned error with following context:

../../runtime/bin/main.cc: 251: error: Dart 2.0 AOT compilations only accept Kernel IR files as input ('example/http_handler.dart' is not a valid Kernel IR file).

Dumping native stack trace for thread 1428
  [0x0000560cff5767df] dart::Profiler::DumpStackTrace(void*)
  [0x0000560cff5767df] dart::Profiler::DumpStackTrace(void*)
  [0x0000560cff7b2302] dart::Assert::Fail(char const*, ...)
  [0x0000560cff1f3083] dart::bin::RunMainIsolate(char const*, dart::bin::CommandLineOptions*)
  [0x0000560cff1f3ae7] dart::bin::main(int, char**)
  [0x0000560cff1f43c9] Unknown symbol
-- End of DumpStackTrace
Aborted (core dumped)

Can you, please, provide a detailed guide how to AOT compile source as it seems Dart 2.0 isn’t able to compile *.dart using CLI.

Author: Fantashit

15 thoughts on “Guide how to compile (AOT) of *.dart in Dart 2.0

  1. We would love to have aot compilation in distribution – use case: faas

    aws lambda cold start with 1500MB Ram, Dart, Script snapshot: 950ms, App-JIT snapshot: 700ms, App-AOT snapshot: 70ms

  2. @mraleph

    We are a company that currently deals with micro services. Our uses cases involves internal and 3th party deployments. But we are looking to grow into the mobile and other market segments.

    Here are some reasons:

    • Restrictions from Clients. We have had clients who’s had issues with languages like PHP, that dealt with code injections. The have very strict requirements like: “No open source projects”, “no clear code deployments”, “no …”. I agree that AOT is not a solution ( there are way to secure languages like PHP to prevent code injection ) but we are not in the business anymore to change client opinions anymore. Its the old “What the client wants, we provide” attitude.

    • Micro service deployments. From our quick internal tests, Dart suffers from the same issue as most JIT languages ( .Net/Java/… ), memory allocation per service. Our designs for the back-end include up to hundreds of micro services per server and the default memory stack on each service its JIT is a wast of resources. JIT can be faster then AOT but it tends to never deal good with memory issues.

    • Easier deployment. Note: Dart Snapshot deployment can do the same but exposes the code when doing so.

    • Proprietary code. Some of our services are deployed on 3th party client servers, As you can imagine, we prefer not to have our code base open and public. We currently use Go to solve this issue. But as we have plans to expand, thus we are currently contemplating Dart because its interesting for having a single stack language combined with Flutter on all platforms ( Front, Back, Mobile ). Its easier on the developers and hiring process then having multiple languages being supporter internally.

    For us Dart is a interesting project, as it combines the fast ability to use the JIT to develop and potentially using the AOT for deployment.

    Please note that this is not a open discussion on preferences or why JIT or AOT is better for X or Y. These are our requirements as requested. For us its simply business decisions and not personal preferences.

    We had the strong impression from Dart 1 articles and information scatter over the net, that Dart was AOT ready and distributed as such. It was surprising to find out during our testing, that Dart 2 ( we even tested 2.2 ) was not. In our experience when a language or project tends to back strip features from the default deployment, these features tend to get little support. This is why the original question got asked.

    Color us surprised that the Web SDK did not include AOT.

    Valid values are: none, kernel, app-jit

    I hope this answer your questions.

  3. @Indribell It seems several other programming languages, for example C, C++, Rust, would suit your use cases better. On the backend, they have unparalleled performance; on the frontend, they can be compiled to WebAssembly.

    Flutter, currently, neither runs on the web nor can Dart be compiled to native code to run on servers.

  4. @Rusticc

    Rust, C, C++ are languages that are too focused on low end system programming and as such require a different technology level of employees. The best way to describe C++/Rust is overkill for the tasks at hand.

    After finding out that Dart had no focus on Back-end development, we switched over to Crystal ( crystal-lang.org ) and have been enjoying the experience.

  5. Those who beat the Dart drum always say that it’s JIT and AOT. I have been investing a small amount of time in taking Dart seriously for the server, with the end goal being to deploy to FaaS like Lambda. For this I knew I would need AOT to compete with alternatives like Node + TypeScript to keep the cold start times down.

    The amount of messaging on AOT is so great that I was actually surprised that dart --build bin/main.dart didn’t work. I started looking thru the docs and couldn’t find anything. I was sure I was missing something. I found this issue and realized that I was not. Dart SDK does not support AOT out of the box from what I can tell.

    Why the misalignment between messaging and delivery? I find it to be misleading. Is it because this capability is provided by community tooling?

  6. @lukepighetti

    The JIT and AOT messaging you see is Dart 1. Pay close attention to the article post dates and when Dart 2 came out. With Dart 2 it seems they did a total turnaround and switched to pure Client side focus ( this is even acknowledge in other issues tickets ). Another issues i advice you to take care off. Dart 2 JIT has fairly large performance / memory regressions going on compared to Dart 1 JIT.

    Currently, if you want AOT, its up to you to manually compile this into the SDK. Flutter has the AOT for its supported platforms because they do this already. This is why a lot of AOT issues seems to be focused around Flutter related bugs.

    My advice if you want a fast back-end, simply use Go or Crystal.

    Crystal ( crystal-lang.org ) + Sentry ( https://github.com/samueleaton/sentry ) + Linux Parallel ( apt-get install parallel… if you run a lot of micro services and want to parallel run each sentry watcher ). This combination is a dream for developing and gives the same development feeling like Dart / PHP / … But with 3 times more performance then Dart 2 JIT currently offers. A simple HTTP client in Dart eats close to 130 Megabyte where as the same client in Crystal uses barely 3 Megabyte memory. And the file watcher like Sentry compile in the background, so you can test ultra fast every change.

    Dart simply runs into the same issue that Java ran into with its JIT. With the difference that Java has had a lot more time to develop their JIT performance.

  7. I am searching how to aot dart code on desktop, and finally find this issuse. I think aot has many advantages: code protection, quick startup, less memory consumption, easy deployment. I am glad to see that aot on desktop is under consideration.

  8. Yeah, I was really excited when I saw that Dart official site says it can compile to native code and JavaScript and do all of backend server, desktop client, web, and mobile app (iOS/Android) development. And then I found out that it only compiles to native code for the mobile app part, not for backend server nor desktop client, what a bummer.

    As the chief technical officer of a small IT service company (tech department has less than a dozen tech engineers) to provide commercial software services to clients, I’d really like to focus on a tech stack centered on one programming language instead of currently using Go for backend system, C# for desktop application, Vue for web and Java/Swift for mobile apps.

    One thing I’m currently wondering is, if I build a snapshot of the app-jit kind, how hard is it to decompile it back into human readable source code? As a company that provides commercial software services, we definitely don’t want our clients to get their hands on the source code.

  9. We’re working on packing this up to make it more approachable. I can’t offer a timeline, but we are making good progress!

  10. version 2.3 has included dart2aot, and I tried, it is ok. But I am wondering if it can be combined into one exe file.

Comments are closed.