r/ProgrammingLanguages • • 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?

24 Upvotes

49 comments sorted by

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

4

u/rexpup 4d ago

I get that, but it does feel like cheating, worse than LLMs. If I get a function call and just turn around and call the library function of my host language, what am I even building a language for?

8

u/Inconstant_Moo 🧿 Pipefish 3d ago edited 3d ago

I get that, but it does feel like cheating, worse than LLMs. If I get a function call and just turn around and call the library function of my host language, what am I even building a language for?

Ideally, you're building it for other people, not for the dubious satisfaction of rolling your own time library to replace one that already works.

A programming language is a UI over machine code. The point of the language is to be a good UI that makes things easier than writing machine code. (If it makes things harder, it's an esolang.) The point of a new language is that you've realized that there are certain common programming tasks that could be done easier in a language with different affordances. That's the only point. The user doesn't care about anything else. There are no bonus points, except in your head, for hand-rolling your standard libraries (or I might add for self-hosting).

To the extent that how you produce your language matters, obviously the arguments are all on the side of re-using code that has already been used in a zillion transactions per second over the past couple of decades because we can be pretty sure that it works and the user does care about that. The fact that pretty much all my standard libraries are thin wrappers over Go's standard libraries didn't just save me a year's development time and make everything go faster, it is also a guarantee of quality that a solo dev who rolls their own libraries cannot possibly make.

The highest form of software engineering is to solve your problems without writing any code.

1

u/rexpup 3d ago

Thanks, really thoughtful response

3

u/Inconstant_Moo 🧿 Pipefish 3d ago edited 3d ago

Sure thing.

Looking at your OP again, I see that you have a very well-defined use-case of being able to swap out business logic within a Rust app. If you're wondering whether it's really ok to achieve this goal by a method which saves you time and will run faster and will be more bug-free and will have fewer security holes, then the answer is "yes". There are literally no downsides.

If you'd like other people to use your language for other use-cases you should in fact go further and do what I did for Go, only with Rust, i.e. supply a way for people to wrap arbitrary Rust libraries in your language. Its relationship with its host language isn't a weakness, it's a selling point.

3

u/Prior-Perspective-61 4d ago

In that case every language is cheating, like when you implement a + b, you use an assembly operation for adding instead of implementing your own 😁

6

u/rexpup 4d ago

You joke, but there are languages like MUMPS that don't use the computer's assembly ADD instruction. MUMPS stores all numbers as string sequences of digits and has procedures to add them bitwise. Not a fast language.

2

u/Prior-Perspective-61 4d ago

Not fast, but funny!

1

u/Inconstant_Moo 🧿 Pipefish 3d ago

I've never been able to figure out why with so many diseases to choose from, they named it after one that's fatal in fewer than 0.1% of cases.

2

u/blackasthesky 3d ago

I mean the contribution then has to be in the language and its syntax and semantics. Imo that's where the actual value should be.

1

u/rexpup 3d ago

The value I'm trying to provide is faster iteration. There's literally nothing innovative about my syntax, semantics, paradigm, or data model. A big pain point we've found in Rust is how long it takes to compile. We were formerly a Ruby on Rails shop that was paying through the nose for hosting, and we increased the request throughput 100x by switching to an Axum/Tokio Rust app. But now we have to provide a million little knobs, dials, and switches for each customer config, or else hard code differences into the app.

I think we hit Greenspun's Tenth Rule, which is that tongue in cheek saying "any complex business application's configuration starts to re-implement common lisp". Configuration was way too complex, so we started writing individual functions that only one customer used. So I was like, "if we're doing a crappy config language anyway, why not make a good config language?"

The application already works on user-based actions and replay-based reconciliation, so we already have a really solid model for user permissions and optimistic offline edits that get validated by the server and reordered to reconcile document state. So I thought "why not just embed a scripting language that lets one button do a series of 100 user actions to set config x? Then we can just write little snippets for each customer without having to do the 45-minute rebuild just to try a change on dev." There's hardly any danger because the only FFI the scripts can do are things a user can do at their permissions level anyway one button at a time. We don't have public users, just enterprise, so I'm not worried about malicious actors, and there's a rate limit.

0

u/kwan_e tonal-lang 4d ago

I get that, but it does feel like cheating, worse than LLMs.

That's ridiculous. The whole point of writing libraries is for reuse. The whole point of open source is reuse. If a library's licence, chosen by the author(s) themselves, gives rights for reuse, then that is not cheating. That is precisely what it's for.

LLMs ignore licences. We know the companies acquire the data unethically. That is not the same as permitted reuse as the whole point libraries to begin with.

Get some historical context, geezus.

1

u/rexpup 4d ago

Get some historical context, geezus.

What a bizarrely hostile and rudely-worded comment. Please do not comment on my posts again.

1

u/MCWizardYT 2h ago

please do not comment on my posts again

No offense, but saying this doesn't help anything. All you can do is stop replying to them and block them

-3

u/kwan_e tonal-lang 4d ago

Get fucked mate.

You literally said calling out to external libraries "worse than LLMs".

No it fucking isn't.

The hostility towards your comment was well-deserved, and now your refusal to acknowledge the absolute stupidity of your statement, discredits you.

-1

u/rexpup 3d ago

Your repeated unwarranted hostility is not kind

3

u/kwan_e tonal-lang 3d ago

Wow, I didn't expect people on this sub to support his moronic statement.

1

u/jcastroarnaud 3d ago

Correct point, rude delivery. Please chill out, or redirect your anger to something constructive.

1

u/kwan_e tonal-lang 3d ago

What exactly was rude about this specific comment?

0

u/nzconstructionlawyer 3d ago

LLMs aren't derived works so they don't need licenses.

7

u/kwan_e tonal-lang 3d ago

They can definitely generate code that is clearly derivative of existing works.

That governments around the world seem willing to ditch copyright law in the face of AI, and that most open source projects aren't resourced enough to challenge, is a different issue.

If it were Disney's works being generated by AI, they would be singing a different tune.

-1

u/nzconstructionlawyer 3d ago

I can copy code that is derivative of existing works and I can write new code. That doesn't make me a derivative work.

1

u/kwan_e tonal-lang 3d ago

The courts don't care.* If your code looks like derivative code, they don't care what you claim was the development process.

* Well, they may not even care about copyright anymore, with all the lobbying from AI companies.

2

u/yorickpeterse Inko 3d ago

This thread isn't going anywhere, all of you need to go outside and touch some grass.

-2

u/nzconstructionlawyer 3d ago edited 3d ago

Are you being stupid on purpose? Seriously? That's your response to my comment?

What I am saying, if I have to spell it out word for word, is that just because LLMs can produce derivative works does not mean that they are derivative works.

I can produce derivative works and I can produce original works. If I produce derivative works and sell them, that's copyright infringement. If I don't, it's not.

If you use LLMs to produce derivative works and sell them, that's copyright infringement. If you use them to produce original works, then you're fine.

But LLMs themselves are not derivative works of their training data just like a human being is not a derivative work of his training data.

EDIT: Ah yes another loser commenter that blocks me as soon as he replies so I can't even see his full reply.

4

u/kwan_e tonal-lang 3d ago edited 3d ago

But LLMs themselves are not derivative works

I never said they were, and the comment I replied to did not rely on them being that either. At least I didn't interpret it that way.

So you are arguing something that I did NOT argue. You're the one being stupid on purpose. The only you way you thought to argue against me was to make up an argument I did not make.

Piss off.

* Turns out it's just a 5 month old troll account.

1

u/rexpup 4d ago

Oh, and to answer anyone who asks "why compile to Wasm if it's hosted in a Rust application"?

  1. 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.
  2. 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 4d 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 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 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 3d 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 3d ago

plugin doesn'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 3d ago

I'm on OpenBSD, which isn't supported by plugin either, 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/titaniumalt 3d ago

I go with extended interpreters (I have a somewhat standard way of writing those in a few lines).

how, exactly?

2

u/anaseto 3d 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/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.

1

u/rexpup 3d 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 3d ago

Why WASM, if it isn't your intention to run it in a web browser, but your own application?

1

u/rexpup 3d 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_ 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 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 Interpreter

From 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 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