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.
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.
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).
Never knew explicitly typing can save perf in ROC.
zhen said:
Never knew explicitly typing can save perf in ROC.
#ideas > requiring more type annotations? is somewhat relevant.
https://github.com/roc-lang/roc/issues/13468 Folks can ping me here for additional clarification.
https://github.com/roc-lang/roc/issues/13469 is a similar bug where the "copy cliff" still exists in odd corners.
@Steve Howell is this on a dev build or optimized build?
Both are in roc check. Full commands are in both issue descriptions.
oh
so what's happening here is just that polymorphism has a runtime cost for --opt=dev and --opt=size but not for --opt=speed
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)
I'd expect that if you called like Str.whatever on one of those strings it would have the same effect
for --opt=speed the annotation shouldn't do anything because we resolve polymorphism at compile time through monomorphization instead of at runtime
I guess also if you did roc check --specialize=yes I'd expect the difference to disappear too
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).
(in the narrow case of having things in tests that compute during roc check)
It's not a real blocker for me, though. I've been trying to be thorough about filing stuff upstream before 0.10.
ha, I'll fix roc check ignoring --specialize
and the quadratic thing is surprising, I'll look into that too
could be a boxy bug
I vaguely recall Claude theorizing about boxy in our private discussions. I just checked both issues; neither mentions it.
ha, yeah, this is from Claude not too long ago on my console:
- #13468: Richard is right. Dev's boxy lowering is what's slow, and --specialize=yes or --opt=speed makes it go away. It's quadratic, though, so boxy lowering is copying the whole list on each call, not paying a flat per-call cost for polymorphism. That's still worth fixing in dev builds.
fixed in https://github.com/roc-lang/roc/pull/13471
Last updated: Nov 26 2026 at 20:35 UTC