blog.dopana

Back

In mainstream systems programming languages, the concept of a collection is typically partitioned into a rigid dichotomy: fixed-size compile-time arrays (such as int a[10] in C or [T; N] in Rust) and heap-allocated dynamic buffers (like std::vector in C++ or Vec<T> in Rust).

However, Gooseโ€™s creatorโ€”Wouter van Oortmerssen (the mind behind FlatBuffers)โ€”recognized that treating all dynamic sequences as monolithic heap vectors incurs severe architectural penalties on modern hardware:

  • Traditional vectors maintain a 3-word triplet on the call stack: (pointer, length, capacity).
  • Elements are scattered across disjoint heap addresses, forcing the CPU to traverse pointer indirections (pointer chasing).
  • Nested structures (strings inside structs, vectors inside vectors) create nested pointer hierarchies, fragmenting hardware CPU Cache Lines (typically 64 bytes).

To solve this, Goose introduces an innovative taxonomy: The Array Family and Variable-Sized Enums.

1. The Goose Array Taxonomy#

Instead of forcing every dynamic workload into a single Vec<T> abstraction, Goose provides five specialized array forms categorized strictly by how their memory boundaries and dimensions mutate:

Universal Non-Owning ViewArray Size Classification in GooseSlice WindowSlice WindowSlice WindowSlice WindowSlice WindowT[k]: Fixed Compile-Time SizeT[] / T[varint]: Sized on Build,Frozen AfterT[..k]: Inline Small-VectorCapacityT[>..]: Grow-Only with StableReferencesT[>..<]: Grow-Shrink for Stacksand QueuesT[:]: Universal Read-Only orMutable SlicePointer + Length Pair (ZeroAllocation)
Type SignatureMemory Layout & Operational SemanticsPrimary Use Cases
T[k]Fixed size kk known at compile time. Inlined with zero metadata overhead.3D vectors f32[3], 4x4 transform matrices f32[16], cryptographic hashes u8[32].
T[] or T[varint]Sized during construction and frozen permanently thereafter.Finalized immutable strings, parsed file records, static lookups.
T[..k]Capacity kk is embedded directly inline; grows and shrinks within that boundary.Short names (โ‰ค16\le 16 bytes), small tag collections, inline small-vector optimization.
T[>..]Grow-only. Elements can be appended, but never removed or shuffled.Primary data tables, order books, tree nodes. References into it never dangle.
T[>..<]Grow-shrink. Supports pushing and popping at the collection boundary.Stacks, queues, ring buffers, graph traversal worklists.
T[:]Slice view. A borrowed { pointer, length } pair that can view any array kind.Generic function parameters that inspect sequences without copying.

2. Ultra-Compact Strings: T[varint]#

In C++, std::string requires between 24 and 32 bytes on the stack for its pointer, length, and capacity metadata. In Rust, String demands 24 bytes.

In Goose, strings are modeled as u8 byte arrays. When parameterized with varint:

  • The length header is encoded as a variable-length integer.
  • For strings under 128 bytes (encompassing over 90% of identifiers, SKUs, usernames, and email addresses), the length metadata occupies only a single byte!
strings.goose
// Consumes exactly: 1 byte length header + 5 bytes text = 6 bytes in RAM!
let sku: u8[varint] = "SKU-7";

// Scratch builder on the Data Stack
var builder: u8[>..] = [];
builder.push_str("Hello ");
builder.push_str("Goose!");

// Non-owning slice view over the builder
let view: u8[:] = builder[..];
rust

There are no separate heap allocations and no scattered memory hops. The string is packed directly into the contiguous layout of the enclosing struct!

3. The Algebraic Data Type Revolution: Variable-Sized Enums (Shape..)#

In mainstream languages supporting Algebraic Data Types (such as Rust enum or C++ std::variant), an enumโ€™s memory footprint is always padded to accommodate its largest possible variant, plus discriminant tag overhead and alignment padding.

The Hidden Penalty of Traditional Enums#

Consider a typical geometric variant:

shapes.rs
enum Shape {
    Circle { r: f64 },       // Needs: 1 tag + 8 bytes = 9 bytes (padded to 16)
    Rect { w: f64, h: f64 }, // Needs: 1 tag + 16 bytes = 17 bytes (padded to 24)
    Dot,                     // Needs: 1 tag = 1 byte
}
rust

In Rust or C++, if you instantiate an array of 1,000,000 shapes containing alternating dots and circles, every single Dot is padded to 24 bytes to match the largest Rect variant! As a result, megabytes of RAM are squandered on empty padding bytes.

