Stream: bugs

Topic: excessive copying when annotations are missing


view this post on Zulip Steve Howell (Nov 24 2026 at 15:14):

Claude found a performance bug during real work on roc-chat.

If a function has no type annotation and returns a record with one list field appended to, Roc copies the whole list on every call. Building 16,384 items takes 11 to 14 seconds; adding the type annotation brings it down to 16 ms.

Probe :: [].{
        push = |{ tag, items }, x| { tag, items: items.append(x) }
        build : U64 -> U64
        build = |n| {
                var $r = { tag: "plans", items: [] }
                for _ in List.from_iter(U64.until(0, n)) {
                        $r = Probe.push($r, "Hello")
                }
                $r.items.len()
        }
        built : U64
        built = Probe.build(16384)
}

Run it with roc check Probe.roc. Annotating push : { tag : Str, items : List(Str) }, Str -> { tag : Str, items : List(Str) } makes it fast.

view this post on Zulip Steve Howell (Nov 24 2026 at 15:18):

Correction: for this file, building 16,384 items takes about 1.9 s without the annotation and 18 ms with it (8,192 items take 0.34 s, so the cost grows faster than linearly). Run it with roc check --no-cache Probe.roc, since a cached rerun skips the evaluation.

view this post on Zulip Steve Howell (Nov 24 2026 at 15:22):

I'll file an issue if somebody doesn't diagnose it here. I understand this is out of scope for 0.10 and it's obviously not blocking me any more (the workaround is to use annotations).

view this post on Zulip zhen (Nov 24 2026 at 15:40):

Never knew explicitly typing can save perf in ROC.

view this post on Zulip Steve Howell (Nov 24 2026 at 15:42):

zhen said:

Never knew explicitly typing can save perf in ROC.

#ideas > requiring more type annotations? is somewhat relevant.

view this post on Zulip Steve Howell (Nov 24 2026 at 21:24):

https://github.com/roc-lang/roc/issues/13468 Folks can ping me here for additional clarification.

view this post on Zulip Steve Howell (Nov 24 2026 at 21:39):

https://github.com/roc-lang/roc/issues/13469 is a similar bug where the "copy cliff" still exists in odd corners.

view this post on Zulip Richard Feldman (Nov 24 2026 at 21:55):

@Steve Howell is this on a dev build or optimized build?

view this post on Zulip Steve Howell (Nov 24 2026 at 21:56):

Both are in roc check. Full commands are in both issue descriptions.

view this post on Zulip Richard Feldman (Nov 24 2026 at 21:57):

oh

view this post on Zulip Richard Feldman (Nov 24 2026 at 21:57):

so what's happening here is just that polymorphism has a runtime cost for --opt=dev and --opt=size but not for --opt=speed

view this post on Zulip Richard Feldman (Nov 24 2026 at 21:58):

it's not the annotation that's fixing the problem, it's that the annotation narrows the type to be concrete (e.g. Str instead of the default "some type that has a from_quote method" - which is polymorphic)

view this post on Zulip Richard Feldman (Nov 24 2026 at 21:59):

I'd expect that if you called like Str.whatever on one of those strings it would have the same effect

view this post on Zulip Richard Feldman (Nov 24 2026 at 21:59):

for --opt=speed the annotation shouldn't do anything because we resolve polymorphism at compile time through monomorphization instead of at runtime

view this post on Zulip Richard Feldman (Nov 24 2026 at 22:00):

I guess also if you did roc check --specialize=yes I'd expect the difference to disappear too

view this post on Zulip Steve Howell (Nov 24 2026 at 22:05):

quick answer from Claude:

Thanks, Richard. Measured in built programs (the count from argv, so no compile-time evaluation): the unannotated #13468 shape is quadratic under --opt=dev (1.0 s / 4.5 s at 16k / 32k items) and gone with --specialize=yes (8 / 19 ms) or --opt=speed (2 / 3 ms), so you're right that it's the boxy lowering, though it looks like a full list copy per call rather than a flat dispatch cost. #13469's shape is linear in every built mode (dev 12 / 25 ms), so that one copies only in roc check's compile-time evaluation. Also, roc check doesn't take --specialize. It accepts the flag and ignores it.

We use roc check pretty frequently, so I think we still have the pitfall that roc check is gonna be slow for test code. Both of these were found for test code that tests boundary conditions at 64k thingies (a limit in the chat program).

view this post on Zulip Steve Howell (Nov 24 2026 at 22:06):

(in the narrow case of having things in tests that compute during roc check)

view this post on Zulip Steve Howell (Nov 24 2026 at 22:08):

It's not a real blocker for me, though. I've been trying to be thorough about filing stuff upstream before 0.10.

view this post on Zulip Richard Feldman (Nov 24 2026 at 22:09):

ha, I'll fix roc check ignoring --specialize

view this post on Zulip Richard Feldman (Nov 24 2026 at 22:09):

and the quadratic thing is surprising, I'll look into that too

view this post on Zulip Richard Feldman (Nov 24 2026 at 22:09):

could be a boxy bug

view this post on Zulip Steve Howell (Nov 24 2026 at 22:14):

I vaguely recall Claude theorizing about boxy in our private discussions. I just checked both issues; neither mentions it.

view this post on Zulip Steve Howell (Nov 24 2026 at 22:16):

ha, yeah, this is from Claude not too long ago on my console:

view this post on Zulip Richard Feldman (Nov 25 2026 at 12:30):

fixed in https://github.com/roc-lang/roc/pull/13471


Last updated: Nov 26 2026 at 20:35 UTC