How do we exec a julia program for competitive programming?

I know this is a very old post, but I’ve been interested in using Julia for competitive programming too recently, and I thought I’d share some thoughts I had regarding how to go about doing this. I tested Julia on AtCoder earlier today and the overhead is between 1 and 2 seconds for even simple code, which is far too high to be usable with a 2s time limit, so precompilation does seem like the only way forward. (A quick disclaimer: I’m fairly new to Julia, and some of the things I say might be incorrect and/or not make sense.)

Having skimmed the PackageCompiler manual, the basic idea we want to use is probably something like the following.

I haven’t tested this myself yet, but it will probably make most cases significantly faster, provided that your code is nice enough (e.g. types not dependent on input).

Obviously, unlike the modules that PackageCompiler would usually be used with, we are dealing with standalone programs. Using the program directly would result in it being run, so people would have to write their solution body within a main() function. With regards to global scope, my understanding is that any variables there would be dirtied in the sysimage by already having had computations performed on them in main() once, which could sometimes be annoying to deal with. Putting any computations at global scope would also allow a contestant to do precomputations at “compile time”, effectively doubling the time limit in some cases. You can deal with this by forcing everything to be in a local scope, but that starts to get restrictive.

One potential alternative that doesn’t require the contestant to do anything differently is to generate a sysimage using only the precompile statements for functions in Base and the other packages supported by AtCoder, and without the module in it. The problem is that this doesn’t deal with things like parsing and type inference for the contestant’s code at all, which seem to me like they would probably take up a significant amount of time, so I’m not sure how useful this would be; nevertheless I can test it if you’re interested.

An ideal solution would probably involve being able to force the computations at global scope to get re-run in some sense (to both clean up the residual sysimage state and prevent precomputation at compile time from being effective). Let me know if you have any ideas about how it might be possible to achieve this, or if anything here needs further explanation / is insane.

2 Likes