title: Can't reproduce false cache line sharing problem in Rust
body: <p>I'm trying to reproduce example 6 of the <a href="http://igoro.com/archive/gallery-of-processor-cache-effects/" rel="nofollow noreferrer">Gallery of Processor Cache Effects</a>.</p>
  
  <p>The article gives this function (in C#) as an example how to test false sharing:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private static int[] s_counter = new int[1024];
  private void UpdateCounter(int position)
  {
      for (int j = 0; j &lt; 100000000; j++)
      {
          s_counter[position] = s_counter[position] + 3;
      }
  }
  </code></pre>
  
  <p>If we create threads passing to this function 0, 1, 2, 3 arguments it will take the computation a long time to finish (author got 4.3 seconds). If we pass, for instance, 16, 32, 48, 64, we'll get much nicer results (0.28 seconds).</p>
  
  <p>I've come up with the following function in Rust:</p>
  
  <pre><code>pub fn cache_line_sharing(arr: [i32; 128], pos: usize) -&gt; (i32, i32) {
      let arr = Arc::new(arr);
      let handles: Vec&lt;_&gt; = (0..4).map(|thread_number| {
          let arr = arr.clone();
          let pos = thread_number * pos;
          thread::spawn(move || unsafe {
              let p = (arr.as_ptr() as *mut i32).offset(pos as isize);
              for _ in 0..1_000_000 {
                  *p = (*p).wrapping_add(3);
              }
          })
      }).collect();
  
      for handle in handles {
          handle.join().unwrap();
      }
  
      (arr[0], arr[1])
  }
  </code></pre>
  
  <p>Benchmarking it with two sets of arguments (0, 1, 2, 3 and 0, 16, 32, 48) gives me almost identical results: 108.34 and 105.07 microseconds.</p>
  
  <p>I use the criterion crate for the benchmarks. I have a MacBook Pro 2015 with Intel i5-5257U CPU (2.70GHz). My system reports to have <code>64B</code> cache line size.</p>
  
  <p>If anybody wants to see my full benchmarking code, here are the links:
  - <a href="https://github.com/mvlabat/hello_perf/blob/92ebb2f3291d9b71feb17fe2990df70faab43164/src/lib.rs#L36" rel="nofollow noreferrer">lib.rs</a>
  - <a href="https://github.com/mvlabat/hello_perf/blob/92ebb2f3291d9b71feb17fe2990df70faab43164/benches/cache_lines.rs#L38" rel="nofollow noreferrer">cache_lines.rs</a></p>
  
  <p>I want to understand the problem and find the way to reproduce similar results as in the article.</p>