# How is destructuring transformed\\lowered

**URL:** <https://discourse.julialang.org/t/how-is-destructuring-transformed-lowered/22421>\
**Category:** Internals & Design\
**Created:** [March 27, 2019, 5:07pm UTC](https://discourse.julialang.org/t/how-is-destructuring-transformed-lowered/22421 "2019-03-27T17:07:14Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![ndinsmore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndinsmore/32/7433_2.png) [@ndinsmore](https://discourse.julialang.org/u/ndinsmore)\
**Post date:** [March 27, 2019, 8:28pm UTC](https://discourse.julialang.org/t/how-is-destructuring-transformed-lowered/22421/7 "2019-03-27T20:28:21Z")

</div>

I think I understand the reasoning but check out the benchmark here

> [@Iterator transformation results in 40% time reduction in Iteration over arrays](https://discourse.julialang.org/t/iterator-transformation-results-in-40-time-reduction-in-iteration-over-arrays/22428):
>
> Recently it has bugged me that for i in 1:length(array) @inbounds v=array[i] body ... end Was on the order of 30 % faster than: for v in array body ... end So I wanted to see if I could transform the latter into the former. The solution I came up with is a little hacky at the moment but it works & doesn’t change the two argument iterate construct. This has been updated to include a fix to include getindex. # B original iterator type # I new iterator type # IE eltype of I …

> [@Iterator transformation results in 40% time reduction in Iteration over arrays](https://discourse.julialang.org/t/iterator-transformation-results-in-40-time-reduction-in-iteration-over-arrays/22428/1):
>
> For the mean time we need the following functions for benchmarking:
> 
> ```julia
> function for_sum(iter)
> ret=eltype(iter)(0)
> for v in iter
> ret += v
> end
> return ret
> end
> 
> # goto_sum is needed because currently for is not lowered with the proper destructuring of next
> function goto_sum(iter)
> ret=eltype(iter)(0)
> next = iterate(iter)
> (next === nothing) && @goto loop_end
> @label loop_head
> (v, state) = next
> ret+=v
> next = iterate(iter, state)
> next === nothing && @goto loop_end
> @goto loop_head
> @label loop_end
> return ret
> end
> 
> ```
> 
> Lets sanity check their equivlence:
> 
> ```julia
> julia> a=collect(1:1000);
> 
> julia> for_sum(a)
> 500500
> 
> julia> goto_sum(a)
> 500500
> 
> julia> @benchmark for_sum($a)
> BenchmarkTools.Trial: 
> memory estimate: 0 bytes
> allocs estimate: 0
> --------------
> minimum time: 78.688 ns (0.00% GC)
> median time: 83.785 ns (0.00% GC)
> mean time: 91.614 ns (0.00% GC)
> maximum time: 883.004 ns (0.00% GC)
> --------------
> samples: 10000
> evals/sample: 964
> 
> julia> @benchmark goto_sum($a)
> BenchmarkTools.Trial: 
> memory estimate: 0 bytes
> allocs estimate: 0
> --------------
> minimum time: 78.688 ns (0.00% GC)
> median time: 83.778 ns (0.00% GC)
> mean time: 91.086 ns (0.00% GC)
> maximum time: 793.855 ns (0.00% GC)
> --------------
> samples: 10000
> evals/sample: 957
> 
> ```

---

_[View the full topic](https://discourse.julialang.org/t/how-is-destructuring-transformed-lowered/22421)._
