Skip to content

C backend: nested product type uses K^depth memory (974-byte file, 19.3 GB RSS) #843

Closed

Description

@H4ad

What you did

bend repro.bend -o /tmp/out.c

What happened

I was experimenting writing a postgres driver for bend and I reach a issue where the compiler was taking too much time and a lot of memory to compile, so I asked my AI to reproduce the issue in a smaller file.

Steps to reproduce:

bend repro.bend -o /tmp/out.c     # 19.3 GB, 50 s, 97 MB .c
bend repro.bend -o /tmp/out.js    #  3.3 GB, 3.6 s, 13 KB .js
bend repro.bend                   #  102 MB, 0.1 s

Maybe I'm doing something wrong/dumb, maybe it's a limitation/bug in the compiler, not sure to be honest, so feel free to close if this out of the scope as bug.

The file

import Base

type BoxK<-a: Data> is Data:
  BoxK{f0: a, f1: a, f2: a, f3: a, f4: a, f5: a, f6: a, f7: a}

def mk0() -> U32:
  1

def mk1() -> BoxK<U32>:
  BoxK{mk0(), mk0(), mk0(), mk0(), mk0(), mk0(), mk0(), mk0()}

def mk2() -> BoxK<BoxK<U32>>:
  BoxK{mk1(), mk1(), mk1(), mk1(), mk1(), mk1(), mk1(), mk1()}

def mk3() -> BoxK<BoxK<BoxK<U32>>>:
  BoxK{mk2(), mk2(), mk2(), mk2(), mk2(), mk2(), mk2(), mk2()}

def mk4() -> BoxK<BoxK<BoxK<BoxK<U32>>>>:
  BoxK{mk3(), mk3(), mk3(), mk3(), mk3(), mk3(), mk3(), mk3()}

def mk5() -> BoxK<BoxK<BoxK<BoxK<BoxK<U32>>>>>:
  BoxK{mk4(), mk4(), mk4(), mk4(), mk4(), mk4(), mk4(), mk4()}

def mk6() -> BoxK<BoxK<BoxK<BoxK<BoxK<BoxK<U32>>>>>>:
  BoxK{mk5(), mk5(), mk5(), mk5(), mk5(), mk5(), mk5(), mk5()}

def mk7() -> BoxK<BoxK<BoxK<BoxK<BoxK<BoxK<BoxK<U32>>>>>>>:
  BoxK{mk6(), mk6(), mk6(), mk6(), mk6(), mk6(), mk6(), mk6()}

def top(p: BoxK<BoxK<BoxK<BoxK<BoxK<BoxK<BoxK<U32>>>>>>>) -> U32:
  0

def main() -> U32:
  top(mk7())

bend --version

bend 2.0.15

uname -sm

Linux x86_64

clang --version (the first line)

clang version 22.1.8

Activity

  1. added
    bug
    on Sep 19, 2026
  2. moorbrook commented on Sep 19, 2026

    @moorbrook
    Contributor

    Confirmed on main at 15ae0c8 (bend 2.0.16), Darwin arm64, Apple clang 21.0.0, 24 GiB host. Max RSS from /usr/bin/time -l.

    program -o out.c -o out.js
    the file above 9.05 GiB, 63 s, 97.4 MB 3.04 GiB, 2 s, 13.1 KB
    same file, BoxK given one extra nullary arm 10.46 GiB, 73 s, 110.9 MB 3.36 GiB, 3 s, 13.1 KB

    The second row is the file above with only this changed:

    type BoxK<-a: Data> is Data:
      Zed{}
      BoxK{f0: a, f1: a, f2: a, f3: a, f4: a, f5: a, f6: a, f7: a}

    So the cost is not specific to a product type; a sum type pays the same, and a fix that keyed on a single-constructor layout would not cover it.

    For contrast, nesting depth on its own is cheap: a chain of 24 defs, each matching a Maybe<&2, U32> and threading the field on into a 24-field record, compiles to C in under a second at 0.14 GiB, and the curve from depth 4 to depth 24 is flat (0.11 to 0.14 GiB, 92 KB to 187 KB of C). It is the field count per level raised to the nesting depth that costs, not the nesting.

  3. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.19.

    Two things were unbounded. The layout of a datatype was flat at any width, so a record nested K deep with F fields was F^K machine words and the emitter named every one as a local. A datatype past 256 words is now a heap node, the way a recursive type already was. Second, the constant folder turns your chain into a DAG, and the emitter walked that DAG as a tree; a folded static term now emits once per layout.

    Your file at depth 7, on this machine:

    before after
    time 50 s 0.06 s
    peak RSS 19.3 GB 122 MB
    emitted C 97 MB 82 KB

    The ceiling is 256 words because the widest flat record we ship is a 24-word triangle in the shader demo, and 256 leaves your BoxK boxed from depth 3 up. A record that fits stays flat, so nothing in the fast path changed.
    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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions