Moonbit: the fast, compact & user friendly language for WebAssembly
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):
- MoonBit
- Go
- 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)
}
func fib(n int) int {
var aux func(n, acc1, acc2 int) int
aux = func(n, acc1, acc2 int) int {
switch n {
case 0:
return acc1
case 1:
return acc2
default:
return aux(n-1, acc2, acc1 + acc2)
}
}
return aux(n, 0, 1)
}
fn fib(n: i32) -> i32 {
fn aux(n: i32, acc1: i32, acc2: i32) -> i32 {
match n {
0 => acc1,
1 => acc2,
_ => aux(n - 1, acc2, acc1 + acc2),
}
}
aux(n, 0, 1)
}
Below is the result of our benchmark available on github.
🟥 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.