Love Rust? Then OCaml's New Eio Library is for You
Communications Officer
We’ve come to expect a lot from the programming languages we use. We want the memory safety of Java, the performance of C/C++, and the concurrency of Go. On top of this, we need robust cybersecurity tools to protect us from the many risks and vulnerabilities in the world, all in an intuitive and easy-to-use package for programmers.
You can expect all of the above with OCaml 5. The new library Eio introduces some great new features that let the programmer write concurrent code in a way that best suits them. Eio is fast, solves the function colouring problem, and can use effect handlers to let the developer customise the scheduling algorithm rather than baking it in at runtime. If you’re a fan of how Rust delivers fast and high performing concurrent code, OCaml 5’s Eio is a close match, with some additional features.
Eio gives OCaml a new edge on speed, ease-of-use, portability, and security. It matches Rust’s reputed performance on the same points, making the two languages more comparable when it comes to concurrent programming. Let me know what you think of my comparison on Discuss or Twitter.
How Eio Makes Concurrent Code Quick and Easy
Rust is a popular programming language that solves a lot of problems for programmers. One of Rust’s strengths is the way it delivers concurrent code in a quick and safe manner.
According to a recent blog post, “Rust solves problems that C/C++ developers have been struggling with for a long time: memory errors and concurrent programming. This is seen as its main benefit.”
Eio makes writing concurrent code in OCaml much easier, resolving earlier pain points and providing significant benefits. OCaml is also a type- and memory-safe language with a low-latency and high-throughput concurrent garbage collector that doesn't get in the way of application code execution.
Below is an overview of the biggest changes Eio brings to concurrent programming in OCaml. With these improvements, OCaml provides a concurrent programming experience comparable to Rust.
Key Benefits of Eio
Performance
Eio brings big performance improvements to concurrent code in OCaml, making use cases like web servers serving requests from users a lot faster. In a speed test comparing Eio’s performance to Go’s net/http
and Rust’s hyper
, the results show that Eio outperforms Go and closely matches Rust. Eio can reliably serve over one million requests per second on a few cores. Being such a close match in terms of performance, OCaml is a strong contender for users looking to expand beyond using Rust.
Ease-of-Use One of Rust’s strengths is its user friendliness, meaning it’s easy to use and code in. With the OCaml 5 update, Eio makes OCaml a much easier language for writing concurrent code.
Eio offers an alternative to monadic I/O, which used to be the only way to write concurrent code in OCaml. Now, with OCaml 5, the developer experience is greatly simplified and will feel familiar to anyone who knows OCaml.
Patrick Ferris, a developer at Tarides, emphasises that with Eio:
“Normal OCaml features just work out of the box, like exception backtraces, which are crucial for writing new libraries, using tools, and debugging programs.”
Being able to use backtraces is a big improvement, as they can show the developer a form of ‘history’ of what interventions have been made to a program. Using it in Eio lets developers debug and troubleshoot more quickly. Having access to standard OCaml features also means that the more difficult parts of concurrent programming, such as cancellations, cleaning up resources, reporting errors, and testing are a lot easier to perform with Eio.
The biggest change users will notice is the resolution of the ‘code colouring problem.’ In the past, synchronous code could not exist alongside asynchronous code without breaking, requiring the developer to use a special calling convention to invoke asynchronous code. With Eio, that is no longer a problem, as both just appear as normal OCaml functions. This significantly improves developer experience and productivity and is unique to Eio. Currently, in Rust’s I/O library the ‘code colouring problem’ still exists, and developers have to spend time resolving conflicts between code types.
Portability Both Rust and OCaml offer excellent portability, and with Eio OCaml gets several quality-of-life updates that lets developers create programs in different environments with several different features.
Operating systems have changed a lot in the last decade, benefiting from continuous development and modernisation. Thanks to its flexibility, Eio is able to take advantage of modern OS features (such as Linux’s io_uring to boost its own performance.
In turn, different backends for various platforms (such as Linux, MacOS, Windows, Mirage, etc.) can also implement the standard environment Eio expects to run programs. This adds an element of predictability to developer workflow, minimising the amount of task-switching and time spent outside of programming.
On the topic of Eio’s flexibility, Thomas Leonard, the creator and lead maintainer of Eio, highlights that:
“Eio can also run existing Lwt and Async code alongside new code, allowing existing projects to be upgraded piece by piece, keeping the tests passing throughout the migration. A couple of lines of code is all it takes to make an existing Lwt application run on Eio, and from there any new code can use Eio directly.”
Instead of having to pick one I/O tool to learn, Eio’s library can run all three in the same program which is completely new.
Security Both Rust and OCaml offer strong safety features. According to the blog Codilime, “High performance and safety are the features that made Rust so appealing.” Well, Eio adds even more security features to OCaml’s already long list.
Eio allows developers to implement measures with great specificity, which has great significance when it comes to security. For example, Eio lets a developer program a web server to serve files only from within specified directory trees, removing the possibility that it could be tricked into serving other files. This ensures that the I/O only shares what it’s intended to do without being bypassed, which is a common security problem with web servers.
Conclusion
Many businesses and programmers love Rust, and for good reason! What OCaml 5 and Eio can offer is an alternative that matches Rust on performance and user friendliness, includes new cutting-edge features, and delivers on safety in a way that is uniquely OCaml. With its new Eio library, concurrent programming in OCaml becomes more similar to Rust, and out of the two only OCaml solves the function colouring problem. If you’re looking to complement your use of Rust with a robust functional programming language – without sacrificing performance – OCaml 5 is the language for you.
Contact us and learn more about how OCaml can transform your business. You can also find us on GitHub, the OCaml Discuss forum, and Twitter
Acknowledgements
With thanks to Thomas Leonard, creator and lead maintainer of Eio, and Patrick Ferris, a developer at Tarides, for their expertise and input that made this article possible.
Sources
Open-Source Development
Tarides champions open-source development. We create and maintain key features of the OCaml language in collaboration with the OCaml community. To learn more about how you can support our open-source work, discover our page on GitHub.
Stay Updated on OCaml and MirageOS!
Subscribe to our mailing list to receive the latest news from Tarides.
By signing up, you agree to receive emails from Tarides. You can unsubscribe at any time.