# Getting rid of allocations when copying data from one row to another

**URL:** <https://discourse.julialang.org/t/getting-rid-of-allocations-when-copying-data-from-one-row-to-another/115211>\
**Category:** Performance\
**Created:** [June 5, 2024, 6:02am UTC](https://discourse.julialang.org/t/getting-rid-of-allocations-when-copying-data-from-one-row-to-another/115211 "2024-06-05T06:02:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GreyAerin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greyaerin/32/209708_2.png) [@GreyAerin](https://discourse.julialang.org/u/GreyAerin)\
**Post date:** [June 5, 2024, 6:02am UTC](https://discourse.julialang.org/t/getting-rid-of-allocations-when-copying-data-from-one-row-to-another/115211/1 "2024-06-05T06:02:01Z")

</div>

I’m writing model code that requires me to replace the border values with the opposite domain values (our domain is everything but the border of the matrix) and since we have to run this value swap every iteration, I was wondering if there is a way I can not end up with array copies occurring in my code.

```julia
@views @inbounds function boundary_condition!(arr)
  arr[begin, begin] = arr[end-1, end-1]
  arr[end, end] = arr[begin+1, begin+1]
  arr[begin, end] = arr[end-1, begin+1]
  arr[end, begin] = arr[begin+1, end-1]

  arr[begin, begin+1:end-1] = arr[end-1, begin+1:end-1]
  arr[end, begin+1:end-1] = arr[begin+1, begin+1:end-1]
  arr[begin+1:end-1, begin] = arr[begin+1:end-1, end-1]
  arr[begin+1:end-1, end] = arr[begin+1:end-1, begin+1]
end

```

Is there a better/more effcient way to do this?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [June 5, 2024, 6:04am UTC](https://discourse.julialang.org/t/getting-rid-of-allocations-when-copying-data-from-one-row-to-another/115211/2 "2024-06-05T06:04:31Z")

</div>

Maybe `@.` when copying slices ?

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [June 5, 2024, 6:06am UTC](https://discourse.julialang.org/t/getting-rid-of-allocations-when-copying-data-from-one-row-to-another/115211/3 "2024-06-05T06:06:03Z")

</div>

Also, functions return their last statement - here a view of the matrix which must be allocated. I suggest a `return nothing` at the end to avoid this.

ETA: I realise since you’re `@inline`ing it the compiler is probably not allocating the return value anyway.

---

<div class="post-metadata">

**Author:** ![GreyAerin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greyaerin/32/209708_2.png) [@GreyAerin](https://discourse.julialang.org/u/GreyAerin)\
**Post date:** [June 5, 2024, 6:07am UTC](https://discourse.julialang.org/t/getting-rid-of-allocations-when-copying-data-from-one-row-to-another/115211/4 "2024-06-05T06:07:58Z")

</div>

This did the trick!
