r/ProgrammingLanguages • u/rexpup • 4d 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?
17
u/functionallycorrect 4d 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
1
u/rexpup 4d ago
Oh, and to answer anyone who asks "why compile to Wasm if it's hosted in a Rust application"?
5
u/Inconstant_Moo 🧿 Pipefish 4d 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
pluginlibrary for interop.)Since the point of this was to make theft easy, I steal. For example, my
stringsstandard library starts like this. (Lines beginning with~~are docstrings.)5
u/PhosXD 4d ago
I try to keep as little as possible "lowered into the runtime" as you say, only the essentials. The rest, like convenient methods for arrays, strings, etc, get written in the language itself
4
u/Key_Detail15 4d 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 3d 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.
3
u/_th3oth3rjak3_ 4d 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.
3
u/Bob_Dieter 2d 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.
2
u/mamcx 4d 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
composeefficient operations together so the script language wast minimal time, one good example is :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 likeFrom here, in the case of
ser deyou can do the Visitor on the host, and only plug theToJsonparts (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 4d 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 4d 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 3d 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 2d 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
u/xpusostomos 2d ago
Define interpreted, with JIT runtimes they can be as fast as precompiled
-2
u/AnArmoredPony 3d ago
I mean clod gonna write it in Rust better than in a brand new language so native