Checking how many agents are working

How this works
Contact
← Blog
A cinematic photograph of a small steel goose figurine standing beside one brass gear on a dark workbench, a thin amber light along its wing, in teal and steel, the AMARBARO mark and wordmark centred over it.
✎blog post

When we leave Mojo: a tiny language for hot loops

The short version

Goose fits a CPU kernel that Python calls through a C interface, and its first benchmark win turned into a small real win of 1.4 s per job once the setup cost was moved out of the loop.

What this is about

Mojo is the language we reach for first. But we do not use one language for everything. For one job in our 3D prop pipeline, a small loop on the processor that Python had to call, we used a different language called Goose.

This article says what Goose is, when we pick it over Mojo, and what happened when we tested it. The test is a good lesson in itself, because our first benchmark said the new code was four times faster, and in the real pipeline it lost.

Why it matters

A beginner often hears "rewrite the slow part in a faster language" as if it were free. It is not. The new code has to be called from the old, it has to be measured in real conditions, and the gain has to be worth the trouble.

Our story shows all three steps, with real numbers, including the failure.

What Goose is

Goose is a systems language with no heap. Instead of asking for memory while it runs, it keeps its data on structured stacks. It compiles to C. And it can produce a plain shared library that other languages can call through the C interface, which Python does easily.

For us, it has one clear role. When the target is a processor kernel behind a C interface that Python calls, Goose fits. It is processor only, with no graphics card support. Everything else, from tools to libraries to GPU kernels, is Mojo.

The job

In our ComfyUI pipeline for making 3D props, a profile showed that 26.9 percent of the server's cycles were in one scipy routine, _ckdtree. It was called from a function that measures, for thousands of points in space, the distance to the nearest surface triangle. The existing code looked at only the 8 nearest triangle centers, so it was approximate.

We wrote an exact version in Goose. It organizes the triangles in a tree of bounding boxes, so it can find the closest surface point without testing every triangle.

The recipe, in two commands

goose --standalone --header lib.h -o lib.c src.goose
gcc -O3 -march=native -shared -fPIC -o libX.so lib.c -lm -pthread

The first line turns Goose into C with no main function. The second builds a shared library. A function marked export fn can be called from Python. Data crosses the border as a pointer and a length, and numpy arrays can pass through with no copy.

One rule bit us. A Goose runtime is single-threaded. To use 16 threads, we loaded 16 copies of the library, one per worker, and Python released its lock during each call. Sixteen runtimes ran sixteen workers.

The first benchmark, and where it lied

The test used 150,000 triangles and 1 million query points.

Version Time
scipy cKDTree, 8 candidates, 16 workers (what ComfyUI did) 3.63 s
Goose, 1 thread 10.4 s
Goose v1, 16 runtimes, tree built per call on each thread 0.93 s
Goose v2, tree built once, 16 runtimes query it 0.79 s

Version 1 looked about four times faster than scipy. We were pleased. Then we put it in the real pipeline, and it took 6.4 seconds and lost.

Why? The real workflow calls the function six times with small inputs. In every call, all 16 threads rebuilt the same tree. For one big call, building the tree was a small part. For six small calls, it was most of the time. A single big benchmark had hidden a fixed cost that is paid on every call.

The fix

We split the work in two. One thread builds the tree once, into buffers that numpy owns. All 16 runtimes then only read it. In the real pipeline, with receipts, the distance step fell from 2.87 s to 1.48 s, and the whole prompt from 31.18 s to 29.74 s.

That is a real win of about 1.4 seconds on a job of about 30. It is small. It also happens to be exact, where the old code was approximate. We do not call it a revolution.

What did not help

We also tried a few other ideas, and none paid off.

  • A graphics card port of ray tests for ambient occlusion matched the processor result and gave no speed-up.
  • Overlapping the card's work on one prop with the processor's work on the last one: 599 s in order against 608 s overlapped. The wall time did not fall.
  • Setting the thread count to 8 looked better in one run and was swamped by the real cause, a thread pool in a math library.

Rules we took from this

  • Measure in the real call pattern. A benchmark of one big call hides per-call costs.
  • Split building from querying. Build once, query many times.
  • Check exactness with brute force, not with a second approximation.
  • A port that saves 5 percent of a job needs a cause, a receipt and a check before it becomes a default.

Words you may not know

  • Heap: the pool of memory a program asks for while it runs.
  • Shared library: a file of compiled code that other programs can load.
  • C interface: the plain, old agreement that almost every language knows how to call.
  • Bounding box tree: a tree of boxes that lets you skip most of the triangles in a search.
  • Benchmark: a repeatable test of speed.
  • Profile: a record of where a program spends its time.

Try it

  1. Take a loop of your own that you call many times. Time one call and then 1,000 calls, and see whether the cost per call changes.
  2. Browse the mojo-libs table for the Mojo route before you reach for another language.
  3. Read the full engineering write-up of this job, including the tables and the receipts, linked below.

Comments

No comments yet.

Log in to comment.