Skip to content

circuit: reuse garble scratch buffers to cut allocations#37

Merged
markkurossi merged 1 commit into
markkurossi:masterfrom
Scratch-net:garble-scratch-reuse
Jun 16, 2026
Merged

circuit: reuse garble scratch buffers to cut allocations#37
markkurossi merged 1 commit into
markkurossi:masterfrom
Scratch-net:garble-scratch-reuse

Conversation

@Scratch-net

Copy link
Copy Markdown
Contributor

Circuit.Garble allocates a fresh []ot.Label for every gate's garbled
table, plus the wire and gate slices, on each call. For a large circuit
that's tens of thousands of allocations per garbling, which turns into real
GC pressure when garbling many circuits or the same circuit repeatedly.

This carves the per-gate tables out of a single backing slab, and adds an
optional Garbled.Release() that returns the scratch to a sync.Pool for
reuse. The pool is stored on the Circuit, so it lives and dies with it.

Garble's signature is unchanged and Release() is purely additive —
existing callers keep working, and skipping Release() just forgoes the
reuse with no behavioural change.

Benchmark

10,000-gate AND chain (BenchmarkGarble, included):

allocs/op B/op ns/op
before 10,008 1,214 KB 1,147µs
slab only (no Release) 11 903 KB 1,015µs
with Release (reuse) 6 18.7 KB 927µs

BenchmarkGarble calls Garble without Release, so it runs on the current
code as well — the allocation drop is reproducible directly by checking out
this branch's parent. BenchmarkGarbleReuse shows the steady-state pooled
path.

Garble allocated a fresh []ot.Label per gate table plus the wire and gate
slices on every call, so a large circuit cost tens of thousands of
allocations per garbling. Carve the per-gate tables from a single slab and
add an optional Garbled.Release() that returns the scratch to a per-circuit
sync.Pool for reuse.

The Garble signature is unchanged and Release is additive: skipping it just
forgoes reuse, with no behaviour change.

BenchmarkGarble (10k-gate AND chain): 10008 -> 11 allocs/op.
@markkurossi markkurossi merged commit 9c484ce into markkurossi:master Jun 16, 2026
2 checks passed
@markkurossi

Copy link
Copy Markdown
Owner

Thanks for the patch, merged it to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants