shiv chawla
shiv chawla

Reputation: 608

First call to Julia is slow

This questions deals with first load performance of Julia

I am running a Julia program from command line. The program is intended to be an API where user of the API doesn't have to initialize internal objects.

module Internal 
   type X
     c::T1
     d::T2
     .
     ..
   end

   function do_something(a::X, arg:Any)
     #some function
   end
   export do_something
end

API.jl

using Internal

const i_of_X = X()

function wrapper_do_something(args::Any)
     do_something(i_of_X, args)
end

Now, this API.jl is exposed to third party user so that they don't have to bother about instantiating the internal objects. However, API.jl is not a module and hence cannot be precompiled. As there are many functions in API.jl, the first load takes a "very" long time.

Is there a way to improve the performance? I tried wrapping API.jl in a module too but I don't know if wrapping const initialized variables in a module is the way to go. I also get segmentation fault on doing so (some of the const are database connections and database collections along with other complex objects).

[EDIT] I did wrap API.jl in a module but there is no performance improvement.

Is there a way to speed these up? Is there a way to fully precompile the julia program?

For a little more background, API based program is called once via command-line and any persistent first compilation advantages are lost as command-line closes the Julia process.

$julia run_api_based_main_func.jl   

One hacky way to use the compilation benefits is to somehow copy/paste the code in already active julia process. Is this doable/recommended?(I am desperate to make it fast. Waiting 15-20s for a 2s analysis doesn't seem right)

Upvotes: 2

Views: 542

Answers (1)

Jeffrey Sarnoff
Jeffrey Sarnoff

Reputation: 1757

It is OK to wrap const values in a module. They can be exported, as need be.

As Fengyang said, wrapping independent components of a larger design in modules is helpful and will help in this situation. When there is a great deal going on inside a module, the precompile time that accompanies each initial function call can add up. There is a way to avoid that -- precompile the contents before using the module:

__precompile__(true)

module ModuleName
# ...
end # module ModuleName

Please Note (from the online help):

__precompile__() should not be used in a module unless all of its dependencies are also using __precompile__(). Failure to do so can result in a runtime error when loading the module.

Upvotes: 4

Related Questions