# Can @inline works when the inlined function is a field of a struct?

**URL:** <https://discourse.julialang.org/t/can-inline-works-when-the-inlined-function-is-a-field-of-a-struct/77904>\
**Category:** New to Julia\
**Created:** [March 15, 2022, 7:45am UTC](https://discourse.julialang.org/t/can-inline-works-when-the-inlined-function-is-a-field-of-a-struct/77904 "2022-03-15T07:45:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 15, 2022, 7:45am UTC](https://discourse.julialang.org/t/can-inline-works-when-the-inlined-function-is-a-field-of-a-struct/77904/1 "2022-03-15T07:45:35Z")

</div>

```julia
struct Cogg
    x::Function
end

fin(data::AbstractArray)=begin
    df=data .* 2
    @inline (i::Int)->begin
        #assume this body of function can not be inlined by julia automatically
        df[i]>0
    end
end

(cogg::Cogg)(n::Int)=begin
    x=rand(n)
    func=cogg.x
    for i=1:n
        func(i)
    end
end

cogg=Cogg(fin)
cogg(100)

```

Does the cogg.x is inlined in the loop body?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [March 15, 2022, 9:04am UTC](https://discourse.julialang.org/t/can-inline-works-when-the-inlined-function-is-a-field-of-a-struct/77904/3 "2022-03-15T09:04:17Z")

</div>

You would need to define

```julia
struct Cogg{F}
    x::F
end

```

so that the type of `cogg.x` is known at compile-time. This will lead to in-lining in some cases, for example:

```julia
julia> cogg = Cogg(x->2x)
Cogg{var"#1#2"}(var"#1#2"())

julia> f(c) = c.x(2)
f (generic function with 1 method)

julia> @code_llvm(f(cogg))
; @ REPL[3]:1 within `f`
define i64 @julia_f_177() #0 {
top:
  ret i64 4
}

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 15, 2022, 1:20pm UTC](https://discourse.julialang.org/t/can-inline-works-when-the-inlined-function-is-a-field-of-a-struct/77904/4 "2022-03-15T13:20:11Z")

</div>

In julia v1.8, you can call `@inline` at the callsite as well

```julia
help?> @inline

  Give a hint to the compiler that calls within block are worth inlining.

  # The compiler will try to inline `f`
  @inline f(...)
  
  # The compiler will try to inline `f`, `g` and `+`
  @inline f(...) + g(...)

  │ Julia 1.8
  │
  │ The callsite annotation requires at least Julia 1.8.

```

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 16, 2022, 1:27am UTC](https://discourse.julialang.org/t/can-inline-works-when-the-inlined-function-is-a-field-of-a-struct/77904/5 "2022-03-16T01:27:36Z")

</div>

Yes, after I append type parameters attached to Function field to each struct, the time has been speed up about 15%. Thanks

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 16, 2022, 1:29am UTC](https://discourse.julialang.org/t/can-inline-works-when-the-inlined-function-is-a-field-of-a-struct/77904/6 "2022-03-16T01:29:48Z")

</div>

Yes, that is more intuitive, Thanks.
