# Will field extraction be recognized as a loop invariant?

**URL:** https://discourse.julialang.org/t/will-field-extraction-be-recognized-as-a-loop-invariant/26345
**Category:** General Usage
**Tags:** question
**Created:** [July 14, 2019, 2:49pm UTC](https://discourse.julialang.org/t/will-field-extraction-be-recognized-as-a-loop-invariant/26345 "2019-07-14T14:49:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [July 14, 2019, 2:49pm UTC](https://discourse.julialang.org/t/will-field-extraction-be-recognized-as-a-loop-invariant/26345/1 "2019-07-14T14:49:02Z")

</div>

In the following example

```julia
struct Foo
    colind::Int
    A::Matrix{Float64}
    v::Vector{Float64}
end

function copycol!(foo::Foo)
    for i in eachindex(foo.v)
        foo.v[i] = foo.A[i, foo.colind]
    end
    foo
end

```

will the compiler recognize that all of the field extractions inside the loop are loop invariants?

At present I would write `copycol!` as

```julia
function copycol!(foo::Foo)
    colind = foo.colind
    A = foo.A 
    v = foo.v
    for i in eachindex(v)
        v[i] = A[i, colind]
    end
    foo
end

```

Am I telling the compiler something it will easily recognize on its own?

---

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [July 14, 2019, 3:05pm UTC](https://discourse.julialang.org/t/will-field-extraction-be-recognized-as-a-loop-invariant/26345/2 "2019-07-14T15:05:06Z")

</div>

In the spirit of “teach a person to fish” which of the `@code_*` macros should I apply to the `copycol!` method to work this out for myself? I hope I would be able to see it at some level before `@code_native`.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 14, 2019, 3:18pm UTC](https://discourse.julialang.org/t/will-field-extraction-be-recognized-as-a-loop-invariant/26345/3 "2019-07-14T15:18:06Z")

</div>

I don’t know the current status but in the 0.6 era this was true for non mutable structs.

See [https://github.com/JuliaLang/julia/issues/9755](https://github.com/JuliaLang/julia/issues/9755), [SparseMatrixCSC should probably be an immutable · Issue #15668 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15668), [https://github.com/JuliaLang/julia/pull/16371](https://github.com/JuliaLang/julia/pull/16371) and comments therein.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 14, 2019, 3:26pm UTC](https://discourse.julialang.org/t/will-field-extraction-be-recognized-as-a-loop-invariant/26345/4 "2019-07-14T15:26:14Z")

</div>

One wrinkle is that the data pointer for a vector can change but manually hoisting the field lookup doesn’t help, you’d need to boost the pointer lookup itself and write unsafe pointer code which will actually segfault if the data pointer changes, so that’s not recommended.
