Why is Hadean Written in Rust?

Rust is an open-source programming language focussed on concurrency, speed, and memory safety. Computer programmers use Rust to develop innovative software applications, such as simulation and game engines, operating systems, file systems, and browser components. By enabling developers to write programs without undefined or unpredictable behaviour, Rust became the ideal programming language for the development of Why is Hadean Written in Rust?

29 March 2021
Richard Edwards

Rust is an open-source programming language focussed on concurrency, speed, and memory safety. Computer programmers use Rust to develop innovative software applications, such as simulation and game engines, operating systems, file systems, and browser components. By enabling developers to write programs without undefined or unpredictable behaviour, Rust became the ideal programming language for the development of the Hadean platform. Its language is syntactically similar to C++ but it ameliorates issues that C/C++ has been struggling with for a long time such as memory errors, difficulties in building concurrent programs, and writing code with security vulnerabilities.

Why Rust is ideal for Hadean

The Hadean platform implements a unique distributed process model that allows massive scale, dynamically allocating more processing power as required. It requires a language that is able to support high-performance programming for the operating system and scale applications across a distributed cloud and edge network. Rust solves problems present in other low-level languages like C and C++ regarding performance and reliability. It uses a very strict compiler that checks every variable you use and every memory address you reference. Rust’s main features in relation to Hadean include:

Zero Cost Abstractions and Monomorphisation

Rust is based on the concept of zero-cost abstraction, which means that “What you don’t use, you don’t pay for”  [Stroustrup, 1994]. In most cases, the abstractions are resolved at compile-time, leading to ‘zero-cost abstractions’ without any runtime overhead. This allows the Hadean platform’s code to be optimised at compile-time, making it already safe and with no runtime overhead.

Rust lets us write highly composable and re-usable code that gets optimised out at runtime. A good example of this is its trait system: by making all our channels conform to a particular trait, Hadean developers can write code that is general over any kind of channel implementation we might want to use (or come up with in the future). But when code is compiled, Rust performs monomorphisation, generating high-performance code specialised to each type of channel, so the Hadean developers don’t pay any performance penalty for that flexibility. This is particularly important for channels, which are a core part of Hadean and used everywhere in both platform and user code — any performance cost in the channel implementation can compound to produce a significant slowdown across the system as a whole!

Asynchronous I/O

Asynchronous programming is very well-integrated and stable in Rust compared to C++, allowing Hadean developers to run several units of work on each application thread and thereby providing better performance and enhanced responsiveness to the final operating system. Making better use of operating system resources allows Hadean applications to scale better on a given number of cores, which is critical for Hadean Simulate.

No Garbage Collector

Rust compiles to native code that runs without a heavy runtime or complicated garbage collector.  This allows Hadean to provide abstractions that enjoy the predictable performance, and avoid surprising the user with functions that sometimes pause while we perform bookkeeping.  Additionally, since Hadean developers write a lot of networked services, predictable performance on a machine allows us to avoid introducing jitter, which can lead to buffers filling up and surprising backpressure being exerted throughout the system — a performance issue that’s often hard to debug.

Borrow Checker

By keeping track of where data is used throughout the program and by following a set of rules, the borrow checker can determine where data needs to be initialised and where it needs to be freed (or dropped, in Rust terms). It auto-inserts memory frees for you, giving you the convenience of a garbage collector with the speed and efficiency of manual management.

rust-hadean

The chart above shows concurrency and memory safety issues that are fundamentally impossible to get in the safe subset of Rust. The issues that Rust can prevent are some of the most dangerous since they often cause compromised secrets, denial of service, and remote code execution vulnerabilities. Some of Rust’s most common use cases are: 

  • Powerful, cross-platform command-line tools
  • Distributed online services
  • Embedded devices
  • Anywhere else you would need systems programming, like browser engines

Rust is a systems programming language with a predictable and consistent runtime behaviour that makes it an excellent choice for Hadean’s purposes. Indeed, thanks to its static type system, carefully selected features, and having no garbage collector, Rust compiles into performant and predictable code. It is statically typed, so the type system helps the developer to deter certain classes of bugs during compilation.  When using C++ for the Hadean platform, developers encountered a series of frictions such as: 

  • Unexpected variable modification causing pointer invalidation
  • Iterators not being checked for validity before use
  • Uninitialised variables causing undefined behaviour such as odd scaling in matrix multiplication codes, test failure, and massive cells that broke behaviour.

Finally, Rust has a very accessible and welcoming community where developers can easily share their capabilities in various projects through forums, chat platforms, and events. This friendly environment made it even easier for developers to efficiently build the Hadean platform using Rust. Download the Hadean Platform SDK today and experience first hand the benefits of Rust.

Back