Skip to main content

Moonbit: the fast, compact & user friendly language for WebAssembly

· 8 min read

blog-title

The importance of WebAssembly (Wasm), a cross-platform Instruction Set Architecture (ISA), is escalating in Cloud and Edge computing, given its efficiency, security, compactness, and open standard. However, its true potential remains untapped. Low level Wasm languages like Rust and C/C++ are challenging to learn and can be slow for developers because of their long compilation time. On the other hand, high level languages like Golang produce code that's inefficient and bulky, failing to harness the key benefits of Wasm's speed and compactness.

Moonbit aims to be the Wasm-first language that solves all those challenging issues. It’s fast to build and run, generating compact Wasm output, as easy as Golang and with none of the existing options’ legacy cruft. Together with Wasm's natural efficiency, security, and compactness, Moonbit is well-positioned to fulfill Wasm's long-standing promises for Cloud and Edge computing.

Moonbit is led by Hongbo Zhang and his experienced team, who have over a decade of professional experience in language design and development. Zhang contributed to several programming languages, including OCaml, ReScript (was ReasonML/BuckleScript) and Flow. He was the chief architect for key components of the Rescript language toolchain, including its lightning-fast compiler, standard library, and build system.

Moonbit is also influenced by Golang and Rust. It integrates the simplicity of Golang, particularly in its package system and Rust’s expressiveness, including robust pattern matching, type inference, generics, and trait-like ad-hoc polymorphism. Moonbit's fault tolerant type system is designed for speed, parallelizability, and incremental checking to provide best IDE support.

Main focus of Moonbit

Fast - fast to build and run

Moonbit aims to be fast across all the stack including both dev performance and runtime performance.

Moonbit is engineered for whole program optimizations on multi-level intermediate representations(IR), a method that enhances the memory layout to minimize cache misses and provides a superior context for data and control flow analysis. This surpasses most existing link-time-optimization (LTO) structures by facilitating comprehensive understanding of the program's structure, enabling more effective optimizations. Optimizing on multiple level IRs also allows the identification and removal of higher-level redundancies not visible at lower levels.

Quick build performance, especially IDE functionality, is essential for the developer experience. Unlike ReScript and Rust, Moonbit allows function-level parallel semantic analysis. Due to its incremental reanalysis at this granularity, Moonbit is capable of handling vast monorepos and delivers millisecond response times, significantly enhancing IDE scalability.

Compact - tiny Wasm output

Moonbit is designed for effective dead code elimination. It omits language features that hinder this analysis and has a standard library structured for easy dead code removal. Through whole program optimization, Moonbit considerably reduces the final code size, enhancing security and reducing attack vulnerabilities. This also ensures rapid startup in serverless computing environments.

User friendly - simple to learn and easy to use

Moonbit makes programming easier with its automatic memory management, setting it apart from Rust. Unlike Golang, it steers clear of risky elements like pointers or left values. It also provides rich safe features including pattern match, algebraic data types and ad-hoc polymorphisms for data oriented programming.

Beyond a language, Moonbit also serves as a platform, providing an extensive toolset even at its early stage. This includes a high-speed build tool, a package manager, a compiler, an IDE, and a unique, container-free Cloud IDE with offline capabilities, accessible from any location with a browser, distinguishing it from conventional Cloud IDEs.

A taste of MoonBit

The advantages of MoonBit can be illustrated with a simple Fibonacci example. Below is the fib function implemented in three languages (MoonBit, Go, and Rust):

fn fib(num : Int) -> Int {
fn aux(n, acc1, acc2) {
match n {
0 => acc1
1 => acc2
_ => aux(n - 1, acc2, acc1 + acc2)
}
}

aux(num, 0, 1)
}

Below is the result of our benchmark available on github.

Compute time

Wasm size

Compilation speed

177.9 ms

160.8 ms

6357.8 ms

Time to compute fib(46) 10000000 times

253 bytes

498 bytes

1447712 bytes

Size of output Wasm file

1.06 s

9.01 s

2.56 s

Time to compile 626 packages

🟥 MoonBit 🟨 Rust 🟦 Go

From the above example, we can see that MoonBit has the following advantages:

  • Better local type inference: MoonBit infers the type of local function aux
  • Compact wasm size: MoonBit produces the smallest wasm output
  • Good performance: Faster than go and comparable to rust
  • User friendly: MoonBit supports recursive closure like Go, which is very difficult to implement in Rust; MoonBit also supports exhaustive pattern match like Rust, which is much more powerful than Go's switch case.

Current status and roadmap

Moonbit, is a fast moving target, but you can try it with our online IDE, or download the CLI tools. The docs are hosted on github and the vscode extension is available.

The development of an entire language toolchain, previously spanning years or even a decade, has been streamlined through our accrued experience and the establishment of a dedicated talented team from Moonbit's inception. We expect Moonbit to reach beta status by the end of Q2 of 2024, indicating a phase of relative stability, minimal bugs, and a robust Foreign Function Interface (FFI) for interactions with the Wasm host. We will make the source code publicly available once we reach beta quality. Our strategic plans involve Wasm GC integration for Wasm 2.0 and our homebrewed GC for Wasm 1.0, in line with Wasm proposals.

Community and early feedback

You are welcome to post questions on our forum, join our discord or follow our Twitter account.