Why is plotting with GR faster when using GR_jll and `@ccall` directly

GR.__init__ changes Environmental Variables

I took a look at GR.jl’s __init__ and I noticed it set some environmental variables:

julia> orgENV= copy(ENV);

julia> using GR

julia> setdiff(ENV,orgENV)
4-element Vector{Pair{String, String}}:
      "GKS_ENCODING" => "utf8"
      "GKS_FONTPATH" => "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
 "GKS_USE_CAIRO_PNG" => "true"
             "GRDIR" => "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"

Setting environmental variable results in slowdown

julia> begin
           ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_FONTPATH"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_ENCODING"] = "utf8"
           ENV["GKS_USE_CAIRO_PNG"] = "true"
       end
"true"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.741127 seconds (292.18 k allocations: 19.435 MiB, 0.92% gc time, 0.46% compilation time)
  2.322584 seconds (3.92 k allocations: 223.016 KiB, 0.46% compilation time)
  0.012314 seconds (3.53 k allocations: 203.047 KiB, 99.16% compilation time)
  0.000056 seconds (6 allocations: 192 bytes)
  3.149256 seconds (416.77 k allocations: 27.419 MiB, 0.22% gc time, 0.83% compilation time)

I actually get a blank plot at the end of this, but now it takes more than two seconds to call gr.gr_polyline.

Setting ENV[“GRDIR”] is sufficient to cause the slowdown

julia> begin
           ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
       end
"C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.678470 seconds (292.17 k allocations: 19.434 MiB, 0.61% compilation time)
  2.325419 seconds (3.92 k allocations: 223.016 KiB, 0.45% compilation time)
  0.025172 seconds (3.53 k allocations: 203.047 KiB, 99.14% compilation time)
  0.000110 seconds (6 allocations: 192 bytes)
  3.106615 seconds (416.75 k allocations: 27.301 MiB, 1.28% compilation time)

Setting other environmental variables does not result in a slowdown

julia> begin
           #ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_FONTPATH"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_ENCODING"] = "utf8"
           ENV["GKS_USE_CAIRO_PNG"] = "true"
       end
"true"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.672858 seconds (292.17 k allocations: 19.434 MiB, 0.46% compilation time)
  0.027195 seconds (3.92 k allocations: 223.016 KiB, 38.21% compilation time)
  0.014427 seconds (3.53 k allocations: 203.047 KiB, 57.92% compilation time)
  0.000004 seconds (6 allocations: 192 bytes)
  0.788307 seconds (416.75 k allocations: 27.350 MiB, 2.77% compilation time)

Package versions

Also here are the versions I am using:

(@v1.6) pkg> st GR
      Status `C:\Users\kittisopikulm\.julia\environments\v1.6\Project.toml`
  [28b8d3ca] GR v0.55.0

(@v1.6) pkg> st GR_jll
      Status `C:\Users\kittisopikulm\.julia\environments\v1.6\Project.toml`
  [d2c73de3] GR_jll v0.56.1+0