The Goose Breakthrough: Shape.. (Variable Mode)#

Goose Variable-Mode Enum: Shape.. (Exact Variant Payload)Rust / C++ Enum: Padded to Largest Variant (17-24 Bytes Each)Circle: Tag (1B) + Radius (8B) +Padding (8B) = 17 BytesRect: Tag (1B) + Width (8B) +Height (8B) = 17 BytesDot: Tag (1B) + Padding (16B)= 17 BytesTotal for 3 elements: 51 Bytes(Substantial space wasted onpadding)Circle: Tag (1B) + Radius (8B) =9 BytesRect: Tag (1B) + Width (8B) +Height (8B) = 17 BytesDot: Tag (1B) = 1 ByteTotal for 3 elements: 27 Bytes(Near 50% memory reduction!)

Goose allows developers to select the representation mode at the point of consumption:

  1. Fixed Mode (Shape): Retains uniform size for constant-time O(1)O(1) random indexing (shapes[i]).
  2. Variable Mode (Shape..): Each element occupies only the exact byte length of its variant:
    • Dot: takes exactly 1 byte (1 tag byte).
    • Circle: takes exactly 9 bytes (1 tag byte + 8 bytes f64).
    • Rect: takes exactly 17 bytes (1 tag byte + 16 bytes f64 dimensions).

[!NOTE] In official benchmarks, Shape.. arrays use 4x less memory than a standard Rust Vec<Shape> and execute at 2x the throughput, because tightly packed bytes fit denser into L1/L2 caches.

4. Case Functions: Dispatch Without Vtables#

Instead of nesting voluminous match or switch blocks inside methods, Goose provides Case Functions. Developers define specialized overloads per variant:

case_functions.goose
// Specialized overloads per variant
fn area(s: Shape.Circle) -> f64 { 3.14159 * s.r * s.r }
fn area(s: Shape.Rect) -> f64   { s.w * s.h }
fn area(s: Shape.Dot) -> f64    { 0.0 }

// The compiler verifies exhaustive variant coverage
// and lowers the dispatch to a jump table!
for s in packed {
    print("Area: ", area(s));
}
rust

There is no vtable pointer indirection and no object-oriented inheritance penalty. The compiler converts the tag byte check into an optimal assembly jump table.

5. Why CPU Hardware Loves Flat Memory#

Modern processors rely on sophisticated Cache Line Prefetchers. Whenever a core loads an address from main memory, it retrieves an entire 64-byte block into L1 cache (~1ns latency).

Hardware Cache Line (64 Bytes)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ [Item 1 (29B)] โ”‚ [Item 2 (29B)] โ”‚ [Item 3 starts...]         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
A single cache line load captures two full Goose records!
text
  • In C++ / Rust: A nested data record is fractured: the root struct is at one address, the string buffer is at a second heap address, and the child vector is at a third. Parsing a single record can trigger 3 to 4 distinct cache misses, stalling CPU execution units.
  • In Goose: Thanks to flat inline arrays and variable-sized enums, a single 64-byte cache line often captures two or more complete relational records. The processor streams sequentially through memory at maximum memory bus bandwidth.

6. Array Capability Comparison Matrix#

CapabilityC++ (std::vector)Rust (Vec<T>)Go ([]T)Goose (Array Family)
Array Sizing TaxonomyMonolithic vectorMonolithic VecHeap slice5 specialized operational forms
Inline Small-VectorExternal library neededRequires smallvec crateUnsupportedFirst-class (T[..k])
Immutable Frozen ArraysConst qualifier onlyUnsupportedUnsupportedFirst-class (T[], T[varint])
Growth Preserves ReferencesโŒ Reallocation invalidatesโŒ Banned by Borrow CheckerโŒ Reallocatedโœ… Guaranteed (T[>..])
Variable-Sized EnumsโŒ Padded to maxโŒ Padded to maxUnsupportedโœ… First-class (Shape..)
In-Place Enum MutationVerboseAwkwardN/Aโœ… Natural via &r

7. Conclusion#

Gooseโ€™s Array Family and Variable-Sized Enums exemplify a foundational engineering truth: Programming languages should be designed around hardware realities.

By eliminating wasteful padding, storing nested collections inline without heap pointers, and specializing array types based on how they grow (T[>..], T[varint], Shape..), Goose allows developers to:

  1. Write expressive, high-level code with memory safety guarantees.
  2. Reduce physical RAM requirements by 2x to 4x.
  3. Maximize hardware CPU cache bandwidth for performance gains that outpace traditional systems languages.

References#