Skip to content

Loading…

module init functions #1268

Closed
JeffBezanson opened this Issue · 7 comments

3 participants

@JeffBezanson
The Julia Language member

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.

@JeffBezanson JeffBezanson was assigned
@timholy
The Julia Language member

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!

@StefanKarpinski
The Julia Language member

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?

@timholy
The Julia Language member

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.

@StefanKarpinski
The Julia Language member

Ah, yes, I see. That makes perfect sense. We should definitely support that.

@JeffBezanson JeffBezanson modified the milestone: 0.3, 1.0
@JeffBezanson
The Julia Language member

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

  1. Specifying how to initialize a particular module (the original issue here)
  2. Rehashing ObjectIdDicts (currently a special case)
  3. Restoring native pointers for e.g. Regex
@timholy
The Julia Language member

Under what other circumstances would you have to attach initializers to objects besides module initialization?

@JeffBezanson
The Julia Language member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.