gridap / gridap/Gridap.jl

gather_free_values! and gather_dirichlet_values! waste memory on unnecessary allocations

Open
#1,253 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
879
Forks
119
Avg merge
6d 3h
Merged PRs (30d)
4

Description

## Summary

I noticed that `gather_free_values!` and `gather_dirichlet_values!` are allocating unnecessary temporary vectors in the `UnconstrainedFESpace` implementation.

## Problem

Both functions delegate to `gather_free_and_dirichlet_values!`, which requires two output buffers. Since each function only needs one output, the other is allocated and immediately discarded:

- `gather_free_values!` allocates a throwaway `dirichlet_values` vector (~736 bytes on a 20×20 mesh)
- `gather_dirichlet_values!` allocates a throwaway `free_values` vector (~3 KB on a 20×20 mesh, **82 KB on a 100×100 mesh**)

The `gather_dirichlet_values!` waste is particularly bad because it scales linearly with the free DOF count.

## Impact

I'm working on a neural operator reduced-order model that calls `interpolate!` thousands of times in parameter sweeps. On a typical workflow with 10,000 samples:

- **20×20 mesh**: ~187 MB wasted
- **100×100 mesh**: ~780 MB wasted

This creates unnecessary GC pressure during snapshot generation.

## Measurements

| Function | Mesh Size | Current Allocation |
|-----------------------------|-----------|-------------------|
| `gather_free_values!` | 20×20 | 4,320 bytes |
| `gather_dirichlet_values!` | 20×20 | 6,624 bytes |
| `gather_dirichlet_values!` | 100×100 | 82,064 bytes |

Contributor guide

Open the contributing guide

Research direction

Search the UnconstrainedFESpace implementation for gather_free_values!, gather_dirichlet_values!, and gather_free_and_dirichlet_values!. First inspect how the two public functions allocate their output buffers, then verify that each only allocates the values it returns. Use the allocation measurements in the issue to confirm the unnecessary temporary vectors are gone.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.