top | item 30344418

(no title)

krumbie | 4 years ago

I don't think it's frowned upon to compile, many people want this capability as well. If you had a program that could be proven to use no dynamic dispatch it would probably be feasible to compile it as a static binary. But as long as you have a tiny bit of dynamic behavior, you need the Julia runtime so currently a binary will be very large, with lots of theoretically unnecessary libraries bundled into it. There are already efforts like GPUCompiler[1] that do fixed-type compilation, there will be more in this space in the future.

[1] https://github.com/JuliaGPU/GPUCompiler.jl

discuss

order

cbkeller|4 years ago

Yeah, not frowned upon at all. StaticCompiler.jl [1] has made huge strides in the past few weeks actually thanks to Mason and Valentin (swapping in GPUCompiler as the backend); you can even compile to a tiny standalone binary without linking to the runtime if you're willing to use some tricks e.g. [2] to avoid GC allocations (stack allocations and manual heap allocations are both fine):

    # This is all StaticCompiler-friendly
    using StaticTools

    function print_args(argc::Int, argv::Ptr{Ptr{UInt8}})
        # c"..." lets you construct statically-sized, stack allocated `StaticString`s
        # We also have m"..." and MallocString if you want the same thing but on the heap
        printf(c"Argument count is %d:\n", argc)
        for i=1:argc
            # iᵗʰ input argument string
            pᵢ = unsafe_load(argv, i) # Get pointer
            strᵢ = MallocString(pᵢ) # Can wrap to get high-level interface
            println(strᵢ)
            # No need to `free` since we didn't allocate this memory
        end
        println(c"That was fun, see you next time!")
        return 0
    end

    # Compile executable
    using StaticCompiler # `] add https://github.com/tshort/StaticCompiler.jl` to get latest master
    filepath = compile_executable(print_args, (Int64, Ptr{Ptr{UInt8}}), "./")
yielding:

    shell> ./print_args 1 2 3 4 5.0 foo
    Argument count is 7:
    ./print_args
    1
    2
    3
    4
    5.0
    foo
    That was fun, see you next time!

    shell> hyperfine './print_args hello there'
    Benchmark 1: ./print_args hello there
      Time (mean ± σ):       2.2 ms ±   0.5 ms    [User: 0.8 ms, System: 0.0 ms]
      Range (min … max):     1.5 ms …   5.5 ms    564 runs

      Warning: Command took less than 5 ms to complete. Results might be inaccurate.

    shell> ls -lh print_args
      -rwxr-xr-x  1 user  staff   8.5K Feb 10 02:36 print_args

GC allocations are allowed if you use instead the approach in this PR [3], but we haven't wrangled that approach to produce standalone binaries yet.

[1] https://github.com/tshort/StaticCompiler.jl

[2] https://github.com/brenhinkeller/StaticTools.jl

[3] https://github.com/tshort/StaticCompiler.jl/pull/58