Vale has an ambitious goal: to be fast, memory safe, and most importantly, easy. There are a lot of stellar languages that have two, and we suspect it's possible to really maximize all three.
To do this, we're harnessing a new concept called regions.
In Part 1 we saw how we can use pure functions to easily immutably borrow data to make it faster to access.
Part 2 showed us how we could more precisely create regions via isolates, and immutably borrow them too.
Part 3 showed us how we can get the benefit of isolates with many more kinds of data.
Let's kick it up a notch, and use regions to immutably borrow part of an object while being able to modify the rest of it.
This pattern is incredibly versatile, and helps us eliminate memory safety overhead for iterating over collections, accessing private data, and even entire architectures such as entity-component-system.
Later on, we'll show how to use this for arrays, hash maps, and larger data structures.
First, let's see how we can use regions to make zero-cost iteration of a linked list.
Here's a singly-linked list of Ships.
Here we iterate over it. There's a much cleaner way to do this, but we'll be verbose here for clarity.
Iterating over this list incurs a few generation checks:
Generation checks usually aren't a significant source of overhead, for various reasons. 3 But if we want to squeeze every ounce of performance out of this part of the program, and the profiler tells us that this area of the code is worth optimizing, we can bring out our region skills to get the job done.
The first question to ask is: which parts of my data shouldn't change right now?
The data in the contained Ship is changing, when we do set cur.hp -= 5.
The ShipListNodes themselves don't seem to be changing though. Perhaps we can put them in a region?
But... the ShipListNode contains a Ship inline. Can we have a struct in one region contain a struct in another one?
Yes we can!
Here are those same structs, but now ShipListNode has some region markers:
Note the ship a'Ship. The a' here means that this data, even though it's inline, is still part of another region.
Here, we put the list into an isolate with '. We specify self' for the Ships to tell the compiler that they're in main's region.
head is of type '?^ShipListNode<main'>.
And now, we borrow it immutably, using .imm. This makes maybe_cur and cur both immutable, which eliminates the generation checks from:
There are still a couple generation checks: ship.hp and ship.name.
In this example, the compiler actually eliminates these too with static analysis, because it knows they are owned by a region that's currently immutable.
This is pretty common; a region's immutability often helps optimize things around it.
If anything isn't clear, feel free to reach out via discord, twitter, or the subreddit! We love answering questions, and it helps us know how to improve our explanations.
We're aiming to complete regions by early 2024, check out the roadmap for more details.
?X means "Option
A couple reasons:
If we made the above list into a generic struct, it would look like this.
It looks like an ordinary generic struct; there's not even any region markers.
That's because in Vale, T actually includes three things:
When someone says ListNode<&myiso'Ship>, T is: non-owning (&) reference to a Ship from region myiso.
If T is a x'Ship, that means ListNode owns data in another region, just like we saw with ShipListNode.
So really, any generic struct might own data in another region.
Every array, list, hash map, and other generic container in Vale is using multi-region data under the hood.
This is incredibly powerful, because it lets us freeze the container while accessing the contained data, such as we saw in the above ShipListNode, and makes our entire program much faster. 4
Between pure functions, isolates, and multi-region objects, we can eliminate the vast majority of memory safety overhead for our programs.
The best thing about all of these mechanisms is that they are opt-in:
This is consistent with Vale's philosophy of avoiding forced complexity.
Next up is Part 5, where we talk about how we can make iteration much faster, and how to use regions to make entire architectures (such as entity-component-system) zero-cost.
That's all for now! We hope you enjoyed this article. Stay tuned for the next article, which shows how one-way isolation works.
If you're impressed with our track record and believe in the direction we're heading, please consider sponsoring us on GitHub!
With your support, we can bring regions to programmers worldwide.
See you next time!
- Evan Ovadia
Draft TODO: estimate how many checks are eliminated just from the stdlib doing this
Vale aims to bring a new way of programming into the world that offers speed, safety, and ease of use.
The world needs something like this! Currently, most programming language work is in:
These are useful, but there is a vast field of possibilities in between, waiting to be explored!
Our aim is to explore that space, discover what it has to offer, and make speed and safety easier than ever before.
In this quest, we've discovered and implemented a lot of new techniques:
These techniques have also opened up some new emergent possibilities, which we hope to implement:
We also gain a lot of inspiration from other languages, and are finding new ways to combine their techniques:
...plus a lot more interesting ideas to explore!
The Vale programming language is a novel combination of ideas from the research world and original innovations. Our goal is to publish our techniques, even the ones that couldn't fit in Vale, so that the world as a whole can benefit from our work here, not just those who use Vale.
Our medium-term goals:
We aim to publish articles biweekly on all of these topics, and create and inspire the next generation of fast, safe, and easy programming languages.
If you want to support our work, please consider sponsoring us on GitHub!
With enough sponsorship, we can: