Skip to content

Cut the allocations on the calls that go inside a loop - #301

Merged
filoferra merged 2 commits into
JuliaParallel:mainfrom
filoferra:perf-hot-paths
Sep 29, 2026
Merged

filoferra merged 2 commits into
JuliaParallel:mainfrom
filoferra:perf-hot-paths

Conversation

@filoferra

Copy link
Copy Markdown
Collaborator

Checking arrays out of a Vec. with_local_array! cost 23 allocations and inferred Any: it asked each vector for its PetscMemType with a checkout of its own before checking the vectors out for real, and memtype_backend dispatched on a Val built from that run-time value. Host memory now answers before the dispatch and the extra query is gone. Devices dispatch as before, so the CUDA extension is untouched.

Out-of-place broadcast. w = 2 .* x read one entry at a time through VecGetValues. A PetscVecStyle style puts copy on the same local-array path copyto! already took. It still returns a Vector; a distributed one now throws an ArgumentError naming the in-place form, where before it died with a raw PETSc error.

call before after
with_local_array!, 1 vector 23, Any 2, inferred
with_field_views! 24, Any 8, inferred
SNES solve, 3 Newton steps 210 47
2 .* x, 10 000 entries 4003 5
v[i] = a 4 0

with_local_array! queried each vector's PetscMemType with a checkout of its own before checking the vectors out for real, memtype_backend dispatched on a Val built from that run-time value, and read/write changed type mid-function.
Host memory now answers before the dispatch, the extra query is gone, and a one-vector checkout costs 2 allocations (the unsafe_wrap, the floor) and infers, from 23 and Any.
with_field_views! goes 24 to 8, a 3-step SNES solve 210 to 47, and v[i] = a to zero. Devices dispatch as before.
@filoferra
filoferra merged commit 6aa1a66 into JuliaParallel:main Sep 29, 2026
5 checks passed
@filoferra
filoferra deleted the perf-hot-paths branch September 29, 2026 10:14
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