I want to compile a function in a way to keep its memory footprint down. In the example below, I am trying to compile a function f
that makes three calls to bigNastyFunction
. I do not want to define bigNastyFunction
outside and use option "InlineExternalDefinitions" -> True
because then three copies of that function will be inserted into the body of the compiled function verbatim, leading to excessive memory usage. Instead, my strategy is to define bigNastyFunction
inside a Module
which is inside Compile
. My hope is that bigNastyFunction
will be appropriately stored as a subroutine, and called by the body as needed.
SetSystemOptions["CompileOptions" -> "CompileReportExternal" -> True];
f = Compile[{{x, _Complex}},
Module[{bigNastyFunction = Function[{y}, Sin[y]]},
bigNastyFunction[x] + bigNastyFunction[x^2]^2 + 3 bigNastyFunction[x^3]
]
]
The example above doesn't work because Function
is not one of the functions that can be compiled by Compile
. What workaround is there to define a large (complicated) function inside the body of a Compile
'd function?