hrbrmstr's Daily Drop

Share this post

2022-08-23.01

dailyfinds.hrbrmstr.dev

2022-08-23.01

Grain; wasm3; Jupiter's Webb

boB Rudis
Aug 23, 2022
Share this post

2022-08-23.01

dailyfinds.hrbrmstr.dev

Grain

brown wheatfield
Photo by Johannes Plenio on Unsplash

Grain [GH] is "a programming language that brings wonderful features from academic and functional programming languages to the 21st century."

It aims to modernize innovative features from functional and academic programming languages and make them usable by all coders. Scads of languages have had some funky cool ideas; said ideas are often weird, clunky, and not fully fleshed out. Grain hopes to "bring new life to these ideas and present them in an accessible form that’s easy to use and understand."

It looks and feels like many modern programming languages. If you can read (or write) JavaScript, Swift, or Rust, you can crank through a Grain project pretty well. Hence, we’ll save you from having to stare at a massive dump of language constructs.

Grain's compilation target is WebAssembly (ofc). Once a program is built (see the next section in this edition), you can use Grain to run the resultant Wasm file.

One feature of the Grain binary is that it comes with way more batteries included than most languages. Specifically, it can do more than just compile a grain source file. Here are the current commands:

  • compile: a grain program into wasm

  • run: run a wasm file with grain's javascript runner

  • lsp: start the Grain LSP server

  • doc: generate documentation for a grain file

  • format: format a grain file

They’re all built-in to the grain binary, and I kinda wish more programming languages adopted this convenience.

Recently released version 0.5 of Grain adds some new features to the language and tooling:

  • Arbitrary precision math

  • "Or" patterns and pattern aliases (Grain has a Rust-ish match statement that gets this upgrade)

  • Smarter parser

  • Faster grain

  • Better Wasm code generation and faster Wasm execution

  • Built-in language server (which backs the VS Code Grain extension)

  • Quite a bit more

The goals of Grain are laudable — I’m a big fan of making practical use of more academic constructs — and it’s fun seeing yet-another community supported programming language being developed out in the open. It does have competition, but it has a great vibe and should be something you keep on your radar.

You can follow along Grain's progress via the above GitHub link, their blog, or Twitter.

Share

wasm3

time lapse photography of vehicle light during nighttime
Photo by Solaiman Hossen on Unsplash

Continuing with the theme of Wasm eating the world, there's a new Wasm engine in town: wasm3 [GH]/(@wasm3_engine), the "fastest WebAssembly interpreter, and the most universal runtime."

The universal runtime comes from the fact that it uses cosmopolitan libc as a foundation.

Of course, you can start playing with it right in your browser, which can also run all published WebAssembly System Interface (WASI) modules with zero extra effort or setup (go ahead, type python at that in-browser Wasm3 shell, and don't judge it too harshly by the way it takes python code input).

There are wasm3 bindings for:

  • Python3

  • Rust

  • C/C++

  • GoLang

  • Zig

  • Perl

  • Swift

  • .Net

  • Arduino

  • PlatformIO

  • Particle

  • QuickJS

(we'll be covering some of those less familiar ones over the coming editions).

It passes all the core tests, needs a mere ~64Kb for code and ~10Kb RAM, and kinda runs everywhere. (I'm seriously feeling like a major underachiever now, compared to the Wasm3 team).

If you can suffer through Substack's terrible code block support, let's look at "Hello, world!" in Zig and Grain, and then Wasm execution speed in Grain and Wasm3:

hello.zig:

const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}

Build it:

zig build-exe -O ReleaseSmall -target wasm32-wasi hello.zig

hello.gr:

print("Hello, world!")

Build it:

grain compile --release hello.gr

Wasm3 timings for both binaries:

time wasm3 hello.wasm # Wasm binary is 2,156 bytes
Hello, world!
wasm3 hello.wasm  0.00s user 0.00s system 76% cpu 0.005 total
time wasm3 hello.gr.wasm # Wasm binary is 56,678 bytes
Hello, world!
wasm3 hello.gr.wasm  0.00s user 0.00s system 79% cpu 0.005 total

(Run those a bunch of times and you'll see that Zig's Wasm binary does perform faster, overall, than Grain's.)

It will come as no surprise that Wasm3 is faster than Grain's Wasm runner:

time grain run hello.gr.wasm
Hello, world!
grain run hello.gr.wasm  0.15s user 0.02s system 109% cpu 0.164 total
time wasm3 hello.gr.wasm
Hello, world!
wasm3 hello.gr.wasm  0.00s user 0.00s system 77% cpu 0.008 total

Don't get too excited. Wasm3 is missing a few features from Wasm core:

  • Multiple memories

  • Reference types

  • Tail call optimization

  • Fixed-width SIMD

  • Exception handling

but, I expect the Wasm3 community will make quick work of those gaps.

Leave a comment

Jupiter's Webb

A wide-field view showcases Jupiter in the upper right quadrant. The planet’s swirling horizontal stripes are rendered in blues, browns, and cream. Electric blue auroras glow above Jupiter’s north and south poles. A white glow emanates out from the auroras. Along the planet’s equator, rings glow in a faint white. At the far left edge of the rings, a moon appears as a tiny white dot. Slightly further to the left, another moon glows with tiny white diffraction spikes. The rest of the image is the blackness of space, with faintly glowing white galaxies in the distance.
Webb NIRCam composite image from two filters – F212N (orange) and F335M (cyan) – of Jupiter system, unlabeled (top) and labeled (bottom). Credit: NASA, ESA, CSA, Jupiter ERS Team; image processing by Ricardo Hueso (UPV/EHU) and Judy Schmidt.

As readers likely know, the James Webb Space Telescope is the world’s largest, most powerful, and most complex space science telescope ever built. It's also the most remote end-of-life JavaScript compute node.

Some (most?) readers likely saw the recent Jupiter images captured by Webb, but if not, now you know!

The header image shows some remote galaxies photobombing our massive Sol system cousin's debut picture. Said image "sums up the science of our Jupiter system program, which studies the dynamics and chemistry of Jupiter itself, its rings, and its satellite system" according to Thierry Fouchet, one of the lead scientists on the Webb observation program.

I'll cease blathering more (so you can just look at more Webb content on NASA's site), save to briefly add that — when we want to — it sure seems we humans can collaborate for exceptional good, and we should probably work on doing that more often, if at all possible.

FIN

I'm super glad we can count on Webb to bring some awe and wonder into random weeks ahead. ☮

Share this post

2022-08-23.01

dailyfinds.hrbrmstr.dev
Previous
Next
Comments
TopNewCommunity

No posts

Ready for more?

© 2023 boB Rudis
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing