But even the first run should not allocate an array of the full size.
Everything is done in-place.
See this, that works but is doing the same!
julia> capnan0!(A,B,v) = B[A .> v] .= NaN
capnan0! (generic function with 2 methods)
julia> A = randn((128, 512, 256));
julia> B = copy(A);
julia> capnan0!(A, B, 100);
julia> @time capnan0!(A, B, 100); A .= B; @time capnan0!(A, B, 100); A .= B; @time capnan0!(A, B, 100); #
0.018046 seconds (8 allocations: 2.004 MiB)
0.014085 seconds (8 allocations: 2.004 MiB)
0.014415 seconds (8 allocations: 2.004 MiB)
There are some aliasing bugs which might occur also here?
EDIT: The mechanism maybe works in the second run since we do no assignments then.