# Performant creation of vector of SVectors given a known formula

**URL:** <https://discourse.julialang.org/t/performant-creation-of-vector-of-svectors-given-a-known-formula/7648>\
**Category:** Performance\
**Tags:** question, array\
**Created:** [December 9, 2017, 9:33am UTC](https://discourse.julialang.org/t/performant-creation-of-vector-of-svectors-given-a-known-formula/7648 "2017-12-09T09:33:51Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [December 9, 2017, 8:46pm UTC](https://discourse.julialang.org/t/performant-creation-of-vector-of-svectors-given-a-known-formula/7648/12 "2017-12-09T20:46:00Z")

</div>

Matrix and Vector{SVector} are synonymous. You can independently update data[i][k]===asmat[k,i] for different k, same i, in different threads without any races; syntax that makes this hard without pointer-games is a shortcoming of julia, not the machine.

Today’s syntax requires us to “overwrite” the entirety of data[i] with the copied values. In 99% of the cases, the compiler removes the spurious reads and writes, such that this update only affects the single memory address of data[i][k]. If both threads try to update data[i][k1] and data[i][k2] at the same time, and the compiler was clever, there is no race condition. If the compiler was unclever, we get a race condition (e.g. thread one reads data[i][k1] and then stores it back, overwriting the update by thread two).

Yuyichao explained the current state in there [https://discourse.julialang.org/t/on-modifying-immutables-by-reference-in-multithreaded-code/6580](https://discourse.julialang.org/t/on-modifying-immutables-by-reference-in-multithreaded-code/6580) and linked to the PR [https://github.com/JuliaLang/julia/pull/21912](https://github.com/JuliaLang/julia/pull/21912). I really hope for Keno’s fix to be accepted at some point, in some variant.

Reinterpreting a matrix is a one-liner; dealing with fieldoffsets in Vector{some\_struct} is much, much worse syntax.

---

_[View the full topic](https://discourse.julialang.org/t/performant-creation-of-vector-of-svectors-given-a-known-formula/7648)._
