You’re basically showing that the (outer) tuple is slow (I don’t think they are meant to be very large). If I only change it to an array, I get very different timings (better, still slow):
julia> function julia_code(N::Int)
io = IOBuffer()
println(io, "function get_it(x)")
print(io, "[")
for i in 1:N
print(io, "($i, x^7+x^6+x^5+x^4+x+1),")
end
print(io, "]")
println(io, "end")
return String(take!(io))
end
The timings (for get_it) are still superlinear:
julia> str=julia_code(1000) ; @time include_string(Main, str);
0.488692 seconds (66.11 k allocations: 3.558 MiB)
julia> @time include_string(Main, "get_it(2)");
3.671691 seconds (3.43 M allocations: 126.870 MiB, 2.41% gc time, 99.99% compilation time)
julia> str=julia_code(10000) ; @time include_string(Main, str);
4.960584 seconds (678.12 k allocations: 36.435 MiB, 2.10% gc time)
julia> @time include_string(Main, "get_it(2)");
224.110427 seconds (34.08 M allocations: 1.219 GiB, 0.72% gc time, 100.00% compilation time)
but this time, finally some response to lower opt level, with:
$ ~/julia-1.7.0/bin/julia -O0
julia> str=julia_code(10000) ; @time include_string(Main, str);
4.708956 seconds (678.12 k allocations: 36.435 MiB, 0.83% gc time)
julia> @time include_string(Main, "get_it(2)");
14.092745 seconds (34.08 M allocations: 1.219 GiB, 12.48% gc time, 99.99% compilation time)
14x slower (for 10x increase in length), but not 61x times slower, as with the default (implying -O2)
At least the parse time is linear.
If the INNER tuples get changed to arrays too, then this gets really slow, even for smaller n:
julia> str=julia_code(100) ; @time include_string(Main, str);
0.067056 seconds (7.62 k allocations: 458.243 KiB, 8.64% compilation time)
julia> @time include_string(Main, "get_it(2)");
0.585371 seconds (1.09 M allocations: 51.450 MiB, 1.59% gc time, 99.93% compilation time)
for:
$ ~/julia-1.7.0/bin/julia -O0
julia> str=julia_code(100) ; @time include_string(Main, str);
0.057833 seconds (7.62 k allocations: 458.243 KiB, 3.16% compilation time)
julia> @time include_string(Main, "get_it(2)");
0.382963 seconds (1.09 M allocations: 51.450 MiB, 99.89% compilation time)
julia> str=julia_code(1000) ; @time include_string(Main, str);
0.500095 seconds (66.11 k allocations: 3.560 MiB)
julia> @time include_string(Main, "get_it(2)");
45.751689 seconds (12.06 M allocations: 558.189 MiB, 0.75% gc time, 100.00% compilation time)