It is common for C libraries to require some init call on startup, like Tk_Init. It would be nice to know about modules that need to call such functions, and hook them into julia's _init in the future when julia code is used as a library.
Currently we have 2 overall phases: definition time (when a source file is loaded) and run time. We need to insert "startup time" in between. It takes place right after load time when you first load a file, and also at startup when "compiled" code is loaded.
Much less important than module initialization, but possibly worth including in the thought process here: an optional thunk for forced-precompilation. See https://github.com/timholy/Images.jl/blob/2d51289829a74aeeaa18bd93e3532f6fe32eb5a0/src/Images.jl#L24-L49
I was able to reduce the time to load Images and run the algorithms.jl test to about 40% of its former value, yay!
So how does that help the load time? Wouldn't it just shift around when the compilation happens? Why is it better to do all the precompilation up front than on demand?
It doesn't change the load time (using Images is now blazingly fast either way), but if I put Images.precompile() into my userimg.jl, then those functions get precompiled when I build Julia. So it increases the amount of time needed to build Julia, but it makes it faster to start computing things on each launch. Assuming that building Julia is less frequent than using Julia, it's a win.
Ah, yes, I see. That makes perfect sense. We should definitely support that.
My straw man proposal here would be to call a function named _init() in a module, if it exists, when the module is loaded. However, I immediately wonder if this should be more general, and let you attach initializers (somewhat like finalizers) to objects or types. This could be used for
Under what other circumstances would you have to attach initializers to objects besides module initialization?
Yeah, we might be better off not over-engineering this and just adding simple module init functions. After all you can use those to set up arbitrary data structures anyway.
call _init() in modules at init time. closes #1268