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.
Then in Part 4, we saw how we could immutably borrow only part of an object, so that we could have more precise control over what parts of our data were immutable.
In this final part, we'll show how we can limit some data to never outlive a certain region, and how that enables useful optimizations like zero-cost iteration.
In Part 4, we saw a basic linked list.
Here it is again, renamed to LinkedListNode to be consistent with Vale's standard library.
Now, we're going to add a LinkedList class which holds the head of the linked list.
The '' in ''?^LinkedListNode<T> means we're holding the head of the list in a cell, like we saw in Part 2.
Here's a basic use case, where we're turning the list immutable to iterate over it faster, and subtracting from each ship's fuel.
There are three surprising things here:
Let's talk about that last one, as it involves some concepts we haven't really covered before.
Under the hood, this is using an iterator, a small struct that keeps track of where we are in the list.
foreach is just syntactic sugar for using iterators.
Here's the above program, without the foreach syntactic sugar.
First, it takes the expression after in (here, list.iter()) and puts it in a local, iterable. (1)
Then, it makes an iterator for it using its Begin function. (2)
Every iteration, it will first call HasNext to see if there's a next element (3), and then call Next to get it. (4)
There are two interesting things happening here, regarding regions.
First, list.iter() is borrowing a cell immutably, and producing a cell guard like we saw in Part 2. Here we're returning that cell guard. 2
Second, we're using Begin to make an iterator that is scoped to the lifetime of that cell guard. The compiler then makes sure the iterator won't outlive the cell guard.
Let's talk about that a bit more!
Draft TODO: Talk about how this requires an RC, or scope tether uprooting.
When we say iterable.Begin(), we're giving Begin a ?&i'LinkedListNode<Ship>, where i' is the implicit hidden region tied to the lifetime of the iterable cell guard, like we saw in Part 2.
LinkedList.vale has a function which accepts that type:
The call will return a LinkedListIter<i', Ship> which we can later call .HasNext() and .Next() on.
Note the i' in LinkedListIter<i', Ship>.
When a struct has a region generic parameter, the compiler makes sure that the struct doesn't outlive that region.
So here, we know that LinkedListIter<i', Ship> won't outlive i'. i' came from the cell guard, so the iterator won't outlive the cell guard.
That's what it means to "limit" a struct; if a struct has a region parameter, the compiler makes sure it does not outlive that region.
The <x', T> is just included for clarity, the compiler can infer it if we leave it out.
We can use this pattern for any data structure, not just LinkedList.
Vale's List is an array list with a cell.
This lets us iterate over the array with zero generation checks.
HashSet, HashMap, and all the collections have cells under the hood. These are private implementation details that the user doesn't have to worry about.
We can use this for any class; Part 2 showed us how a Ship can have a private Engine in a cell.
These techniques can even make entire architectures fast. Entity-component-system is an architecture that holds all of its state in List<T>s, and iterates through them a lot. Since iterating is now zero-cost, architectures like this become much faster in Vale.
As you can see, structs can be "scoped" to the lifetime of an existing region, such as one that's produced by opening a cell. This lets us more flexibly access data from a region.
This is especially useful for iterators, which like to immutably open a collection's contents and read them much faster.
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: talk about how all these techniques come together to give us a ton of optimization power. kind of like a super-powered blend of borrow checking, shared mutability, and other stuff.
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: