writeIndexedListPayload<T, W extends RecordWriterBase> static method

void writeIndexedListPayload<T, W extends RecordWriterBase>(
  1. W w,
  2. List<T> items,
  3. void writeItem(
    1. W w,
    2. T item
    )
)

[4B count][8B×n offset table][item bytes] — offsets from payload start. This layout allows O(1) random item access (see the native LazyRecordList).

Implementation

static void writeIndexedListPayload<T, W extends RecordWriterBase>(
  W w,
  List<T> items,
  void Function(W w, T item) writeItem,
) {
  // One writer: reserve the offset table, write each item directly into it,
  // then backpatch the offsets — no per-item writer, no intermediate copies.
  final n = items.length;
  w.writeInt32(n);
  const offsetTableStart = 4; // right after the 4-byte count, within payload
  for (var i = 0; i < n; i++) {
    w.writeInt(0); // reserve an int64 slot; backpatched below
  }
  // w._length now points at where item 0 begins; record each item's offset.
  final offsets = List<int>.filled(n, 0);
  for (var i = 0; i < n; i++) {
    offsets[i] = w._length;
    writeItem(w, items[i]);
  }
  for (var i = 0; i < n; i++) {
    w._patchInt64(offsetTableStart + i * 8, offsets[i]);
  }
}