blog.dopana

Back

For decades, systems software engineers have been trapped in a difficult trilemma when selecting a language for high-performance applications:

  1. C / C++ / Zig: Offer maximum raw performance and low-level control, but developers bear the burden of manual memory tracking (malloc / free). Inevitably, spatial memory violations, use-after-free, and memory leaks remain pervasive security risks.
  2. Go / Java / C#: Eliminate memory bugs through automated Garbage Collection (GC), but introduce non-deterministic stop-the-world latency spikes, high CPU overhead, and memory fragmentation due to scattered pointer indirection.
  3. Rust: Delivers both safety and high throughput without a garbage collector through static ownership and borrow checking. In exchange, developers must navigate restrictive lifetime annotations ('a), exclusive aliasing constraints (&mut), and frequent retreats into unsafe for complex graph or cyclical data structures.

Into this landscape enters Goose, a fascinating systems programming language conceived by Wouter van Oortmerssen (creator of FlatBuffers, the Lobster language, TreeSheets, and a veteran of Google and 3D game engines). Goose proves an intriguing premise: It is possible to achieve complete memory safety in a systems language with NO general heap allocator, NO garbage collector, and NO lifetime annotations!

[!NOTE] TL;DR: Goose completely discards the concept of a general-purpose heap. Dynamic values reside exclusively on compiler-managed Data Stacks, growing via pointer bumps and deallocating in a single instruction at scope exit. By storing nested structures flat and inline without pointer chasing, Goose regularly runs faster than hand-optimized C++ and safe Rust while consuming noticeably less memory.

1. Mental Model (ELI5): The Clever Tool Roll#

To understand why Goose feels distinct from every mainstream language, consider this everyday analogy:

Goose: Flat Contiguous MemoryC++ / Rust / Go: Scattered Boxes and PointersPointerPointerPointerPointerOrder Struct (160 bytes)Heap String: aliceHeap Vector: Items ArrayHeap String: SKU-441Heap String: SKU-7Flat Order Block (29 bytes, 0allocations)[id][alice][SKU-441:2:1999][SKU-7:1:500]
  • Traditional Languages (C++, Rust, Go): Imagine setting up a desk where every time you need a new item (a string or a growable list), you run across the room, purchase a small cardboard box on the heap (malloc), place it in an arbitrary corner, and stretch a physical piece of string (a pointer) connecting back to your desk. Cleaning up means following every individual piece of string to discard each box. If a string snaps, the abandoned box becomes a memory leak.
  • Goose: Imagine an expandable leather tool roll. When you need to add another item, you simply unroll the mat a bit further and lay the item directly next to the previous one (a pointer bump). There are zero strings or separate boxes. When your work is finished, you roll the mat back up in a single physical motion, and millions of items vanish instantly!

2. A Radical Memory Architecture: Data Stacks#

Unlike conventional architectures that divide execution between a native call stack and an untamed heap, Goose organizes memory into the Native Call Stack and a finite set of NN Data Stacks, computed statically at compile time.

2. Scope Exit Deallocation1. Allocation and GrowthStack BaseElement 0 (first ref)Element 1Element N (Stack Top)Pointer Bump (Zero overhead)Single CPU Store (Reset BumpPointer)1,000,000 Elements InstantlyFreed

Core Execution Mechanics#

  1. Each Data Stack is a large reserved virtual address range (e.g., 2 GB of virtual space, committed by the OS page table only when actually written).
  2. At any given moment, at most one resizable value is active per stack, always positioned on top.
  3. Appending an element (push) involves advancing a pointer forward. There are no capacity rechecks, no memory relocations, and no copying.
  4. When the enclosing lexical scope terminates, all memory allocated within that scope—regardless of nesting depth or collection size—is freed with a single CPU store instruction that resets the bump pointer. There are no destructor traversals and no GC sweep phases.
scratch.goose
for round in 3 {
    // Grow-only array: allocation is purely a pointer bump
    var scratch: u8[>..] = [];
    for i in 100000 {
        scratch.push((i % 256) as! u8);
    }
    print("Round ", round, ": ", scratch.len, " bytes");
} // 100,000 elements reclaimed in 1 CPU cycle at scope boundary!
rust

3. Growth Preserves References (Stable References)#

In C++ (std::vector<T>) or Rust (Vec<T>), inserting elements past capacity triggers an allocation spike: a larger memory block is acquired on the heap, existing elements are copied over, and the old buffer is deallocated.

[!WARNING] Because reallocation relocates memory addresses, any pointer or reference pointing into a std::vector or Vec can instantly become a dangling reference. Rust’s borrow checker strictly bans holding a reference while calling push. Developers are forced to substitute references with manual integer indices (e.g., u32).

In Goose, memory never relocates. As a result, references pointing into a growing array remain completely valid indefinitely:

stable_ref.goose
struct Item { id: i32, weight: f32 }

var items: Item[>..] = [];
// Capture a direct reference to element 0
let first .= items.push(Item { 1, 0.5 });

// Push 1,000,000 additional elements into the container
for i: i32 in 2..1000001 {
    items.push(Item { i, 0.0 });
}

// 'first' remains 100% valid, stable, and safe to mutate!
first.weight = 99.5;
rust

In Goose, push yields an active typed reference to the newly inserted record. This enables developers to interlink elements into trees, cyclic graphs, or doubly-linked chains while populating an array without fear of pointer invalidation.

4. Flat and Contiguous: Nested Data Stays Inline#

Consider a common relational structure: an Order containing customer metadata and an embedded array of Item records:

order_book.goose
struct Item  { sku: u8[varint], qty: varint, cents: varint }
struct Order { id: varint, customer: u8[varint], items: Item[varint] }

var book: Order[>..] = [];
book.push(Order {
    id: 1001,
    customer: "alice",
    items: [
        Item { sku: "SKU-441", qty: 2, cents: 1999 },
        Item { sku: "SKU-7",   qty: 1, cents: 500 }
    ]
});
rust

Here is how different language runtimes represent this single order record in memory:

LanguageRepresentational TypesRAM FootprintHeap Allocations
C++std::string + std::vector<Item>160 bytes1 heap allocation
RustString + Vec<Item>153 bytes4 heap allocations
Gooseu8[varint] + Item[varint]29 bytes0 allocations!

In Goose, this entire hierarchical order consumes only 29 bytes laid out consecutively in memory without a single internal pointer. When iterating through a batch of orders, the CPU streams linear bytes directly through L1/L2 caches, eliminating cache misses and pointer-chasing overhead entirely.

5. Memory Safety Without Lifetime Annotations#

Rust popularized safe systems programming through explicit lifetime parameters such as 'a and 'b. While robust, lifetimes often increase cognitive load and complicate signatures across APIs.

Goose achieves compile-time safety without any lifetime syntax by automatically inferring static roots:

lifetimes.goose
fn longest(a: u8[:], b: u8[:]) -> u8[:] {
    if a.len >= b.len { a } else { b }
}

var outer: u8[>..] = [];
var w = outer[..];
{
    var inner: u8[>..] = [];
    format(inner, "nested buffer");
    w = longest(outer, inner);
    // COMPILE ERROR: Storing a reference rooted at 'inner',
    // which does not outlive destination 'w'!
}
rust

The Goose compiler tracks the root provenance of every reference and slice. If a reference attempts to escape beyond the lifetime boundary of its root variable, compilation is rejected immediately with a clear error pointing out the root owner and target destination.

6. Relative References: Position-Independent Structures#

Standard 64-bit pointers demand 8 bytes of storage each. In pointer-heavy graphs or binary trees containing millions of nodes, pointer metadata often dwarfs the actual payload.

Goose introduces Relative References:

  • Node&<u32>: Stores the connection as a typed self-relative offset (taking only 4 bytes, or even 2 bytes via u16).
  • Because links are relative to the node itself, the structure is naturally position-independent.
binary_tree.goose
// A binary tree node taking only 12 bytes total, including both left/right links!
struct Node {
    key: i32,
    left: Node&<u32>?,
    right: Node&<u32>?
}
rust

Instant Byte-Level Serialization#

Because relative links retain their meaning regardless of where the memory block resides, data structures can be persisted directly to disk or transmitted across a socket as raw byte images:

serialization.goose
// Capture the entire tree as a raw framed byte image
var image: u8[>..] = tree.to_bytes();

// Reconstruct and verify structural integrity in a single pass
var loaded, ok = from_bytes<Node[>..]>(image);

// If bytes are tampered with or corrupted...
image[image.len - 3] = 200;
var bad, bok = from_bytes<Node[>..]>(image);
// bok returns false and bad yields an empty collection, preventing wild pointers!
rust

from_bytes validates framing, variant tags, and link boundaries in one pass before letting raw bytes re-enter the typed program space.

7. Multi-Frame Error Propagation#

In Rust, error handling often requires pervasive Result<T, E> wrapping and the ? operator at every intermediate call frame. In C++, exceptions entail heavy stack-unwinding machinery.

Goose provides return err from func:

This construct is verified statically (ensuring the target frame is an active ancestor) and implemented via an invisible return discriminant check per stack frame without expensive runtime unwinding.

8. Concurrency Without Shared Mutable Memory#

Goose eliminates data races entirely by enforcing zero shared mutable memory across threads:

  • Each thread_fn executes in an isolated environment with its own dedicated Data Stacks and thread-local globals.
  • Threads communicate exclusively through typed message queues (qput, qget).
  • Queued payloads must be flat values (devoid of dangling external references). Passing data is simply a hardware memcpy.

9. Comprehensive Comparison: Goose vs. Mainstream Languages#

DimensionC / C++RustGoZigGoose
Memory ModelManual (malloc/free)Ownership & BorrowingGarbage Collector (GC)Manual AllocatorsCompiler Data Stacks
General HeapYesYesYesYesNone (Zero Heap)
Spatial / Temporal Safety❌ Prone to UAF / Leaks✅ Safe (outside unsafe)✅ Safe (with nil panics)❌ Manual (prone to UAF)✅ 100% Safe (No unsafe)
Lifetime AnnotationsNoneMandatory ('a)NoneNoneNone (Static Roots)
References During Growth❌ Invalidated on realloc❌ Banned by Borrow Checker❌ Reallocated❌ Invalidated on realloc✅ Preserved (Stable)
Nested Data LayoutScattered via pointersScattered via pointersScattered via pointersScattered via pointersContiguous (Inline/Flat)
Scope Deallocation CostO(N)O(N) destructor callsO(N)O(N) destructor callsNon-deterministic GCO(N)O(N) allocator freesO(1)O(1) (Single Store Op)
JIT / Fast PrototypingSlow compilationSlow compilationFast compilationFast compilationInstant (Embedded TinyCC)

10. Engineering Limitations & Trade-Offs#

While Goose introduces profound advancements, it is designed with explicit technical trade-offs:

  1. Scope Ownership Discipline: Developers must deliberately think about where data is anchored. For non-hierarchical, long-lived cyclical data, you must utilize library abstractions like slot_pool or slice_pool.
  2. Recursive Function Constraints: Recursive routines cannot keep growable data across recursive call frames without passing a pre-allocated pool downward.
  3. Variable-Sized Arrays are Sequential: Because elements can vary in byte length (varint), indexing by offset (array[i]) is not available in O(1)O(1) time; you must iterate sequentially or maintain a parallel index of references.
  4. Whole-Program Compilation: The compiler requires full visibility over the program to compute data stack budgets and static root graphs.

11. Conclusion#

Goose represents a bold and refreshing departure from conventional systems language design. By daring to question the necessity of the heap allocator and harnessing the raw power of Data Stacks coupled with compact, flat memory layouts, Goose achieves:

  • Benchmark execution speeds that outpace both C++ and safe Rust.
  • A 1.3x to 1.9x reduction in peak memory consumption.
  • Absolute memory safety without the cognitive friction of lifetime syntax ('a) or the non-deterministic overhead of garbage collection.

Whether you are building game engines, real-time audio systems, embedded services, or high-throughput data processing pipelines, Goose is an extraordinarily innovative language worth studying and adopting.

References#