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.
struct ShipListNode {
ship Ship;
next priv vary ?^ShipListNode; 2
}
struct Ship {
name str;
hp int;
}
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:
exported func main() {
head =
Some(
^ShipListNode(
Ship("Serenity", 10),
Some(
^ShipListNode(
Ship("Raza", 22),
None))));
maybe_cur = head;
while maybe_cur.NonEmpty() {
cur = maybe_cur.Expect();
ship = cur.ship;
set cur.hp -= 5;
println("Damaged {cur.name}!");
maybe_cur = cur.next;
}
}
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.
struct ShipListNode<a'> {
ship a'Ship;
next priv vary ?^ShipListNode<a'>;
}
struct Ship {
name str;
hp int;
}
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.
exported func main() {
head =
'Some(
^ShipListNode(
main'Ship("Serenity", 10),
Some(
^ShipListNode(
main'Ship("Raza", 22),
None))));
maybe_cur = head.imm;
while maybe_cur.NonEmpty() {
cur = maybe_cur.Expect();
ship = cur.ship;
set cur.hp -= 5;
println("Damaged {cur.name}!");
maybe_cur = cur.next;
}
}
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.
struct ListNode<T> {
ship T;
next priv vary ?^ListNode<T>;
}
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.
See you next time!
- Evan Ovadia
Draft TODO: estimate how many checks are eliminated just from the stdlib doing this
With your help, we can launch a language with speed, safety, flexibility, and ease of use.
We’re a very small team of passionate individuals, working on this on our own and not backed by any corporation.
If you want to support our work, please consider sponsoring us on GitHub!
Those who sponsor us also get extra benefits, including:
With enough sponsorship, we can:
We have a strong track record, and during this quest we've discovered and implemented a lot of completely new techniques:
These have been successfully prototyped. With your sponsorship we can polish them, integrate them, and bring these techniques into the mainstream. 6
Our next steps are focused on making Vale more user-friendly by:
We aim to combine and add to the benefits of our favorite languages:
We need your help to make this happen!
If you're impressed by our track record and believe in the direction we're heading, please consider sponsoring us:
If you have any questions, always feel free to reach out via email, twitter, discord, or the subreddit. Cheers!
Tentatively named the Vale Software Foundation.
Generational references, the linear-aliasing model, and higher RAII are all complete, and region borrowing, fearless FFI, and perfect replayability have been successfully prototyped. Be sure to check out the experimental version of the compiler!