CBinding.jl icon indicating copy to clipboard operation
CBinding.jl copied to clipboard

Long compilation time for large Carrays

Open jmark opened this issue 2 years ago • 2 comments

It takes a lot of time to instantiate a parsed struct with very large char arrays.

Minimal example

# example.jl

using CBinding;

c`
    -Wall
    -Werror
`

c"""
/* Default size on my system according to my <stdio.h>. */
# define BUFSIZ 8192

struct example_t {
    char dummy[BUFSIZ];
};
"""

example = c"struct example_t"r(dummy = "example");
0; # supress large output in the REPL

It takes nearly 3 Minutes and a lots of RAM (it seems):

julia> @time include("example.jl") # first call
165.834280 seconds (39.78 M allocations: 8.739 GiB, 1.16% gc time, 99.91% compilation time)
julia> @time include("example.jl") # second call
127.044148 seconds (2.04 M allocations: 136.764 MiB, 0.46% gc time, 99.47% compilation time)

jmark avatar May 04 '22 14:05 jmark

This is probably a result of the code emitted by CBinding being rather redundant and relying on the Julia compiler optimizations to remove the redundancies. As the example below illustrates, the first time construction is 99.9% compilation time, but successive calls are much better.

julia> module LargeArray
           using CBinding
           c``
           c"""
           typedef char LA[1024];
           """j
       end
Main.LargeArray

julia> @time LargeArray.LA("example"...);
  3.977991 seconds (2.29 M allocations: 211.621 MiB, 0.65% gc time, 99.92% compilation time)

julia> @time LargeArray.LA("example"...);
  0.000050 seconds (109 allocations: 20.781 KiB)

I will see about emitting more to-the-point code which doesn't use the compiler as a crutch. :wink:

krrutkow avatar May 06 '22 15:05 krrutkow

Thank you for your quick response! This a workaround a can work with for now! :-)

jmark avatar May 09 '22 07:05 jmark