Rhai: An embedded scripting language for Rust
15 comments
·January 17, 2025satvikpendem
When I last read about Rhai it was apparently very slow such that it was simply faster and more ergonomic writing scripts in Rust itself, has that changed?
xnacly
According to the documentation it evaluates by walking the ast, so yes, this is considered very slow. The readme also mentions 1 million loop iterations in 0.14s (140ms). Even my unoptimised naive lips like language [1] (also implemented via a ast walker) does the same in 28.67ms - so yes id consider this pretty slow.
MarceColl
Not sure when you read this, but I can tell you that two years ago it was VERY slow. I used it for a game and I had to redo it in lua some months later because it was a big bottleneck. I don't have more up to date information.
Black616Angel
What does "Passes Miri." mean? There is no link and online I only find a Malaysian city with that name.
Deukhoofd
Miri is a Rust tool with a similar function to Valgrind, it checks for undefined behavior.
chubs
Very impressive. I read the readme and I’m unsure how memory management works, is it GC? And is it OOP or not? Thanks :)
kreetx
Only guessing, but since the language is for scripting then maybe all garbage could be collected when the script finishes?
tommiegannert
I was also curious. Looking at the code, it seems values are Boxed, but there's a special type called Shared that is an Rc-RefCell (unless Send-enabled.):
// Also handle case where target is a `Dynamic` shared value
// (returned by a variable resolver, for example)
Couldn't find any other information about a GC, so guessing this is pure ref-counting. Speaking of potential memory leaks, there's also string interning happening. I agree this seems to be for short-lived contexts right now.
oulipo
Cool! But wouldn't the way to go for scripting in a language these days to just compile to WASM and run in an embedded micro-VM? Why hasn't Rhai made this choice?
Epicism
That’s very cool! I appreciate how you can simplify interacting with rust applications without rust programming skills.
ilrwbwrkhv
This is awesome. Would this be a replacement for Lua use cases in a Rust desktop app?
searealist
Needs a comparison to the excellent Rust Starlark Implementation.
BiteCode_dev
Starlark is for config, not scripting.
searealist
I'm not sure what you think the difference is.
Here's a real-world example of using Rhai:
https://glicol.org/tour#meta1
I've embedded Rhai in Glicol to support sample-level audio synthesis, and the performance is [acceptable]. Real-time audio is very computationally intensive, after all.
I've tried various embedded scripting languages in Rust, but Rhai probably has the best documentation and playground.
For reference, see this survey:
https://www.boringcactus.com/2020/09/16/survey-of-rust-embed...
Recent Koto Lang also looks promising:
https://koto.dev/