# Package compiler and module global variable

**URL:** <https://discourse.julialang.org/t/package-compiler-and-module-global-variable/99585>\
**Category:** General Usage\
**Tags:** package-compiler\
**Created:** [May 30, 2023, 5:06am UTC](https://discourse.julialang.org/t/package-compiler-and-module-global-variable/99585 "2023-05-30T05:06:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [May 30, 2023, 5:06am UTC](https://discourse.julialang.org/t/package-compiler-and-module-global-variable/99585/1 "2023-05-30T05:06:29Z")

</div>

```julia
module JModel
mutable struct JStruct
    path::String
   # other fields
end
jstruct = JStruct("")
Base.@ccallable function init_model(conf_path::Cstring)::Cint
    global jstruct
    jstruct = JStruct(unsafe_string(conf_path))
    return 0
end
Base.@ccallable function proc(evt::Ptr{Culong} )::Cint
    println("jstruct: ", jstruct.path)
    println("evt: $(unsafe_load(evt,1))")
    return 0
end
 
end # module JModel

```

Say I have to init a Julia struct from C passed in string and do a frequent `proc` call from C. Does using something like this global variable have a performance cost ? What’ s the Idiom way of doing something like this?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 30, 2023, 5:18am UTC](https://discourse.julialang.org/t/package-compiler-and-module-global-variable/99585/2 "2023-05-30T05:18:14Z")

</div>

One important point to avoid issues is to either make the global `const` or add a type binding to it.

Here we use a const `jstruct` and we just mutate the field since your struct is mutable. The `const` just means we cannot assign a new value to `jstruct`. An important consequence for this for the compiler is that we know that the type of `jstruct` will not change. We can assume it will be of type `JStruct`.

```julia
const jstruct = JStruct("")
Base.@ccallable function init_model(conf_path::Cstring)::Cint
    global jstruct
    jstruct.path = unsafe_string(conf_path)
    return 0
end

```

Another way to do this is to add a type binding.

```julia
jstruct::JStruct = JStruct("")
Base.@ccallable function init_model(conf_path::Cstring)::Cint
    global jstruct
    jstruct = JStruct(unsafe_string(conf_path))
    return 0
end

```

You could use a `Ref`.

```julia
const jstruct_ref = Ref(JStruct(""))
Base.@ccallable function init_model(conf_path::Cstring)::Cint
    global jstruct
    jstruct[] = JStruct(unsafe_string(conf_path))
    return 0
end
Base.@ccallable function proc(evt::Ptr{Culong} )::Cint
    println("jstruct: ", jstruct[].path)
    println("evt: $(unsafe_load(evt,1))")
    return 0
end

```
