Skip to content

One + on a List<&2, U32> seals every List cell a native build makes, List<&1, U32> ones included: a loop that shares nothing runs 2x slower #1559

Closed

Description

@aldeni

Hi! I've been writing some larger programs in Bend lately, and reading the emitted C next to BendRT has been one of the nicest parts of it: the design is easy to follow once you have the paper open. That's how I ran into this one. I think it's mostly a question about a trade-off, so here's what I found, in case it helps.

What you did

bend heat.bend -o heat && ./heat --threads 1

and the same with cold.bend, which is heat.bend without sum2, twice and the twice(...) term in main.

What happened

Both programs build a 1M-cell List<U32> and sum it, 30 times. heat.bend adds one function that uses a List<&2, U32> twice, called once on a one-element list. That alone doubles the run time of the loop, which never shares anything:

Linux x86-64, --threads 1, medians of 9 interleaved runs (peak RSS of one run) cold.bend heat.bend
main (d328354) 162 ms, 17.8 MB 313 ms, 25.5 MB
2.0.37 157 ms, 17.9 MB 310 ms, 25.6 MB

The emitted C of the two programs differs only where List is built and matched. build seals both fields of every cell, and sum takes instead of reading in place:

// cold.c
e.mem[_nd_0 + 0] = _i_0;
e.mem[_nd_0 + 1] = _acc_1;
...
u64 _sp_0 = term_loc(_xs_0);
Term _f_0 = e.mem[_sp_0 + 0];

// heat.c
e.mem[_nd_0 + 0] = rfc_seal(e, _i_0);
e.mem[_nd_0 + 1] = rfc_seal(e, _acc_1);
...
u64 _sp_0 = ctr_take(e, _xs_0, 2, _fb_0);
Term _f_0 = _fb_0[0];

The shared list doesn't even exist at run time: twice(7 <> Nil{}) is folded into U32_BIN(0ull, +, 7ull) twice.

What I read

As far as I can tell, this is the design working as intended. BendRT says it in three places:

  • section 3.1: "a type is hot when some binder of it is used more than once […]; everything else is built and consumed through plain stores and loads";
  • section 4.2: "Only hot types pay any of this, and they seal every stored field with a count at build time";
  • section 11: "contraction exists only at Data, so counts are minted only where a whole-program analysis finds sharing […], and the unshared majority of a program compiles to moves, borrows and frees with no count traffic".

I can see why it's built this way, and I like it. A cold type pays nothing at all, not even a test in its matches. A keep is one redirect. And a shared node's fields never change, which is what lets a take copy them out before it acquires (the comment above term_peek puts it better than I can). In comp.ts, facts_hot heats "t:" + w.k, the datatype's name, so a share of any List makes Con hot and node_fill seals every Con the program builds.

Two things in this repro surprised me, though:

  • The cells being sealed are List<&1, U32>. If I read the Kinds section right, List<&1, U32> is a Type, not Data, so it can never be used with +. Only the List<&2, U32> in twice can be shared, and none of its cells come from build.
  • The guide's runtime section says "only + values carry a reference count", and section 4.2 says "an unshared value carries no count anywhere". That holds for the value itself, but here every field of every unshared cell carries one.

What I tried

In the emitted C only, not as a patch: I removed the two rfc_seal calls from build in main's heat.c, left sum's ctr_take alone, and compiled it with clang -std=c11 -O3 heat.c -lpthread -lm -ldl (the same line gives 318 ms for the unedited file). It ran in 176 ms and 17.8 MB, so most of the difference is the seal at build time. The ctr_take that stays costs the rest, about 14 ms.

Then I went through the history and saw that you'd tried the general version of this: 257b8c3 ("a keep seals its tree in place (term_seal); builds store raw words") moved the seal from the build to the first keep, and 527c68b brought the build-time seal back 73 minutes later. I couldn't work out from the commits what made you go back, and I'd be curious to know. My guess is the cost it moves onto a keep: walking and allocating for the whole raw part of the tree, where today a keep mints one redirect. Section 4.2 is careful that drop allocates nothing and runs from any code on either processor, and a keep that walks and allocates might not have that property. But it could just as well have been a correctness case, or a benchmark (the hvm5 demo landed in between).

