Currently, the documentation of List.reserves reads: "Ensure this list has room for at least spare additional items.". I thought for example that if I do reserve(2) it would just make sure that there is at least 2 empty spaces to add new elements. But my current experiment, in a linux VM, results in quadratic performance behavior when using reserve.
In practice, a minimal example showing the problematic perf behavior would look like this:
var $xs = []
while $xs.len() < n {
$xs = $xs.reserve(2)
$xs = $xs.append(0)
$xs = $xs.append(0)
}
Doing this results in quadratic timing increase when n increases. If we remove the reserve line instead, the time grows roughly linearly with n.
What are your thoughts about that? Should I open an issue? Is that specific to the platform I’m using (the default one) or a problem across platforms?
I will dig into this
Yeah the docs are very light on detail :smile: I will fix that.
List.reserve should say it allocates exactly len + spare and reallocates+copies whenever spare exceeds current slack. So, the docs should mention it belongs outside loops, and repeated small reserves are O(n²).
If we remove the
reserveline instead, the time grows roughly linearly withn.
Roc reserves many more elements by default so it needs to a lot less copying in this case.
Anton said:
Yeah the docs are very light on detail :smile: I will fix that.
Last updated: Sep 03 2026 at 15:16 UTC