Repository navigation
feat: Workspace, an arena for the results of array operations in loops - #12
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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(xy+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