A narrower idea, if it fits

A build whose type has a literal &1 quantity could store raw words, even when its datatype is hot. It's section 11's principle applied one level down: contraction exists only at Data, and a List<&1, U32> isn't Data, so nothing can ever keep it. As far as I can see, it also keeps every property the design protects:

  • a keep is still one redirect;
  • a shared node's fields still never change: a raw &1 cell is never shared, so no take ever copies its fields out while another owner holds it;
  • there is no walk, on either processor;
  • matches stay as they are: a hot datatype's match already goes through ctr_take, which handles raw and sealed nodes alike;
  • a generic build over an erased quantity still seals, as today.

For this repro, that is exactly the hand edit above (176 ms). It doesn't help a Data type that has no quantity, like String; there, something like 257b8c3 would be the way. I may well be missing a path where a &1 value meets a keep, so please take it as a question.

If the build-time seal is the trade-off you want everywhere, a sentence in the guide would help whoever meets this next, in the spirit of section 11's "a cost model a programmer can read": one + on a type anywhere in the program makes every value of that type carry counts on its fields.

Some context

This isn't only a micro-benchmark effect. In a larger program I profiled on 2.0.36, a server whose requests build, filter and print lists of records and strings, 281 of its 337 constructors were hot, and term_drop, rfc_wrap and span_fade together were 28–33% of the CPU samples on its list-heavy requests. I can't say how much of that is the build-time seal, since term_drop runs for unshared values too, but it's what sent me looking.

I'd be happy to help with whichever direction you prefer, whether that's a PR for the &1 build with these two files as its test, the guide sentence, or more measurements. No rush at all. And thanks for the paper: it made all of this much easier to follow.

The file

# heat.bend: a 1M-cell List<U32> built and summed 30 times. cold.bend is this
# file without sum2, twice, and the twice(...) term in main.
import Base

def build(n: Nat, +i: U32, acc: List<U32>) -> List<U32>:
  match n:
    case 0n:
      acc
    case 1n+p:
      build(p, U32.add(i, 1), i <> acc)

def sum(xs: List<U32>, acc: U32) -> U32:
  match xs:
    case Nil{}:
      acc
    case x <> t:
      sum(t, U32.add(acc, x))

def rounds(r: Nat, acc: U32) -> U32:
  match r:
    case 0n:
      acc
    case 1n+p:
      rounds(p, U32.add(acc, sum(build(1000000n, 0, Nil{}), 0)))

# All that cold.bend lacks: sum2, and twice, which uses a List<&2, U32>
# twice; main calls it once, on a one-element list.
def sum2(xs: List<&2, U32>, acc: U32) -> U32:
  match xs:
    case Nil{}:
      acc
    case x <> t:
      sum2(t, U32.add(acc, x))

def twice(+xs: List<&2, U32>) -> U32:
  U32.add(sum2(xs, 0), sum2(xs, 0))

def main() -> IO(Unit):
  IO.print(U32.show(U32.add(rounds(30n, 0), twice(7 <> Nil{}))))

cold.bend prints 1959202368, heat.bend prints 1959202382.

bend --version

bend 2.0.37, and main at d328354

uname -sm

Linux x86_64

clang --version (the first line)

clang version 22.1.8

Activity

  1. Lorenzobattistela commented on Oct 21, 2026

    @Lorenzobattistela
    Collaborator

    Thanks @aldeni for the careful write-up. We're keeping the build-time seal: #1566 tried storing &1 builds raw, and it breaks @unsafe copies like Array.fork in C, while your server measured neutral. So we documented the cost instead: the guide now says copying any value of a datatype makes all its nodes count their fields (#1812), and BendRT §4.2 and §11 say the same (#1813). Closing this with that PR.

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