RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Geometry, physics and speed

Arrays of value-class structs

StructList: a fixed-length list of value-class instances stored contiguously, with no per-element objects.

import 'package:riptide_struct/riptide_struct.ray';
1 classes7 functions and methods4 of 8 documented5 runnable examples

Overview

StructList<T>: a fixed-length list of @pragma('raya:value') instances.

  • Raya, AOT, T a value class known at the call site: the elements are stored contiguously in one Uint8List, in C layout (declaration order, natural alignment), with no per-element objects. list[i] checks the index once and reads the fields; with Raya's value pass a read-modify-write loop allocates nothing.
  • Anywhere else (other runtimes, JIT and evelyn run debug, generic code, a non-value T): a list of objects with the same values.
@pragma('raya:value')
final class Particle {
  const Particle(this.x, this.y, this.z, this.vx, this.vy, this.vz);
  @pragma('raya:f32') final double x, y, z, vx, vy, vz; // float32 storage; a plain double is float64
}

final particles = StructList<Particle>.filled(1 << 20, const Particle(0, 0, 0, 0, 1, 0));
for (var i = 0; i < particles.length; i++) {
  final p = particles[i];
  particles[i] = Particle(p.x + p.vx * dt, p.y + p.vy * dt, p.z + p.vz * dt, p.vx, p.vy, p.vz);
}

Field types: double (8 bytes, or 4 with @pragma('raya:f32')), int (8), bool (1), Float32x4 (16, 16-aligned) and nested value classes (laid out inline like a C struct member). The element size is rounded up to the largest alignment.

Native code. bytes is the storage (null when the elements are objects): pass bytes.address to a leaf @Native call to hand the array to C without copying. StructList<T>.fromBytes(pointer.asTypedList(n * size)) wraps native memory, with size = StructList.sizeOf<T>. Both need Raya AOT (bytes is null, sizeOf is -1 and fromBytes throws elsewhere), so keep a fallback for debug builds.

Raya's AOT compiler also stores value-class fields of ordinary classes inline (class Body { V3 pos, vel; } keeps six unboxed doubles), with no package needed.

Classes

abstract class StructList<T extends Object>

Constructors

factory StructList.filled(int length, T fill)

A list of length copies of fill.

Example
import 'package:riptide_struct/riptide_struct.ray';

final class Particle {
  const Particle(this.x, this.y, this.mass);
  final double x, y, mass;
  @override
  String toString() => 'Particle($x, $y, m=$mass)';
}

void main() {
  final ps = StructList<Particle>.filled(4, const Particle(0, 0, 1));
  for (var i = 0; i < ps.length; i++) {
    ps[i] = Particle(i.toDouble(), i * i / 2, 1 + i / 4);
  }
  print('length ${ps.length}');
  print('ps[3] = ${ps[3]}');
  var total = 0.0;
  for (var i = 0; i < ps.length; i++) {
    total += ps[i].mass;
  }
  print('total mass $total');
  // Without inline storage (this run), the elements are objects and bytes is null.
  print('inline bytes: ${ps.bytes == null ? 'none (objects)' : '${ps.bytes!.length} bytes'}');
}
Output
length 4
ps[3] = Particle(3.0, 4.5, m=1.75)
total mass 5.5
inline bytes: none (objects)
factory StructList.fromBytes(Uint8List bytes)

A list over bytes in C layout (for memory shared with native code, such as Pointer<Uint8>.asTypedList). Needs Raya AOT and a value class T.

Example
import 'package:riptide_struct/riptide_struct.ray';
import 'raya:typed_data';

final class Vec2 {
  const Vec2(this.x, this.y);
  final double x, y;
}

void main() {
  // fromBytes views memory in C layout; it needs ahead-of-time compilation and a value class.
  try {
    final v = StructList<Vec2>.fromBytes(Uint8List(32));
    print('viewing ${v.length} elements');
  } on UnsupportedError catch (e) {
    print('in this run: ${e.message}');
  }
}
Output
in this run: StructList.fromBytes needs Raya AOT and a value class

Static

sizeOfstaticmethod
static int sizeOf<T extends Object>()

The size of one element in bytes (C layout). Needs Raya AOT and a value class T; elsewhere -1.

Example
import 'package:riptide_struct/riptide_struct.ray';

final class Rgba {
  const Rgba(this.r, this.g, this.b, this.a);
  final double r, g, b, a;
}

void main() {
  // The C-layout size of one element; -1 where the elements are stored as objects (this run).
  final n = StructList.sizeOf<Rgba>();
  print('sizeOf<Rgba>() = $n');
  print(n < 0 ? 'stored as objects here' : '$n bytes per element');
}
Output
sizeOf<Rgba>() = -1
stored as objects here

Properties

lengthgetter
int get length
bytesgetter
Uint8List? get bytes

The elements' storage, in C layout, when they are stored inline; null when the elements are objects. Pass bytes.address to a leaf FFI call to hand the array to native code without copying.

Operators

operator []operator
T operator [](int index)
Example
import 'package:riptide_struct/riptide_struct.ray';

final class Point {
  const Point(this.x, this.y);
  final double x, y;
}

void main() {
  final pts = StructList<Point>.filled(3, const Point(1, 2));
  print('pts[0] = (${pts[0].x}, ${pts[0].y})');
  try {
    pts[3];
  } on RangeError {
    print('pts[3]: RangeError (length ${pts.length})');
  }
}
Output
pts[0] = (1.0, 2.0)
pts[3]: RangeError (length 3)
operator []=operator
void operator []=(int index, T value)
Example
import 'package:riptide_struct/riptide_struct.ray';
import 'raya:convert';

final class Point {
  const Point(this.x, this.y);
  final double x, y;
}

void main() {
  final pts = StructList<Point>.filled(9, const Point(0, 0));
  for (var i = 0; i < pts.length; i++) {
    pts[i] = Point(i.toDouble(), (i - 4) * (i - 4) / 4);
  }
  print('pts[8] = (${pts[8].x}, ${pts[8].y})');
        {'label': 'pts', 'style': 'points', 'points': [for (var i = 0; i < pts.length; i++) [pts[i].x, pts[i].y]]},
      ]})}');
}
Output
pts[8] = (8.0, 4.0)