Skip to content

Guide: what a shared +Data costs inside a parallel call is only documented in a demo's comments #836

Closed

Description

@AdrielSantana

Summary

The guide says that + values carry a reference count. It does not say what
that count costs inside a parallel call: one atomic operation per node touched
per use, on a counter that every other lane is touching too. That fact decided
the performance of everything I built in two days of Bend renderers, and the
only place it is written down is in comments inside
demos/app_slash_boss_3d/bend3d.bend:

# All scalars: the record rides in registers through every
# call and fork; a boxed record shared by every vertex is an atomic
# count per use.
# One list per cell, not per pixel: a list shared by
# pixels is contended atomics.

The guide's Parallelism section lists the two promises a parallel call makes:
the calls are independent, and they run in roughly the same time. In practice
a third thing decides the speed — what the lanes share — and the guide is
silent on it. Worse, a reader of the guide alone is steered toward the wrong
design: Array is Type and cannot be read by both sides of a parallel call,
so the natural next move for a world every pixel reads is a shared +Data
tree, which is exactly what those comments warn against.

Measurements

Voxel raycaster, 32×32 columns, each column a U32 of heights. Every row of
each table renders the same image: the harness sums all pixel colours and the
checksum is identical across rows. Bend 2.0.9, Apple M5, median of 5.

Reading the world once per column crossing, 512²:

the world is ms/frame
a pure function of (x, z), nothing shared 23
a shared +Data quadtree of all 1024 columns 220

Attempts to make the tree read cheaper, checksum identical each time: a
Bool.pick chain instead of a 4-way match helper, 1.2x; packing four
columns per leaf, 1.00x; hoisting the frame-constant camera basis out of the
per-pixel loop, 1.00x; caching the ray's region node, 1.00x; unrolling the
recursive walk, 1.00x; the last two together, 1.2x. The cost is touching the
shared top-level nodes, not depth or arithmetic.

Storing only the player's edits (a WNone{} constructor means "nothing edited
below"; the world starts as one) and computing the rest, 256², N edited
columns scattered over the world. Only the tree's shape changes, and the
checksum is identical on every row:

edited columns ms/frame
0 7
16 35
64 38
256 44
all 1024 41

Played interactively with tick instrumented, the first edit doubles the
frame and it plateaus at about 4x the untouched world. Sixteen edits anywhere
turn the root into a node every ray must touch.

What I am asking for

A paragraph in the Parallelism section, next to the two promises, saying
roughly:

A + value shared across the lanes of a parallel call is reference-counted,
and every node a lane touches is an atomic operation on a count that every
other lane is also touching. Keep per-lane state in scalars: they ride in
registers through every call and fork. Read a shared structure once per
group of lanes, not once per lane, and never in the innermost loop.
bend3d.bend bins its scene per screen cell for exactly this reason.

That is the content of the two comments above, in the place people look
first. I would have designed the world differently on day one with that
paragraph, and #828 (fork depth) is the other half of the same lesson. When I
brought this up on Discord the answer was that this needs to be covered at
the guide level, so this issue is just the tracking item for that.

A design question, asked as a question

Is a borrowed read of a +Data inside a ! region, one that does not touch
the count because the compiler can see the value is only read and never
dropped inside the fork, something the runtime model could support? It is the
capability that would make the shared-tree design viable as written. If it
cannot fit, that is just as useful to state in the guide: "a shared tree read
per lane is not something the runtime will make cheap."

Environment

Bend 2.0.9, macOS 26.6.2, Apple M5 (10 CPU / 8 GPU cores), Metal.
Every number above, with the harnesses and checksums, is in the README
section "Onde isto para": https://github.com/AdrielSantana/metal-bending

Activity

  1. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    Since 2.0.10 this lives in bend guide shaders (guide/SHADERS.md): the cost of a + tree read by every lane, your voxel case with its numbers, the fix (per-tile flat lists built on the host), and the Ownership section, which answers your design question: the compiler borrows a boxed parameter that a def only matches or passes on (every read is term_peek, no count), and owns one it returns or stores. 2.0.13 adds the pointer you asked for at the end of the guide's Parallelism section. Closing; reopen if the tutorial leaves a gap.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions