<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Valentin Deleplace</title>
    <description>The latest articles on DEV Community by Valentin Deleplace (@deleplace).</description>
    <link>https://dev.to/deleplace</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F625265%2Fa34ff566-86cd-462d-87f2-8f514a0d34ac.jpeg</url>
      <title>DEV Community: Valentin Deleplace</title>
      <link>https://dev.to/deleplace</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deleplace"/>
    <language>en</language>
    <item>
      <title>Data-oriented ant, in Go</title>
      <dc:creator>Valentin Deleplace</dc:creator>
      <pubDate>Tue, 04 May 2021 15:23:53 +0000</pubDate>
      <link>https://dev.to/deleplace/data-oriented-ant-in-go-3eki</link>
      <guid>https://dev.to/deleplace/data-oriented-ant-in-go-3eki</guid>
      <description>&lt;p&gt;I read the great article &lt;a href="https://blog.royalsloth.eu/posts/the-compiler-will-optimize-that-away/" rel="noopener noreferrer"&gt;The compiler will optimize that away&lt;/a&gt; and wanted to quick check how the "most naive data-oriented approach" would impact the performance of a simple Go code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Type definitions
&lt;/h1&gt;

&lt;p&gt;Let's say an "Ant" has eight fields, 4 ints and 4 strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// An Ant is a struct with a few arbitrary fields.
type Ant struct {
    Field1 int
    Field2 string
    Field3 int
    Field4 string
    Field5 int
    Field6 string
    Field7 int
    Field8 string
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The numbers presented here hold for a 64-bit machine, where an &lt;code&gt;int&lt;/code&gt; is 8 bytes and a &lt;code&gt;string&lt;/code&gt; header is 16 bytes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxznug2savf8icrxlcnat.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxznug2savf8icrxlcnat.png" alt="One Ant"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The straightforward way to represent a colony of ants is a contiguous slice of ants:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// An AntColony is a slice of Ants.
type AntColony []Ant
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk53zgrejp2qw9vdi0142.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk53zgrejp2qw9vdi0142.png" alt="An Ant Colony"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The simple data-oriented way to represent a colony of ants is to pack all the values of each field in its own slice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// A DataOrientedAntColony is an alternative representation of
// an AntColony, where all the values of each fields are stored
// in a contiguous slice, in a data-oriented manner.
type DataOrientedAntColony struct {
    Field1 []int
    Field2 []string
    Field3 []int
    Field4 []string
    Field5 []int
    Field6 []string
    Field7 []int
    Field8 []string
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2b12g190hvfrdo7vpog0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2b12g190hvfrdo7vpog0.png" alt="A data-oriented Ant Colony"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is &lt;a href="https://github.com/Deleplace/ants/blob/main/antcolony.go" rel="noopener noreferrer"&gt;the source&lt;/a&gt; of the type definitions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Benchmarks
&lt;/h1&gt;

&lt;p&gt;Here is &lt;a href="https://github.com/Deleplace/ants/blob/main/antcolony_test.go" rel="noopener noreferrer"&gt;the source&lt;/a&gt; for the benchmarks.&lt;/p&gt;

&lt;p&gt;My general expectation is that the "data-oriented" approach will be faster for some access patterns, because (as explained in the &lt;a href="https://blog.royalsloth.eu/posts/the-compiler-will-optimize-that-away/" rel="noopener noreferrer"&gt;article&lt;/a&gt;) the bottleneck is accessing data in the memory, so fetching fewer bytes from the main memory should be faster, when we only care about a subset of the fields. &lt;/p&gt;

&lt;p&gt;For 1,000,000 ants:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;SearchByField1:&lt;/em&gt; traverse the whole colony, use only &lt;code&gt;Field1&lt;/code&gt; (an int)

&lt;ul&gt;
&lt;li&gt;my expectation: data-oriented will be faster.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;SearchByField2:&lt;/em&gt; traverse the whole colony, use only &lt;code&gt;Field2&lt;/code&gt; (a string)

&lt;ul&gt;
&lt;li&gt;my expectation: data-oriented will be slightly faster (if at all), as the time will be dominated by following pointers to string contents to compare.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Inspect:&lt;/em&gt; traverse the whole colony, use &lt;code&gt;Field1&lt;/code&gt;, &lt;code&gt;Field3&lt;/code&gt;, &lt;code&gt;Field5&lt;/code&gt;, &lt;code&gt;Field7&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;my expectation: data-oriented fetches less memory, but from 4 distinct slices in different memory locations, so I guess it will be overall slower.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Results
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go test -bench=.

cpu: Intel(R) Core(TM) i7-7600U CPU @ 2.80GHz

BenchmarkAntSearchByField1-4             172       6943993 ns/op
BenchmarkDOAntSearchByField1-4          2064        604584 ns/op

BenchmarkAntSearchByField2-4             100      13402776 ns/op
BenchmarkDOAntSearchByField2-4           244       4948108 ns/op

BenchmarkAntInspect-4                     86      13767480 ns/op
BenchmarkDOAntInspect-4                  100      10134642 ns/op
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;SearchByField1:&lt;/em&gt; a whooping 91% latency improvement (11x speedup). We're using only 8 bytes of each Ant out of 96 (8%). The total time is dominated by, and proportional to, the amount of memory fetched.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;SearchByField2:&lt;/em&gt; a 63% latency improvement (3x speedup). We're using only 16 bytes of each Ant out of 96 (17%), and then chasing a pointer to the actual string contents. The total time is still dominated by the amount of Ant memory fetched, regardless the extra string contents fetch and comparison. I did not expect such a high speed-up for the data-oriented version.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Inspect:&lt;/em&gt; a 26% latency improvement (1.4x speedup). We're using 32 bytes of each Ant out of 96 (33%). My intuition about the cost of dealing with memory scattered in 4 different slices is dispelled: the data-oriented version is faster again!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;A simple data-oriented memory layout can provide dramatic speedups.&lt;/p&gt;

</description>
      <category>go</category>
      <category>performance</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
