# What is \`Core.Box()\`?

**URL:** <https://discourse.julialang.org/t/what-is-core-box/8717>\
**Category:** New to Julia\
**Created:** [January 31, 2018, 2:31pm UTC](https://discourse.julialang.org/t/what-is-core-box/8717 "2018-01-31T14:31:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [January 31, 2018, 2:31pm UTC](https://discourse.julialang.org/t/what-is-core-box/8717/1 "2018-01-31T14:31:44Z")

</div>

While debugging, this is something I see quite often. I didn’t find any trace in the docs and really curious about its function. Any pointer or hint is appreciated!

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [January 31, 2018, 3:51pm UTC](https://discourse.julialang.org/t/what-is-core-box/8717/2 "2018-01-31T15:51:14Z")

</div>

This has to do with ‘boxing’ data, which is e.g. required when the concrete type of some variable in a function body cannot be determined from the types of the inputs of a function ([type instability](https://docs.julialang.org/en/latest/manual/faq/#man-type-stability-1)). See [https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-changing-the-type-of-a-variable-1](https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-changing-the-type-of-a-variable-1) for an example (although `Core.Box` doesn’t actually show up in the lowered code here). In this case, the actual type of the data is only known at runtime, and so Julia has to store a type tag identifying the runtime type along with the actual data. Boxed variables are heap-allocated and tracked by the garbage collector.

Seeing `Core.Box` in the output of `code_warntype` is usually a sign that inference can’t determine the concrete type of some variable, and can be a source of performance issues.

Here’s a case where `Core.Box` shows up in lowered code in Julia 0.6.1 (from [https://github.com/JuliaLang/julia/issues/19668#issuecomment-268523270](https://github.com/JuliaLang/julia/issues/19668#issuecomment-268523270)):

```julia
julia> struct KWArg
           x::Int
           KWArg(;x::Int=1) = new(x)
       end

julia> kwarg(;x::Int=1) = x
kwarg (generic function with 1 method)

julia> @code_lowered KWArg()

CodeInfo(:(begin 
        nothing
        return ((Core.getfield)($(QuoteNode(Core.Box(#call#3))), :contents))(1, #self#)
    end))

```

(Note: I actually found it hard to find cases where an explicit `Core.Box` shows up in lowered code for functions on 0.6.1.)

This is where `Core.Box` is defined on current master: [https://github.com/JuliaLang/julia/blob/56b9593d8c25b839511e33e1fb06b58f95ce15df/base/boot.jl#L301-L305](https://github.com/JuliaLang/julia/blob/56b9593d8c25b839511e33e1fb06b58f95ce15df/base/boot.jl#L301-L305). Note the `Any` type of the contents, and the [`@nospecialize`](https://docs.julialang.org/en/latest/base/base/#Base.@nospecialize) in the constructor.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 31, 2018, 4:30pm UTC](https://discourse.julialang.org/t/what-is-core-box/8717/3 "2018-01-31T16:30:33Z")

</div>

No it’s totally unrelated to inference.

It’s added to the code when the frontend (parser) need a variable that can be mutated by multiple functions. (and in the case you show I’m 90% sure there was an issue for it). It does hurt inference but it is **NOT** caused by bad inference.

> [@chobbes](#):
>
> While debugging, this is something I see quite often. I didn’t find any trace in the docs and really curious about its function. Any pointer or hint is appreciated!

For the user, it’s basically the same as `Base.RefValue{Any}`. You should not use it which is why it’s not in the document.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [January 31, 2018, 4:41pm UTC](https://discourse.julialang.org/t/what-is-core-box/8717/4 "2018-01-31T16:41:55Z")

</div>

Thanks for clearing that up. I guess I was confusing the concept of boxing in general with `Core.Box`.

> [@yuyichao](#):
>
> and in the case you show I’m 90% sure there was an issue for it

Yeah, I linked to your issue comment.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [January 31, 2018, 6:17pm UTC](https://discourse.julialang.org/t/what-is-core-box/8717/5 "2018-01-31T18:17:49Z")

</div>

> [@yuyichao](#):
>
> For the user, it’s basically the same as Base.RefValue{Any}. You should not use it which is why it’s not in the document.

Thanks!

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [January 31, 2018, 6:18pm UTC](https://discourse.julialang.org/t/what-is-core-box/8717/6 "2018-01-31T18:18:19Z")

</div>

> [@tkoolen](#):
>
> Thanks for clearing that up. I guess I was confusing the concept of boxing in general with Core.Box.
> 
> and in the case you show I’m 90% sure there was an issue for it
> 
> Yeah, I linked to your issue comment.

Thanks too, buddy!
