LanguagesArchitecture
Note: Regions are still a work-in-progress. Part 1 has been successfully prototyped, but parts 2-5 are only a preview describing how we expect them to work in practice, to show where we're headed and what we're aiming for. They could surpass our wildest expectations, or they could shatter and implode into a glorious fireball, who knows! Follow along as we implement all this, and reach out if anything isn't clear! 0 1

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.

A simple example

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:

  • maybe_cur.NonEmpty()
  • maybe_cur.Expect()
  • cur.ship
  • ship.hp
  • ship.name
  • cur.next

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!

A struct in two worlds

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:

  • maybe_cur.NonEmpty()
  • maybe_cur.Expect()
  • cur.ship
  • cur.next

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;
  }
}

Side Notes
(interesting tangential thoughts)
0

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.

1

We're aiming to complete regions by early 2024, check out the roadmap for more details.

2

?X means "Option", and ^ means "on the heap", so this is an optional ShipListNode on the heap.

3

A couple reasons:

  • They're perfectly predicted; the language always knows which way the CPU should speculatively execute.
  • The generations are usually on the same cache line as the data itself.

Most generic structures are multi-region objects

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:

  • The type, such as Ship.
  • The ownership, whether it be owned, heap-owned ('^'), non-owning ('&'), or weak ('weak&')
  • The region.

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.

Conclusion

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:

  • A programmer can write a complete Vale program without ever learning about regions or multi-region-objects.
  • A programmer can ignore any region markers and still understand the code; regions don't affect a program's semantics.

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

4

Draft TODO: estimate how many checks are eliminated just from the stdlib doing this

We're looking for sponsors!

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:

  • Early access to all of our articles!
  • A sneak peek at some of our more ambitious designs, such as memory-safe allocators based on algebraic effects, an async/await/goroutine hybrid that works without data coloring or function coloring, and more.
  • Your name on the vale.dev home page!

With enough sponsorship, we can:

  • Start a a 501(c)(3) non-profit organization to hold ownership of Vale. 5
  • Buy the necessary computers to support more architectures.
  • Work on this full-time.
  • Make Vale into a production-ready language, and push it into the mainstream!

We have a strong track record, and during this quest we've discovered and implemented a lot of completely new techniques:

  • The Linear-Aliasing Model that lets us use linear types where we need speed, and generational references where we need the flexibility of shared mutability.
  • Region Borrowing, which makes it easier to write efficient code by composing shared mutability with the ability to temporarily freeze data.
  • Higher RAII, where the language adds logic safety by enforcing that we eventually perform a specific future operation.
  • Perfect Replayability makes debugging race conditions obsolete by recording all inputs and replaying execution exactly.

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:

  1. Finalizing the compiler's error messages and improving compile speeds.
  2. Polishing interop with other languages.
  3. Growing the standard library and ecosystem!

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!

5

Tentatively named the Vale Software Foundation.

6

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!