title: Improving cache hit rate
body: <p>Hey so I have a function written in <code>c</code> whose cache performance I have to improve. The statistics are being provided by <code>cachegrind</code>. But I'm completely stuck and cannot get more than 10% improvement in the performance. I really need help with this.</p>
  
  <p>Here is the function:</p>
  
  <pre><code>#define LARGER  50000
  
  typedef struct { 
      char c1;
      double f1;
      int n1; 
      char c2;
      int n2;
      double f2; 
  } data; 
  
  char* func()
  {
      data* B = (data*) calloc(LARGER, sizeof(data));
      if (!B) return 0;
  
      double sum_f = 0.0;
      double sum_n = 0;
      char* sum_c = (char*) malloc(( 2 * LARGER + 1) * sizeof(char));
  
      sum_c[2 * LARGER] = '\0';
  
      int i;
      for(i = 0; i &lt; LARGER; i++)
      {
          sum_f += B[i].f1 + B[i].f2;
          sum_n += B[i].n1 + B[i].n2;
          sum_c[2 * i] = B[i].c1;
          sum_c[2 * i + 1] = B[i].c2;
      }
  
      free(B);
      return sum_c;
  }
  </code></pre>
  
  <p>The first thing that I noticed is that the definition of the <code>struct data</code> is not very cache-friendly because it has a ton of padding.</p>
  
  <p>So, I changed the definition according to allignment requiirements to this -</p>
  
  <pre><code>typedef struct {
      int n1;
      int n2;
      double f1;
      double f2;
      char c1;
      char c2;
  } data_new;
  </code></pre>
  
  <p>But this gives me only around 10% increase in cache-performance. I have no ideas on how to modify the for loop to improve the spatial locality further.</p>
  
  <p>Can anyone guide me on how to be able to write better loops that are cache-friendly.</p>
  
  <p>P.S. I am doing these questions as a part of my self study of a computer architecture book and I have no instructor to seek help.</p>