Skip to content

Commit e0b4db2

Browse files
Add comprehensive docstrings for cache functions
- Added detailed docstring for get_tmp explaining dual number handling - Added docstring for enlargediffcache! explaining resizing behavior - Improved documentation for automatic differentiation support
1 parent 0b48f9c commit e0b4db2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/PreallocationTools.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ chunksize(::Type{T}) where {T} = 0
4444

4545
# ForwardDiff-specific methods moved to extension
4646

47+
"""
48+
get_tmp(dc::FixedSizeDiffCache, u::Union{Number, AbstractArray})
49+
50+
Returns the appropriate cache array from the `FixedSizeDiffCache` based on the type of `u`.
51+
52+
If `u` is a regular array or number, returns the standard cache `dc.du`. If `u` contains
53+
dual numbers (e.g., from ForwardDiff.jl), returns the dual cache array. The function
54+
automatically handles type promotion and resizing of internal caches as needed.
55+
56+
This function enables seamless switching between regular and automatic differentiation
57+
computations without manual cache management.
58+
"""
4759
function get_tmp(dc::FixedSizeDiffCache, u::Union{Number, AbstractArray})
4860
if promote_type(eltype(dc.du), eltype(u)) <: eltype(dc.du)
4961
dc.du
@@ -148,6 +160,25 @@ function _restructure(normal_cache::AbstractArray, duals)
148160
ArrayInterface.restructure(normal_cache, duals)
149161
end
150162

163+
"""
164+
enlargediffcache!(dc::DiffCache, nelem::Integer)
165+
166+
Enlarges the dual cache array in a `DiffCache` when it's found to be too small.
167+
168+
This function is called internally when automatic differentiation requires a larger
169+
dual cache than initially allocated. It resizes `dc.dual_du` to accommodate `nelem`
170+
elements and issues a one-time warning suggesting an appropriate chunk size for
171+
optimal performance.
172+
173+
## Arguments
174+
- `dc`: The `DiffCache` object to enlarge
175+
- `nelem`: The new required number of elements
176+
177+
## Notes
178+
The warning is shown only once per `DiffCache` instance to avoid spam. For optimal
179+
performance in production code, pre-allocate with the suggested chunk size to avoid
180+
runtime allocations.
181+
"""
151182
function enlargediffcache!(dc, nelem) #warning comes only once per DiffCache.
152183
chunksize = div(nelem, length(dc.du)) - 1
153184
@warn "The supplied DiffCache was too small and was enlarged. This incurs allocations

0 commit comments

Comments
 (0)