My code, after compiled with PackageCompiler
, show some memory allocation in logging functions. With the help of --trace-compile
, I’ve found that when the compiled binary is running, the following functions are precompiled:
- Printf.fmt
- Printf.plength
- Printf.format
- Printf.computelen
- Dates.format
- Dates.DateFormat
- one custom function, which is just
function my_printf!(io, format::Printf.Format, args...)
pos = Printf.format(LOG_DATA_BUFFER, 1, format, args...)
GC.@preserve LOG_DATA_BUFFER unsafe_write(io, pointer(LOG_DATA_BUFFER), pos - 1)
return
end
where
const LOG_DATA_BUFFER = Base.StringVector(4096)
However, when compiling the binary, I used the precompile_execution_file
argument, and the exactly same function with exactly same input is called in the file as the compiled binary.
If all functions are specialized, I think they should all be compiled into the binary, and there should be no precompiling when the binary is executed? So, the problem should be, that the aformentioned functions are not specialized?
However, after checking the source code, I find that the @nospecialize
macro is not used in Prinf
, so why will some functions be not specialized?
I’ve found some discussion, like this and this, on this topic. But I have to confess this is too hard for me to understand.
Let’s put the question clear: what should I do to make the compiled binary not precompiling these functions when executing? Is the precompiling really the reason for the allocation?
I’ve already made a custom MyLogger <: AbstractLogger
, and implemented the Logging.handle_message
method for it, in which I basicly copied the implementation of ConsoleLogger
, but removed the @nospecialize
macro.