I am creating some numerical methods, and I am running into some issues with closures.

Here’s an example of what I’m doing:

Function to drive myPkg:

```
function main()
#allocate temporaries
function linear()
# Use temps and do stuff
end
function nonlinear()
# Use temps and do stuff
end
myPkg.solver(retVal, linear, nonlinear, otherParams)
end
```

Function inside my module:

```
function solver(retval, linear, nonlinear, otherParams)
for i = 1:N
linear(retVal, otherParams)
nonlinear(retVal, otherParams)
end
return retVal
end
```

My problem is that an excessive amount of allocations take place and the function is over ten times slower than if I encapsulate the functions inside of the module. I’m not certain if my concept of a closure is completely wrong, or if the way I have it causes additional temporaries to be created that I don’t see.

If you want a more concrete example checkout my GitHub for comparison of the module that encapsulates the functions (https://github.com/cpross90/BenjaminOnoSolver.jl) and the module that accepts external functions (https://github.com/cpross90/SplitStep.jl). I’m looking to have the module work similarly to how the Optim.jl package receives objective functions for a cost function/gradient/hessian.

If there are any suggestions to how I can tackle this better I am all ears. Just an undergraduate applied math researcher trying to get a hold of Julia.

Thanks!