Elide 1.0.0-alpha10 Help

Polyglot 101

Traditionally, software is written in just one language at a time: there are JavaScript apps, and Python scripts, and all sorts of other things developers make.

Elide offers a new way to think about software: in multiple languages at once (hence, "polyglot!"). But what does that mean? This guide explains how multiple languages can work in tandem to accomplish amazing things.

Starting Imperatively

Let's start with a simple imperative example of how multiple languages might work together. We could begin with some JavaScript:

export function sayHello(name) { console.log(`Hello, ${name}!`); }

We've defined a function here, which takes a name parameter; we format that and print it. Easy. Now let's imagine our magic runtime can shift to Python to call that code:

from "./my-js.mjs" import sayHello as say_hello def call_hello(): """Call the `sayHello` function in JavaScript, from Python.""" say_hello("Elide")

This is pretty easy to follow so far. We've defined that sayHello function in JavaScript, and now we are calling that same function from a new function, called callHello, in Python.

But why?

Have you ever wanted to write a Python app which could host a React UI? How about an application that's built in Ruby; maybe you want to do some image processing and you miss your PIL?

There are tons of cases where one language is good at something, another is good at something else, and your app has to do both. Elide is what bridges this gap.

Isn't that inefficient?

That depends! Elide certainly behaves differently from, say, Node, but Elide handily beats Node and even Bun on several key benchmarks. Just like Bun vs. Node, or CPython vs. PyPy, performance isn't necessarily apples-to-apples.

But, since you may be wondering, in the code sample above 👆

  • There is just one process. No, Elide isn't calling out to Node with your source code, and then to Python, and stitching between with JSON or something. That would be gross. There are systems that do this; Elide is not one of them.

  • There is just one governing interpreter. Elide uses language implementations from the GraalVM team, built on top of Truffle. Instead of two interpreters (Python and JavaScript, in this case), there's just one, but it speaks both languages.

  • There is no serialization going on. The name value doesn't need to be transformed as it crosses a border between Python and JavaScript. It's just a string; both languages can use it as they would normally.

Skeptical? Good. The best engineers often are. Let's beat the serialization allegations:

export function sayHello(nameGetter) { console.log(`Hello, ${nameGetter()}!`); }

And:

from "./my-js.mjs" import sayHelloWithGetter as say_hello def get_name(): return "Elide" def call_hello_look_ma_try_this_with_serialization(): say_hello(get_name)

Elide will happily run this code. This is impossible with JSON, or Protobuf, or any other trickery on top of Node.js or CPython.

When people see a demo like this, the "polyglot" concept immediately begins to make sense. That's great, but how do 2+ languages interact in one context?

Let's do some exercises

Now that we are thinking in a roughly-polyglot way, let's test the waters with some basics: many applications have to read environment variables or write to a file. You can follow the guides below or skip ahead.

Exercise: Environment Variables

Read env variables and use Elide's host policy to restrict host env access

Exercise: Filesystem Basics

Read a file using Node.js built-ins (node:fs).

Benchmarking

Elide makes use of language runtimes from the GraalVM team which are benchmarked extensively and continuously. People often ask if they can replicate these benchmarks. The answer is yes, you are encouraged to!

There are some tools we can recommend:

Tool

Great for

Example invocation

hyperfine

CLI-invocable micro-benchmarks

Using Hyperfine for benchmarks

wrk2

Server benchmarking

Using wrk2 to benchmark Elide's server

Benchmarking with Hyperfine

Coming soon.

Benchmarking with wrk2

Coming soon.

Last modified: 15 May 2024