Stream: contributing

Topic: Binary search


view this post on Zulip Vanand Gasparyan (Nov 15 2026 at 22:26):

I just created this draft PR to introduce partition_point function on List, which is basically the binary search algorithm. I need some help with the following:

  1. Design: Does this function belong in List? The other languages I know come with partition_point and binary_search. I really dislike the latter. In C++ it returns boolean, in Rust a Result of the index (any of the valid ones) or could-have-been-index. I dislike these two versions of binary_search for the same reason I like std::lower/upper_bound in C++. These are actually useful. Any thoughts on also adding some version of List.binary_search?
  2. Tail-call optimization: As I understand, Roc supports this, but the recursive function must qualify. How can I check if my implementation does? And even if it does _now_, can it be future-proofed with some tooling, in case the TCO requirements or the implementation itself change?
  3. Iterative vs recursive: Related to the last point, does it make sense to implement an iterative variant and run benchmarks? If so, is there a standard way of benchmarking?

view this post on Zulip Anton (Nov 16 2026 at 17:55):

  1. No standard way to benchmark, I am working on perf regression detection that runs on 3 operating systems. When adding a builtin function, we typically first do a PR that just gets a correct version in and later follow-up PRs for optimization.

view this post on Zulip Anton (Nov 16 2026 at 18:58):

Does this function belong in List?

I don't love the "assuming list is partitioned under predicate", it seem more idiomatic to have a type that would enforce this.

view this post on Zulip Anton (Nov 16 2026 at 19:00):

can it be future-proofed with some tooling, in case the TCO requirements or the implementation itself change?

If we include this in my upcoming perf regression check, that would do the job.

view this post on Zulip Anton (Nov 16 2026 at 19:05):

We've discussed something like a TCO annotation in the past, but it's not a high priority for now.

view this post on Zulip Anton (Nov 16 2026 at 19:10):

How can I check if my implementation does:

From Opus 5.5:

The practical check is a stress test: call the function with an input big enough to make it recurse millions of times.

1. Stress test with expect

sum_tail : U64, U64 -> U64
sum_tail = |n, acc| if n == 0 acc else sum_tail(n - 1, acc + n)

expect sum_tail(10_000_000, 0) == 50_000_005_000_000

Run it with roc test MyFile.roc --no-cache. I tried this with your local roc-main build:

If your function survives a depth in the millions, it qualifies. This test doesn't tell you which optimization kicked in, but that rarely matters: each one means the stack doesn't grow.

2. What makes a call qualify

From the "Tail Calls" section of docs/langref/functions.md and the compiler code (src/lir/tail_call_builder.zig), the recursive call's result has to be returned exactly as it is.

These still qualify:

These don't qualify:

Exception, "modulo cons": if the function returns a recursive tag union and the call's result goes straight into one of its tags, it also becomes a loop. For example, Cons(x, count_up(n + 1, end)) where the return type is LinkedList(a).

Calls to other functions: a tail call to a different function, or to a function passed in as an argument, reuses the current stack frame. So mutually recursive functions like is_even/is_odd that only call each other in tail position can't overflow either.

3. Compiler debug flag

roc can be built with a debug option that logs this:

zig build roc -Dprint-trmc=true            # one line per transformed function on stderr
zig build roc -Dprint-ir-after-trmc=true   # full IR dump of each transformed function

The output looks like tce: proc p12 (0 construct sites, 1 tail calls). It identifies functions by internal number, not by name, so it's most useful on a small file with a single recursive function. I haven't tried these flags; the stress test is simpler.

view this post on Zulip Vanand Gasparyan (Nov 21 2026 at 16:12):

That was quite helpful @Anton . I ran some experiments and found a version of the implementation that enables TCO. Here it is:

partition_point : List(a), (a -> Bool) -> U64
partition_point = |list, predicate| {
  partition_point_helper : List(a), (a -> Bool), U64 -> U64
  partition_point_helper = |lst, pred, offset| {
    if lst.is_empty() {
      return offset
    }
    mid_idx = lst.len() / 2
    mid = list_get_unsafe(lst, mid_idx)
    if predicate(mid) {
      partition_point_helper(lst.drop_first(mid_idx + 1), pred, offset + mid_idx + 1)
    } else {
      partition_point_helper(lst.take_first(mid_idx), pred, offset)
    }
  }

  partition_point_helper(list, predicate, 0)
}

The PR is no longer a draft, ready for review.

view this post on Zulip Anton (Nov 24 2026 at 17:04):

Thanks for your work on this @Vanand Gasparyan, I am going to come back to this when I can.


Last updated: Nov 26 2026 at 20:35 UTC