
Introduction to Dart VM - skybrian
https://mrale.ph/dartvm/
======
bsaul
I've always had the hint that dart was a technological marvel, but then when i
look at the dart language itself compared to today's language (rust & swift
for example) i feel a bit underwhelmed.

And the worst part is what i feel was the first environment outside erlang to
get real actor-like concurrency is now barely mentionning actors in its
documentation. It looks like they dumbed down the recommended server
architecture to event-loop style to make it more accessible, instead of going
full speed on actors.

Please correct me if i'm wrong..

~~~
mraleph
I am not a language designer myself, but I think "avoid overwhelming" was an
explicit idea behind initial design of Dart. Yes, this led to a language that
some might consider boring, but it also led to a language that a lot of people
can easily pick up and use (with little surprise and reasonable amount of
fun).

There are few things that I would like to have in Dart (ADT and pattern
matching for example), but otherwise I actually think its a fun language to
program in.

> And the worst part is what i feel was the first environment outside erlang
> to get real actor-like concurrency is now barely mentionning actors in its
> documentation.

Dart went through a lot of turmoil in the past few years, but now that this
turmoil is over we hope to explore concurrency story again.

Dartino (which was a dialect of Dart targeting IOT devices) had really fun
concurrency story centered around lean isolates, coroutines and fast message
passing based on deeply immutable data and we think there might be something
here that we could incorporate into Dart proper.

~~~
rkangel
I also agree with a desire for ADT and pattern, and also some better handling
of Null (see Kotlin).

But I also agree that it's a very unsurprising language, in a good way.

~~~
mraleph
Non-nullability is actually being actively worked on[1]. Currently language
team is working on specifying the feature and figuring out the path towards
it.

[1] [https://github.com/dart-
lang/language/issues/110](https://github.com/dart-lang/language/issues/110)

~~~
rkangel
That's great to hear, thanks for pointing it out.

------
bassman9000
This part on the unoptimized vs optimized code looks interesting:

 _Some functions contain very long running loops, so it makes sense to switch
execution from unoptimized to optimized code while the function is still
running. This process is called on stack replacement (OSR) owing its name to
the fact that a stack frame for one version of the function is transparently
replaced with a stack frame for another version of the same function._

~~~
mraleph
That is fairly classical thing to do - a lot of VMs, including JVM and JS VMs
do OSR.

~~~
bassman9000
Replacing the whole method/function seems pretty straight forward. Any good
JS/JVM articles on how code is replaced mid-function?

Edit: I see the article does mention IL _deoptimization IDs_. I assume those
are used as "anchors" to know where to continue with the optimized rest of the
loop.

~~~
mraleph
Yes, your intuition is correct. The way it works in Dart VM is like so:

Each loop has an interruption point somewhere (e.g. in the header). If a very
hot loop is found VM would enter runtime at that interruption point, where it
would synchronously initiate compilation of an optimized version of the
function for OSR _at this particular interruption point_.

After unoptimized IL is contsructed it is "shaken": a matching interruption
point is found in the graph and everything not reachable from it is shaken
(which drops all code before the loop).

After then the graph is transformed a bit - for each local variable alive at
the interrupt point we introduce artificial parameter - so that we could
simply treat OSR substitution kinda like a call.

Then graph is optimized and compiled.

Finally when we return back to the original unoptimized code we would do a
tail call to the new optimized version.

(I will cover this in the "Introduction to Dart VM" in more details and with
some drawings, when I reach this).

------
paperwork
Can dart be evaluated within the browser? I’ve been looking for a language
which can be executed within the browser (without needing a connection to any
server).

I would like to write some blog posts explaining technical concepts and
provide executable, interactive code. As far as. I can tell, currently
JavaScript is the only language for the job. All languages which transpile to
js seem to require (out of browser) command line tools

~~~
rkangel
Dart was originally designed as a JS 'replacement', designed to supplant it.
The talked about browsers executing it directly, but as a transition step
compile-to-JS has always been a first class citizen for Dart.

Now of course no language is going to replace JS, it's going to be compile to
WASM or JS, and Dart is now focused on things like Flutter where it is used in
a 'Ahead of Time' compiled form.

