Embedding Scheme in Rust
14 comments
·January 8, 2025anonzzzies
mrkeen
I did something similar. C++ game engine with embedded Lua. I brought it to a C++ job interview. The interviewer kept asking how certain things worked. After a while I realised I was only clicking into Lua source code to show what was going on.
He said something like, "You know you can just write C++, right?"
Did not get the job :)
Permik
Hopefully the interviewer did not cut you out because of the project, because there's many valid reasons why someone would use Lua: - "no need to use C++ for non-performance critical sections of job." - "The nature of the game does not require high levels of optimizations to implement the game" - "This is just the MVP, lua lets me iterate more quickly" (totally lying, you're totally going to ship the game as is)
i.e. "why many word when few do job"
paines
Intresting. I know of two projects from the past where people used Gambit and Chicken and couldn't recommend it, because of the garbage collection. So the games runs, bullets fly, monsters move, etc... and at some point gc kicks in and you would notice a minimal halt or lag which for games is a big NO NO. Now that you mentioned a shmup, where a lot I going on, I am wondering how you or Chez handled that?
shakna
Chez's GC is a lot more configurable, and the default is smarter, than either Gambit or Chicken. Chez has a generational garbage collector, rather than the more simple ones. It can also run in parallel mode, to prevent it being "stop the world" - it'll run in another thread instead.
Because the collections are smaller, and you can slightly offload them into another thread, it's a lot less noticeable in usage.
stefanka
How does this compare to steel? https://github.com/mattwparas/steel
raviqqe
Stak aims to add a scripting environment in Rust with minimal overhead (e.g. additional bin size) but a slight sacrifice of speed. For example, its VM is written in ~1500 lines in Rust including some tests (https://github.com/raviqqe/stak/tree/f799140377162f1c2f94cc4....) The philosophy is similar to SectorLisp (https://github.com/jart/sectorlisp) but for R7RS :)
stefanka
Interesting, thanks. Do you plan speed/performance comparisons as a scripting / pluign language? That would be relevant for games or graphics intense usecases—blackjack-rs considered scheme/lisp but opted for mlua due to performance)
raviqqe
Here are some benchmarks: https://github.com/raviqqe/stak/actions/runs/12654408322/job...
For pure computational work, Stak is 2 to 2.5 slower than CPython. These are E2E benchmarks for the interpreter commands. As you said, I should probably add benchmarks of scripting use cases with Lua.
null
oldpersonintx
[dead]
null
unit149
[dead]
Long ago, I decided to embed a scheme in C++ when I was building a game (nothing AAA or fancy; just a RPG mixed with shmup I wanted to make since I was a kid); when I succeeded, I noticed I never touched the C++ again. So then wondered why I used C++ in the first place and went full for Chezscheme, which is very fast anyway and fast enough for most things. It is fun though. I found the Python case a better fit; it was (is) so slow that actually it makes sense to implement critical path code in C/++.