Skip to content

Three string-literal match arms exceed 512 MiB during C generation on 2.0.19 #892

Closed

Description

@gouveags

What you did

I hit compiler memory pressure while building an OpenAI SDK for Bend. I reduced one part of it to three string-literal cases on Bend 2.0.19. This looks related to #867, but uses string patterns rather than the dense U32 tables fixed there.

Compile the file below to C. On Linux I ran the reproduction in a memory-limited scope so it could not take down the desktop:

systemd-run --user --scope -p MemoryMax=512M -p MemorySwapMax=0 \
  timeout 30s bend literal.bend -o literal.c

What happened

The compiler exceeded the 512 MiB cgroup limit and was killed before emitting C. The kernel recorded a memory-cgroup OOM with 521956 KiB anonymous RSS. This is a bounded reproduction, not a measurement of how much memory an unrestricted run would eventually need.

Replacing the string patterns with String.eq and a match on three Bools compiled successfully under the same limit: 1.06 s, 136048 KiB maximum RSS, approximately 97 KiB of C.

In the larger SDK I also observed multi-gigabyte compiler processes and a host OOM while two builds overlapped. I have stopped overlapping builds and now use memory limits. The SDK workaround is to use enums/comparisons for protocol dispatch. I am not claiming the three-case example alone caused the host OOM.

Is there a small emitter-side optimization for these literal string matches, or should this remain a documented limitation? I avoided touching the language/checker.

The file

import Base

def classify(s: String) -> U32:
  match s:
    case "response.completed": 1
    case "response.failed": 2
    case "response.incomplete": 3
    case _: 0

def main() -> IO(Unit):
  do IO<Unit>:
    s : String <- IO.try(String, IO.get_env("BEND_REPRO_INPUT"))
    IO.print(U32.show(classify(s)))

Comparison version:

import Base

def classify_flags(a: Bool, b: Bool, c: Bool) -> U32:
  match a b c:
    case True{} _ _: 1
    case _ True{} _: 2
    case _ _ True{}: 3
    case _ _ _: 0

def classify(+s: String) -> U32:
  classify_flags(String.eq(s, "response.completed"), String.eq(s, "response.failed"), String.eq(s, "response.incomplete"))

def main() -> IO(Unit):
  do IO<Unit>:
    s : String <- IO.try(String, IO.get_env("BEND_REPRO_INPUT"))
    IO.print(U32.show(classify(s)))

bend --version

Bend 2.0.19 (official Linux x64 release)

uname -sm

Linux x86_64

clang --version (the first line)

clang version 21.1.8 (Fedora 21.1.8-6.fc43)

The failing command only emits C, so Clang is not invoked.

Activity

  1. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.22 (bend update). A word match now compares the whole word: a string, char, U32 or F32 literal pattern is one equality and its default one else, instead of 32 nested Bool matches per character written as nested ifs. Your file emits 291 KB of C (was 16.7 MB) and 52 KB of JS (was 6.8 MB); the remaining memory is the checker's own. A default that calls a handler collapses too. Pinned as tests/compile/string_match_sparse.bend. Thanks for the measurements.

    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