r/ProgrammingLanguages • u/rexpup • 9d ago
Discussion For interpreted languages, how much of your standard library is written in your language vs. lowered into the runtime?
I'm working on a self-hosted language that compiles to Wasm. It runs inside of a Rust project.
Part of my standard library is serialization and deserialization into common forms like json, toml, ron that other systems already use - it's how I'm interoperating with other services. So far, I've been writing these in the language itself, and it was decently fast.
Then I encountered the actually-deployed code of other services that often produced out-of-spec stuff. So I go back and write handling for all these edge cases and stuff, and suddenly it's a lot slower. I check out what's taking so long and it's all the edge cases.
So the language has FFI into the Rust runtime for certain things that it has to have. I'm thinking of having the library functions just call out to Rust code that does the heavy lifting. The interface adds a little lag, sure, but if I do the entire parsing into objects at the rust level, I think it'll be faster.
When is it worth it? The Rust application takes ages to compile, which is why my language project exists in the first place. I want to be able to write business logic and swap it out really quickly. But I don't think I'm going to be revising the library functions very frequently.
What do you all do?
1
u/rexpup 9d ago
Oh, and to answer anyone who asks "why compile to Wasm if it's hosted in a Rust application"?
wasmtime is already built in Rust, and it's what I'm using to run services in this, as well as embed it in the Rust application. Wasmtime already has some stuff that (along with some stuff I wrote) allows me to hotswap functions while the application is live, so I can sort of live code. I'm trying to get other people on board with this but we'll see. Whole project might be trashed, who knows.
I'm working on a way to run the same code on client browsers. We already keep a decent amount of the user's data on the client so that the views can predicatively show what user actions do and get rolled back on correction from the server, a lot like a multiplayer game (in fact, the microservice groups users by "lobbies" which I just read up on how game services do lobbies to handle how we balance loads). So if users can see the results of their actions without a server round trip that really reduces how laggy the application "feels"
1
u/paul_h 9d ago
I’ve a Rexx that runs on top of JavaScript and can script into iframes from outside the frame (postMessage) it’s quite neat and making lots of npmjs available means I have to worry a little about what can’t run in the browser as it’s designed for command line. Rexx is an unsophisticated 1979 language that peaked in 1989 on the Amiga as ARexx. Some would argue it peaked on as/400 and s/390 machines where it still runs today.
5
u/Inconstant_Moo 🧿 Pipefish 9d ago
My language runs on a Go VM, and it's been written to have excellent interop with Go because that way I can piggyback on their ecosystem the way Clojure and Kotlin do with Java and Gleam and Elixir do with Erlang. (Except that Go compiles to native so instead of targeting someone else's VM I wrote my own and use the notorious plugin library for interop.)
Since the point of this was to make theft easy, I steal. For example, my strings standard library starts like this. (Lines beginning with ~~ are docstrings.)
~~ The `strings` library supplies functions for handling the built-in `string` type.
import
golang "strings"
golang "errors"
def
~~ `compare(a, b)` returns an integer comparing two strings lexicographically. The
~~ result is `0` if `a == b`, `-1` if `a < b`, and `+1` if `a > b`.
compare(a, b string) -> int : golang {
return strings.Compare(a, b)
}
~~ `contains(s, substr)` reports whether `substr` is contained within `s`.
contains(s, substr string) -> bool : golang {
return strings.Contains(s, substr)
}
// ... etc.1
u/anaseto 9d ago
Making it easy to get functionality from Go's stdlib is very nice, yeah, I use it a lot for my array language, too (my next version is going to have channels, networking and http stuff based on Go's). I don't use
plugin, though, as it's not portable: I go with extended interpreters (I have a somewhat standard way of writing those in a few lines).1
u/Inconstant_Moo 🧿 Pipefish 9d ago
plugindoesn't work on Windows but there are magics one can do with WASM, all the stdlibs work there. And I've written an IDE which you can use in your browser. And I figure pretty much the only reason anyone would want to write Pipefish on a Windows machine is educational, for which a browser implementation is fine. For any serious purposes you'd use a LAMP stack where the P stands for Pipefish.1
u/anaseto 8d ago
I'm on OpenBSD, which isn't supported by
plugineither, afaik. Didn't know about those wasm magics (I mostly use my language for scripting, so I never really looked into wasm stuff much, as it's typically only used occasionally for linking to a short example).1
u/Inconstant_Moo 🧿 Pipefish 4d ago
According to Google there is a way to use the plugins on OpenBSD using still more WASM magicks and a bunch of words I don't fully understand. Presumably in order for it to be production-grade fast this wouldn't work 'cos the point of the Go is that it compiles to native, but it would be usable for someone who just wanted to mess about with it.
1
u/titaniumalt 9d ago
I go with extended interpreters (I have a somewhat standard way of writing those in a few lines).
how, exactly?
2
u/anaseto 8d ago
This is, for example, the "full" version of the interpreter that includes all standard extensions. I currently propose 3 different build tags with selections for conveniency, but the idea is that anyone can select which extensions they want and add their own just by writing a single short file like that, and get an interpreter that works the same way.
5
u/Bob_Dieter 8d ago
From my experience observing the behavior of Julia and other dynamic languages, the heuristic goes like this:
If your language is developed enough to be pleasant to work in (scope of features, type system, etc)
AND
If it is capable of producing code with native performance (native in this case being "as fast as you can go with WASM).
... then you will see some benefits from implementing stuff in the language itself, like better integration with the type system, stack traces and a lower barrier for people trying to understand the implementation.
4
u/Key_Detail15 9d ago
In my case, I have very fast compilation of the interpreter (about 0.1s) so that is not the deciding factor.
I just have a mix: some stuff suits being built-to to the interpreter, other stuff is in the interpreted language as it uses its higher-level features and dynamic typing. (The implementation language is at about the level of C.)
(My very first interpreted language was an add-on scripting language for my applications. There the motivations were different: putting half the functionality into multiple 'hot-loaded' modules meant less pressure on memory from the main app.
I could conveniently develop and test modules from within the running application. I also had users so that, if there was bug in a scripting module, they could more easily be sent an updated module.)
1
u/WittyStick 9d ago
If your runtime provides sufficient access to the hardware resources, you shouldn't need to provide features in the runtime. The trouble is you've chosen WASM as your runtime, which is limited because it's a lowest-common-denominator.
So eg, if your runtime only provides 128-bit SIMD vectors, but your hardware provides 256-bit or 512-bit SIMD vectors, you're going to want to implement a lot of things in the runtime to take advantage of the hardware.
If you had a runtime that provided 256-bit or 512-bit SIMD vectors to the language, you could implement most of that as libraries in your language.
1
u/rexpup 9d ago
The trouble is you've chosen WASM as your runtime
That was the one non-negotiable constraint. Well, that or Javascript but I definitely wasn't going do that.
1
u/WittyStick 9d ago
Why WASM, if it isn't your intention to run it in a web browser, but your own application?
1
u/rexpup 9d ago
It also will run in clients' browsers. When we determine something has user-level permissions, we try to run it on that user's data in that user's browser. It makes for fewer round-trips and feels a lot snappier to users. But users can also automate actions and create cron jobs, so we need the exact same logic to run on the server (ideally without a rewrite).
3
u/_th3oth3rjak3_ 9d ago
Ah this is a good question. I got to a point like this and got stuck. That language ended up in the graveyard and then I started another one. lol.
I hope you find your answer! Btw, how do you guys actually find motivation to keep going? I get through parsing and then I start losing motivation because type checking.
2
u/mamcx 9d ago
Exist many ways to solve this, none is particularly simple but there are some I think.
First, you should think in terms of how compose efficient operations together so the script language wast minimal time, one good example is :
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
// This is costly on the interpreter:
let mut sum = 0;
for number in numbers {
sum += number;
}
// but this can be done fast on the host
let sum: i32 = numbers.iter().sum();
println!("Sum: {}", sum);
}This is your first layer.
Your second is how allow to "swap" host impl vs interpreter impl, lets say you wanna to_json, so you can do something like
trait ToJson
fn to_json(self):Json
impl ToJson Rust<FFI>
impl ToJson InterpreterFrom here, in the case of ser de you can do the Visitor on the host, and only plug the ToJson parts (native | interpreter).
I think a interpreter should be able to do whatever but be able to hotswap to a internal more efficient things (like in python pandas) is very valuable.
2
u/slaymaker1907 9d ago
Currently most of it is in the runtime as it's pretty minimal. It's also tricky with an interpreter as performance will be much worse if you write it in your language.
2
u/Prior-Perspective-61 9d ago
My language is not interpreted, but I do not use FFI either (except malloc and free), on a codegen step I directly generate target language AST for intrinsic functions in code (C in my case), and it is super fun IMO
Also my language AST is simple, so even operators like +, -, * are parsed as a chain of functions (opAdd (opMul 2 3) 4) which are intrinsic (like int opAdd(int a, int b) { return a + b; } and inlined 😅
2
u/koflerdavid 9d ago
It doesn't really matter, unless it does. If writing code in your language provides particular benefits (nice type system, verifiability, maintainability) then there might be benefits in writing library code in that language. It also helps developers understand how your runtime works without having to switch languages just to read code.
The host language on the other hand might provide all you need regarding a standard library; you just have to write bindings for it or expose it via FFI, assuming your FFI is easy to use. Writing a standard library from scratch is a huge effort that has to be justified, for v example for learning purposes.
Another matter to consider is performance. You don't necessarily know how developers will use the library functions. FFI is usually not for free, but your language might not be particularly performant either. So you have to find a good tradeoff there. For example you might want to write a sorting algorithm with custom comparators in the language so you don't have to do FFI O(n*log(n)) times!
2
u/pauseless 8d ago
My Go-based interpreted language that compiles to WASM uses absolutely every go library it can for the task at hand. The wasm restriction means that the core has to be a bit disciplined, but when making an executable to run on the command line, everything in the Go standard library can be made available. I just only write the bindings as and when I need them. I have tests to ensure I don’t blow up the wasm build by accidentally introducing the wrong dependency.
Another interpreted language, is made to run across JS runtimes, CLR and JVM. Everything that can be shared is in the language. That layer then calls in to whichever interop layer as needs be. I have absolutely zero desire to maintain multiple versions of identical code.
1
1
u/Public_Grade_2145 4d ago edited 4d ago
I'm working on a self-hosting Scheme compiler and a tree-walk Scheme interpreter. Following the Lisp tradition, I prefer writing libraries (including the compiler) in the same language unless it is more convenient to write them in other languages. For example, GC and runtimes are more convenient to write in C.
A more motivating example: bignum library.
Chez Scheme implements bignums in C, whereas Goeteia implements bignums in Scheme.
https://github.com/guenchi/Goeteia
I was inspired by Goeteia to implement bignums in Scheme as well.
Another example: peripheral library on MCU.
The most obvious and convenient approach is to make use of the official SDK.
For example, see: https://github.com/taimoon/scheme-interpreter/blob/main/HACKING.md#baremetal-example-rp2040
A less convenient approach is to actually read the datasheet and write the driver instead.
For example, see: https://github.com/taimoon/scheme-interpreter/blob/main/examples/rp2040/uart.scm
Pleasant to write the same language is good, but FFIs is also equally good.
-2
u/AnArmoredPony 9d ago
I mean clod gonna write it in Rust better than in a brand new language so native
17
u/functionallycorrect 9d ago
I’d have to see more of what your language is about. I personally like using FFI a lot in my project for almost everything. 90% of my stdlib is native calls