|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Typelevel Native |
| 4 | +category: technical |
| 5 | + |
| 6 | +meta: |
| 7 | + nav: blog |
| 8 | + author: armanbilge |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +#### What is Scala Native? |
| 14 | + |
| 15 | +Scala Native is an optimizing ahead-of-time compiler for the Scala language. Put simply: it enables you to **compile Scala code directly to native executables**. |
| 16 | + |
| 17 | +It is an ambitious project following in the steps of Scala.js. Instead of targeting JavaScript, the Scala Native compiler targets the [LLVM] IR and uses its toolchain to generate native executables for a range of architectures, including x86, ARM/M1, and in the near future web assembly. |
| 18 | + |
| 19 | +#### Why is this exciting? |
| 20 | + |
| 21 | +**For Scala in general**, funnily enough I think [GraalVM Native Image] does a great job summarizing the advantages of native executables, namely: |
| 22 | +* instant startup that immediately achieves peak performance, without requiring warmup or the heavy footprint of the JVM |
| 23 | +* packagable into small, self-contained binaries for easy deployment and distribution |
| 24 | + |
| 25 | +It is worth mentioning that in benchmarks Scala Native handily beats GraalVM Native Image on startup time, runtime footprint, and binary size. |
| 26 | + |
| 27 | +Moreover, breaking free from the JVM is an opportunity to design a runtime specifically optimized for the Scala language itself. This is the true potential of the Scala Native project. |
| 28 | + |
| 29 | +**For Typelevel in particular**, Scala Native opens new doors for leveling up our libraries. |
| 30 | + |
| 31 | +W |
| 32 | + |
| 33 | +I am also excited at the prospect of using Cats Effect to work with (non-Scala) native libraries exposing a C API. [`Resource`] and more generally [`MonadCancel`] provide the necessary combinators for safely navigating manual memory managment. |
| 34 | + |
| 35 | +#### How does it work? |
| 36 | + |
| 37 | +The burden of cross-building the Typelevel ecosystem for Scala Native falls to [Cats Effect] and mostly to [FS2]. |
| 38 | + |
| 39 | +From there, : any project |
| 40 | + |
| 41 | +#### How can I try it? |
| 42 | + |
| 43 | +Christopher Davenport has put up a [scala-native-ember-example](https://github.com/ChristopherDavenport/scala-native-ember-example) and reported some benchmark results! |
| 44 | + |
| 45 | + |
| 46 | +#### What's next and how can I get involved? |
| 47 | + |
| 48 | +* Please try the Typelevel Native stack! And even better deploy it, and do so loudly! |
| 49 | +* Cross-building existing libraries and developing new, Typelevel stack ones |
| 50 | + - a pure Scala [gRPC] implementation built on http4s would be fantastic, even for the JVM. Christopher Davenport has published [proof-of-concept][grpc-playground]. |
| 51 | + - the fledgling [otel4s] project will need pure Scala backends |
| 52 | +* |
| 53 | + - An [NGINX Unit] server backend for http4s promises exceptional performance |
| 54 | + - I kick-started [http4s-curl] |
| 55 | +* Developing I/O-integrated runtimes. |
| 56 | + - [epollcat] supports Linux and macOS and has plenty of opportunity for optimization. |
| 57 | + - A [libuv]-based runtime |
| 58 | + - |
| 59 | +* Tooling. Anton Sviridov has spear-headed two major projects in this area: |
| 60 | + - [sn-bindgen] |
| 61 | + - [sbt-vcpkg] |
| 62 | + |
| 63 | +#### Ember example benchmark |
| 64 | + |
| 65 | +``` |
| 66 | +$ hey -z 30s http://localhost:8080 |
| 67 | +
|
| 68 | +Summary: |
| 69 | + Total: 30.0160 secs |
| 70 | + Slowest: 0.3971 secs |
| 71 | + Fastest: 0.0012 secs |
| 72 | + Average: 0.0131 secs |
| 73 | + Requests/sec: 3815.4647 |
| 74 | +
|
| 75 | + Total data: 1145250 bytes |
| 76 | + Size/request: 10 bytes |
| 77 | +
|
| 78 | +Response time histogram: |
| 79 | + 0.001 [1] | |
| 80 | + 0.041 [114486] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ |
| 81 | + 0.080 [7] | |
| 82 | + 0.120 [5] | |
| 83 | + 0.160 [5] | |
| 84 | + 0.199 [3] | |
| 85 | + 0.239 [5] | |
| 86 | + 0.278 [3] | |
| 87 | + 0.318 [4] | |
| 88 | + 0.357 [3] | |
| 89 | + 0.397 [3] | |
| 90 | +
|
| 91 | +
|
| 92 | +Latency distribution: |
| 93 | + 10% in 0.0119 secs |
| 94 | + 25% in 0.0121 secs |
| 95 | + 50% in 0.0122 secs |
| 96 | + 75% in 0.0125 secs |
| 97 | + 90% in 0.0133 secs |
| 98 | + 95% in 0.0224 secs |
| 99 | + 99% in 0.0234 secs |
| 100 | +
|
| 101 | +Details (average, fastest, slowest): |
| 102 | + DNS+dialup: 0.0000 secs, 0.0012 secs, 0.3971 secs |
| 103 | + DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0011 secs |
| 104 | + req write: 0.0000 secs, 0.0000 secs, 0.0013 secs |
| 105 | + resp wait: 0.0131 secs, 0.0011 secs, 0.3941 secs |
| 106 | + resp read: 0.0000 secs, 0.0000 secs, 0.0010 secs |
| 107 | +
|
| 108 | +Status code distribution: |
| 109 | + [200] 114525 responses |
| 110 | +``` |
| 111 | + |
| 112 | +[Cats Effect]: https://typelevel.org/cats-effect/ |
| 113 | +[epollcat]: https://github.com/armanbilge/epollcat |
| 114 | +[FS2]: https://fs2.io/ |
| 115 | +[GraalVM Native Image]: https://www.graalvm.org/22.2/reference-manual/native-image/ |
| 116 | +[gRPC]: https://grpc.io/ |
| 117 | +[grpc-playground]: https://github.com/ChristopherDavenport/grpc-playground |
| 118 | +[http4s]: https://http4s.org/ |
| 119 | +[http4s-curl]: https://github.com/http4s/http4s-curl/ |
| 120 | +[libuv]: https://github.com/libuv/libuv/ |
| 121 | +[LLVM]: https://llvm.org/ |
| 122 | +[NGINX Unit]: https://unit.nginx.org/ |
| 123 | +[io_uring]: https://en.wikipedia.org/wiki/Io_uring |
| 124 | +[sbt-vcpkg]: https://github.com/indoorvivants/sbt-vcpkg/ |
| 125 | +[Scala Native]: https://scala-native.org/ |
| 126 | +[sn-bindgen]: https://github.com/indoorvivants/sn-bindgen |
| 127 | +[`MonadCancel`]: https://typelevel.org/cats-effect/docs/typeclasses/monadcancel |
| 128 | +[`Resource`]: https://typelevel.org/cats-effect/docs/std/resource |
0 commit comments