⚡ LoopBench Optimizer

Introducing LoopBench Optimizer: an evaluator-gated loop that makes your code faster — and proves it

by manashatwar · GitHub · 4 min read

Today I'm open sourcing LoopBench Optimizer: an autonomous optimization harness powered by coding LLMs. You point it at a repo and a file, and it evolves that code to run faster — while proving, every step of the way, that the change is both correct and measurably better.

You can run LoopBench on your laptop. There's no cloud service to set up and no privileged access to grant. For inference it talks to any OpenAI-compatible endpoint, so you can use Groq, Google Gemini, OpenAI, or a local model — including free tiers. Every candidate is executed in a locked-down Docker sandbox (--network=none, read-only mount), so untrusted, LLM-generated code never touches your machine or the network.

The whole run is bounded: LoopBench stops on a token, dollar, runtime, or iteration budget, so an experiment can't quietly run up a bill.

Architecture

At its core, LoopBench runs an LLM in a closed feedback loop and treats the LLM as an architect — it rewrites the logic, and the system turns that into a guaranteed-valid patch. Every candidate is created, isolated, tested, scored, and recorded before the loop decides what to try next.

The cycle each generation:

The key design decision: LLMs are brilliant at logic and terrible at being diff engines. Asking them for byte-exact unified diffs produces corrupt patches 20–30% of the time. LoopBench sidesteps that entirely — the model rewrites logic, difflib produces the patch, and the loop becomes unbreakable.

Running LoopBench on real code

LoopBench has been run against real, external repositories, not just toy fixtures.

Pointed at a classic O(n²) bubble sort using a free model (Groq's llama-3.3-70b), it took the sort from 443 ms to 0.18 ms — over 2,000× faster — by autonomously replacing the whole algorithm with an O(n log n) sort, with every correctness test still green inside the sandbox. On real numeric code, the same loop found a +38% NumPy vectorization of a Python loop.

The result I care about most is a negative one. Pointed at a Vyper smart contract that had no real optimization headroom, LoopBench reported +0.00% and kept the original. It refuses to ship a change it can't prove is better. That's the whole philosophy: a scientist, not a cheerleader. If a candidate can't demonstrate it's faster and still correct, it never ships — full stop.

Where it works, and where it doesn't

LoopBench works best when three things are true: the target has a measurable metric (latency, gas, throughput, memory), a correctness gate (tests, or an exit code), and a pip-installable toolchain. That covers Python and Python-hosted ecosystems like Vyper cleanly.

It's not a fit for code with no measurable signal — UI polish, prose, or logic with no tests to anchor correctness. And result quality tracks model quality: a stronger model finds more aggressive, correct rewrites; a weaker one plays it safe. Because the loop only keeps verified wins, a poor fit shows up honestly as a small or zero improvement rather than a broken patch.

Defining what "better" means

You tell LoopBench what to optimize with a small evaluator — your test file is the benchmark. It asserts correctness and prints a speed marker (LOOPBENCH_SPEED_MS), or you extract a custom metric (gas, throughput, memory) with a regex and optimize that instead. There's no separate scoring engine to learn; if your tests pass and your number improves, the patch is kept.

Do I need a frontier model?

No. LoopBench is provider-agnostic and works with off-the-shelf and free models — the 2,000× bubble-sort result above was produced by a free Groq model. Stronger models simply find bolder rewrites. You set LLM_API_BASE and LLM_MODEL and the loop does the rest.

Getting started

Install, add your key, and scaffold a job:

pip install -e .
cp .env.example .env        # add any OpenAI-compatible key (Groq, Gemini, OpenAI)

# Optimize a file that already has a timing test:
loopbench run --target . --target-file src/hotpath.py --metric latency

# Or scaffold a job to optimize someone else's repo:
loopbench init --job my_job                 # creates loopbench.yaml + test_target.py
loopbench run --config my_job/loopbench.yaml

Docker Desktop must be running — that's where the verification happens. The full 5-minute quick start is in the README.

Feedback welcome

LoopBench Optimizer is built on the excellent OpenEvolve project and is still early. Issues, ideas, and contributions on GitHub are welcome.

Star it on GitHub →