Skip to content

feat: Workspace, an arena for the results of array operations in loops - #12

Merged
tannevaled merged 1 commit into
mainfrom
feat/workspace
Oct 4, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/workspace

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Every operation returns a new array; in a loop those are garbage the
collector must chase, while NumPy frees its temporaries the moment their
reference count drops. On a Zen 3 (16 cores) that made x + y lose to NumPy
0.13-0.25x at every size up to 256 Ki elements, although AddInto wins.
Measured on M4: a GC cycle every ~22 calls on a benchmark's tiny heap, and
with a realistic 256 MiB-2 GiB live heap still half the overhead (zeroing
memory the kernel overwrites, cold memory). GOGC=off only trades the cycles
for page faults, and a library must not set GOGC anyway.

Workspace is ND4J's answer to the same problem: ws.Use(x) binds an input,
every result computed from a bound array is carved from the arena (and
stays bound, so a chain stays in it), and ws.Reset() recycles the pass. A
pass that needed several blocks is folded into one, so after the first pass
nothing is allocated. Sites whose kernel writes every element skip the
zeroing; those that accumulate (GEMM, vec.mat, axis reductions) clear their
slot. Detach takes a result out. Allocation is serialized, so a workspace
can be shared between goroutines. Without Use nothing changes: a nil
workspace allocates from the heap as before.

Zen 3, 16 cores pinned, vs NumPy 2.5.3 single-threaded:

x + y 1 024 2.6 us -> 0.38 us (2.1x NumPy)
x + y 256 Ki 498 us -> 67 us (2.2x NumPy)
sqrt(xy+x) 1 024 12.2 us -> 2.6 us (1.4x NumPy)
sqrt(x
y+x) 256 Ki 1.18 ms -> 0.39 ms (7.5x NumPy)

Tests: every allocating operation is compared bit for bit with its heap
result, also on passes whose recycled memory was first filled with NaN
(planting a missing zeroing in the GEMM makes it fail); block folding,
alignment and capacity, Detach surviving Reset, and concurrent allocation
under -race (removing the lock makes it fail). ExampleWorkspace runs the
README's loop.

🤖 Generated with Claude Code

Every operation returns a new array; in a loop those are garbage the
collector must chase, while NumPy frees its temporaries the moment their
reference count drops. On a Zen 3 (16 cores) that made x + y lose to NumPy
0.13-0.25x at every size up to 256 Ki elements, although AddInto wins.
Measured on M4: a GC cycle every ~22 calls on a benchmark's tiny heap, and
with a realistic 256 MiB-2 GiB live heap still half the overhead (zeroing
memory the kernel overwrites, cold memory). GOGC=off only trades the cycles
for page faults, and a library must not set GOGC anyway.

Workspace is ND4J's answer to the same problem: ws.Use(x) binds an input,
every result computed from a bound array is carved from the arena (and
stays bound, so a chain stays in it), and ws.Reset() recycles the pass. A
pass that needed several blocks is folded into one, so after the first pass
nothing is allocated. Sites whose kernel writes every element skip the
zeroing; those that accumulate (GEMM, vec.mat, axis reductions) clear their
slot. Detach takes a result out. Allocation is serialized, so a workspace
can be shared between goroutines. Without Use nothing changes: a nil
workspace allocates from the heap as before.

Zen 3, 16 cores pinned, vs NumPy 2.5.3 single-threaded:

  x + y        1 024   2.6 us -> 0.38 us   (2.1x NumPy)
  x + y       256 Ki   498 us -> 67 us     (2.2x NumPy)
  sqrt(x*y+x) 1 024   12.2 us -> 2.6 us    (1.4x NumPy)
  sqrt(x*y+x) 256 Ki  1.18 ms -> 0.39 ms   (7.5x NumPy)

Tests: every allocating operation is compared bit for bit with its heap
result, also on passes whose recycled memory was first filled with NaN
(planting a missing zeroing in the GEMM makes it fail); block folding,
alignment and capacity, Detach surviving Reset, and concurrent allocation
under -race (removing the lock makes it fail). ExampleWorkspace runs the
README's loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit c67dabd into main Oct 4, 2026
12 checks passed
@tannevaled
tannevaled deleted the feat/workspace branch October 4, 2026 13:19
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.

1 participant