<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[J@ArangoDB]]></title>
  <link href="http://jsteemann.github.io/atom.xml" rel="self"/>
  <link href="http://jsteemann.github.io/"/>
  <updated>2016-06-23T17:57:39+02:00</updated>
  <id>http://jsteemann.github.io/</id>
  <author>
    <name><![CDATA[jsteemann]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Handling Binary Data in Foxx 3.0]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/22/handling-binary-data-in-foxx-3-0/"/>
    <updated>2016-06-22T17:57:30+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/22/handling-binary-data-in-foxx-3-0</id>
    <content type="html"><![CDATA[<p><em>Note: this post is about the ArangoDB 3.x series</em></p>

<p>A while ago I wrote a <a href="http://jsteemann.github.io/blog/2014/10/15/handling-binary-data-in-foxx/">blog post</a>
about handling binary data in Foxx applications. That blog post provided a solution
for retrieving and serving non-UTF-8 data from a Foxx application. It was written
for the 2.x series of ArangoDB.</p>

<p>With the release of ArangoDB 3.0 that solution needs some adaption, and this
blog post provides the source code for the 3.0 version of the Foxx application.</p>

<!-- more -->


<p>The <code>rawBodyBuffer()</code> function of the <code>request</code> object is gone in 3.0, and to
retrieve the body as a blob we now need to use <code>req.rawBody</code>.
And as there are no controllers and applicationContexts in Foxx 3.0 anymore,
the original route code needs to be changed from</p>

<figure class='code'><figcaption><span>2.8 Foxx action that can handle binary input</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">controller</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;/receive-binary&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="c1">// fetch request body into the buffer</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">body</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">rawBodyBuffer</span><span class="p">();</span>
</span><span class='line'>  <span class="c1">// create an absolute filename, local to the Foxx application directory</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">filename</span> <span class="o">=</span> <span class="nx">applicationContext</span><span class="p">.</span><span class="nx">foxxFilename</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;fs&quot;</span><span class="p">).</span><span class="nx">write</span><span class="p">(</span><span class="nx">filename</span><span class="p">,</span> <span class="nx">body</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>to the following code:</p>

<figure class='code'><figcaption><span>3.0 Foxx action that can handle binary input</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">router</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;/receive-binary&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="c1">// fetch request body into a Buffer</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">body</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">rawBody</span><span class="p">;</span>
</span><span class='line'>  <span class="c1">// create an absolute filename, local to the Foxx application directory</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">filename</span> <span class="o">=</span> <span class="nx">module</span><span class="p">.</span><span class="nx">context</span><span class="p">.</span><span class="nx">filename</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;fs&quot;</span><span class="p">).</span><span class="nx">write</span><span class="p">(</span><span class="nx">filename</span><span class="p">,</span> <span class="nx">body</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>To serve binary data from a Foxx action, the original route code needs to
be changed from</p>

<figure class='code'><figcaption><span>2.8 Foxx action that returns contents of a file</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">controller</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/provide-binary-file&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="c1">// create an absolute filename, local to the Foxx application directory</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">filename</span> <span class="o">=</span> <span class="nx">applicationContext</span><span class="p">.</span><span class="nx">foxxFilename</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">);</span>
</span><span class='line'>  <span class="c1">// send the contents, this will also set mime type &quot;application/octet-stream&quot;</span>
</span><span class='line'>  <span class="nx">res</span><span class="p">.</span><span class="nx">sendFile</span><span class="p">(</span><span class="nx">filename</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>3.0 Foxx action that returns contents of a file</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">router</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/provide-binary-file&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="c1">// create an absolute filename, local to the Foxx application directory</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">filename</span> <span class="o">=</span> <span class="nx">module</span><span class="p">.</span><span class="nx">context</span><span class="p">.</span><span class="nx">fileName</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">);</span>
</span><span class='line'>  <span class="c1">// send the contents, this will also set mime type &quot;application/octet-stream&quot;</span>
</span><span class='line'>  <span class="nx">res</span><span class="p">.</span><span class="nx">sendFile</span><span class="p">(</span><span class="nx">filename</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>The adjusted Foxx application can be downloaded <a href="http://jsteemann.github.io/downloads/code/filelist-30.zip">here</a>
with full source code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How Much Memory Does an STL Container Use?]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/14/how-much-memory-does-an-stl-container-use/"/>
    <updated>2016-06-14T00:35:29+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/14/how-much-memory-does-an-stl-container-use</id>
    <content type="html"><![CDATA[<p>Ever wondered how much heap memory will be used by STL containers, and
in what chunks they will allocate it?</p>

<p>Here is the answer for some frequently used containers types containing
uint64_t values (an 8 byte type). For the associative containers I also
used uint64_t as the key type.</p>

<!-- more -->


<p>All results are for the STL implementation I had ready at hand (g++5.3 on
Ubuntu 16.04, x86_64, libstdc++.so.6.0.22, with default allocator).
The results may be completely different for other platforms and/or other
data types.</p>

<p>The following containers are compared:</p>

<ul>
<li>std::vector&lt;uint64_t></li>
<li>std::map&lt;uint64, uint64_t></li>
<li>std::set&lt;uint64_t></li>
<li>std::unordered_map&lt;uint64, uint64_t></li>
<li>std::unordered_set&lt;uint64_t></li>
<li>std::deque&lt;uint64_t></li>
</ul>


<p>The containers themselves are creared on the stack. To store the elements,
the containers will need to use heap memory. The number of elements in the
containers (<em>n</em>) is increased exponentially from 0 (empty) to 512 in the
tests. The memory usage pattern should be quite clear by then.</p>

<p>Reported are the amount of heap memory allocated by the container after all
elements have been inserted, the total number of heap memory allocated by
the container during its whole lifetime (i.e. the sum of all its <code>malloc</code>s),
plus the number of calls to <code>malloc</code> and <code>free</code> the container issued.</p>

<figure class='code'><figcaption><span>heap allocation results</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
<span class='line-number'>87</span>
<span class='line-number'>88</span>
<span class='line-number'>89</span>
<span class='line-number'>90</span>
<span class='line-number'>91</span>
<span class='line-number'>92</span>
<span class='line-number'>93</span>
<span class='line-number'>94</span>
<span class='line-number'>95</span>
<span class='line-number'>96</span>
<span class='line-number'>97</span>
<span class='line-number'>98</span>
<span class='line-number'>99</span>
<span class='line-number'>100</span>
<span class='line-number'>101</span>
<span class='line-number'>102</span>
<span class='line-number'>103</span>
<span class='line-number'>104</span>
<span class='line-number'>105</span>
<span class='line-number'>106</span>
<span class='line-number'>107</span>
<span class='line-number'>108</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>n =     0
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;     0 bytes allocd at end,  total:     0 bytes mallocd,    0 malloc(s), 0 free(s)
</span><span class='line'>map            =&gt;     0 bytes allocd at end,  total:     0 bytes mallocd,    0 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;     0 bytes allocd at end,  total:     0 bytes mallocd,    0 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;     0 bytes allocd at end,  total:     0 bytes mallocd,    0 malloc(s), 0 free(s)
</span><span class='line'>unordered_set  =&gt;     0 bytes allocd at end,  total:     0 bytes mallocd,    0 malloc(s), 0 free(s)
</span><span class='line'>deque          =&gt;   576 bytes allocd at end,  total:   576 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =     1
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;     8 bytes allocd at end,  total:     8 bytes mallocd,    1 malloc(s), 0 free(s)
</span><span class='line'>map            =&gt;    48 bytes allocd at end,  total:    48 bytes mallocd,    1 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;    40 bytes allocd at end,  total:    40 bytes mallocd,    1 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;    40 bytes allocd at end,  total:    40 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>unordered_set  =&gt;    32 bytes allocd at end,  total:    32 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>deque          =&gt;   576 bytes allocd at end,  total:   576 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =     2
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;    16 bytes allocd at end,  total:    24 bytes mallocd,    2 malloc(s), 1 free(s)
</span><span class='line'>map            =&gt;    96 bytes allocd at end,  total:    96 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;    80 bytes allocd at end,  total:    80 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;    88 bytes allocd at end,  total:   104 bytes mallocd,    4 malloc(s), 1 free(s)
</span><span class='line'>unordered_set  =&gt;    72 bytes allocd at end,  total:    88 bytes mallocd,    4 malloc(s), 1 free(s)
</span><span class='line'>deque          =&gt;   576 bytes allocd at end,  total:   576 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =     4
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;    32 bytes allocd at end,  total:    56 bytes mallocd,    3 malloc(s), 2 free(s)
</span><span class='line'>map            =&gt;   192 bytes allocd at end,  total:   192 bytes mallocd,    4 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;   160 bytes allocd at end,  total:   160 bytes mallocd,    4 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;   136 bytes allocd at end,  total:   152 bytes mallocd,    6 malloc(s), 1 free(s)
</span><span class='line'>unordered_set  =&gt;   104 bytes allocd at end,  total:   120 bytes mallocd,    6 malloc(s), 1 free(s)
</span><span class='line'>deque          =&gt;   576 bytes allocd at end,  total:   576 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =     8
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;    64 bytes allocd at end,  total:   120 bytes mallocd,    4 malloc(s), 3 free(s)
</span><span class='line'>map            =&gt;   384 bytes allocd at end,  total:   384 bytes mallocd,    8 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;   320 bytes allocd at end,  total:   320 bytes mallocd,    8 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;   280 bytes allocd at end,  total:   336 bytes mallocd,   11 malloc(s), 2 free(s)
</span><span class='line'>unordered_set  =&gt;   216 bytes allocd at end,  total:   272 bytes mallocd,   11 malloc(s), 2 free(s)
</span><span class='line'>deque          =&gt;   576 bytes allocd at end,  total:   576 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =    16
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;   128 bytes allocd at end,  total:   248 bytes mallocd,    5 malloc(s), 4 free(s)
</span><span class='line'>map            =&gt;   768 bytes allocd at end,  total:   768 bytes mallocd,   16 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;   640 bytes allocd at end,  total:   640 bytes mallocd,   16 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;   568 bytes allocd at end,  total:   712 bytes mallocd,   20 malloc(s), 3 free(s)
</span><span class='line'>unordered_set  =&gt;   440 bytes allocd at end,  total:   584 bytes mallocd,   20 malloc(s), 3 free(s)
</span><span class='line'>deque          =&gt;   576 bytes allocd at end,  total:   576 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =    32
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;   256 bytes allocd at end,  total:   504 bytes mallocd,    6 malloc(s), 5 free(s)
</span><span class='line'>map            =&gt;  1536 bytes allocd at end,  total:  1536 bytes mallocd,   32 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;  1280 bytes allocd at end,  total:  1280 bytes mallocd,   32 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;  1144 bytes allocd at end,  total:  1472 bytes mallocd,   37 malloc(s), 4 free(s)
</span><span class='line'>unordered_set  =&gt;   888 bytes allocd at end,  total:  1216 bytes mallocd,   37 malloc(s), 4 free(s)
</span><span class='line'>deque          =&gt;   576 bytes allocd at end,  total:   576 bytes mallocd,    2 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =    64
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;   512 bytes allocd at end,  total:  1016 bytes mallocd,    7 malloc(s), 6 free(s)
</span><span class='line'>map            =&gt;  3072 bytes allocd at end,  total:  3072 bytes mallocd,   64 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;  2560 bytes allocd at end,  total:  2560 bytes mallocd,   64 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;  2312 bytes allocd at end,  total:  3016 bytes mallocd,   70 malloc(s), 5 free(s)
</span><span class='line'>unordered_set  =&gt;  1800 bytes allocd at end,  total:  2504 bytes mallocd,   70 malloc(s), 5 free(s)
</span><span class='line'>deque          =&gt;  1088 bytes allocd at end,  total:  1088 bytes mallocd,    3 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =   128
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;  1024 bytes allocd at end,  total:  2040 bytes mallocd,    8 malloc(s), 7 free(s)
</span><span class='line'>map            =&gt;  6144 bytes allocd at end,  total:  6144 bytes mallocd,  128 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt;  5120 bytes allocd at end,  total:  5120 bytes mallocd,  128 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;  4664 bytes allocd at end,  total:  6144 bytes mallocd,  135 malloc(s), 6 free(s)
</span><span class='line'>unordered_set  =&gt;  3640 bytes allocd at end,  total:  5120 bytes mallocd,  135 malloc(s), 6 free(s)
</span><span class='line'>deque          =&gt;  1600 bytes allocd at end,  total:  1600 bytes mallocd,    4 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =   256
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;  2048 bytes allocd at end,  total:  4088 bytes mallocd,    9 malloc(s), 8 free(s)
</span><span class='line'>map            =&gt; 12288 bytes allocd at end,  total: 12288 bytes mallocd,  256 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt; 10240 bytes allocd at end,  total: 10240 bytes mallocd,  256 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt;  9416 bytes allocd at end,  total: 12488 bytes mallocd,  264 malloc(s), 7 free(s)
</span><span class='line'>unordered_set  =&gt;  7368 bytes allocd at end,  total: 10440 bytes mallocd,  264 malloc(s), 7 free(s)
</span><span class='line'>deque          =&gt;  2624 bytes allocd at end,  total:  2624 bytes mallocd,    6 malloc(s), 0 free(s)
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>n =   512
</span><span class='line'>---------
</span><span class='line'>vector         =&gt;  4096 bytes allocd at end,  total:  8184 bytes mallocd,   10 malloc(s), 9 free(s)
</span><span class='line'>map            =&gt; 24576 bytes allocd at end,  total: 24576 bytes mallocd,  512 malloc(s), 0 free(s)
</span><span class='line'>set            =&gt; 20480 bytes allocd at end,  total: 20480 bytes mallocd,  512 malloc(s), 0 free(s)
</span><span class='line'>unordered_map  =&gt; 18872 bytes allocd at end,  total: 25216 bytes mallocd,  521 malloc(s), 8 free(s)
</span><span class='line'>unordered_set  =&gt; 14776 bytes allocd at end,  total: 21120 bytes mallocd,  521 malloc(s), 8 free(s)
</span><span class='line'>deque          =&gt;  4752 bytes allocd at end,  total:  4816 bytes mallocd,   11 malloc(s), 1 free(s)</span></code></pre></td></tr></table></div></figure>


<p>I have intentionally not done any optimizations such as calling <code>reserve()</code>
on the containers before inserting the individual elements, in order to
reflect some real-word scenarios in which it is unclear how much elements
will be added to the containers during program execution.</p>

<p>The test program for reproduction is <a href="http://jsteemann.github.io/downloads/code/containers.cpp">here</a>. Compile and run it with:</p>

<figure class='code'><figcaption><span>compiling and executing the test program</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>g++ -std<span class="o">=</span>c++11 -O0 containers.cpp -o containers
</span><span class='line'><span class="k">for </span>i in 0 1 2 4 8 16 32 64 128 256 512; <span class="k">do</span> ./containers <span class="s2">&quot;$i&quot;</span>; <span class="k">done</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compiling an Optimized Version of ArangoDB]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/09/compiling-an-optimized-version-of-arangodb/"/>
    <updated>2016-06-09T20:31:31+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/09/compiling-an-optimized-version-of-arangodb</id>
    <content type="html"><![CDATA[<p>Why should you care about compiling ArangoDB on your own when there are
official release packages that are ready to use?</p>

<!--more -->


<p>There are three main reasons for compiling on your own:</p>

<ul>
<li><p>as a developer you want to <em>make changes to the ArangoDB C++ source code</em>.
Then your only option obviously is to compile on your own. Please consult
the <a href="http://jsteemann.github.io/blog/2016/06/09/compiling-a-debug-version-of-arangodb/">compiling a debug version of ArangoDB</a>
page for more information.</p></li>
<li><p>you are trying to <em>get meaningful stack traces from core dumps</em> produced
by ArangoDB and need an ArangoDB binary that comes with enough debug
information (debug symbols, probably also assertions turned on). In this
case, please also consult the <a href="http://jsteemann.github.io/blog/2016/06/09/compiling-a-debug-version-of-arangodb/">compiling a debug version of ArangoDB</a>
blog post for how to get this done.</p></li>
<li><p>you want to use an ArangoDB binary that is <em>optimized for your specific
target architecture</em>.</p></li>
</ul>


<p>The latter reason is relevant because the official release packages that
are provided by ArangoDB cannot make too many assumptions about the environment
in which they will be used. In the general release packages there is not so
much room for platform-specific optimizations as there would be if you are
compiling just for the local machine.</p>

<p>For example, all relevant CPU offer <a href="https://en.wikipedia.org/wiki/SIMD">SIMD</a>
instructions that a compiler can exploit when generating code. But different
generations of CPUs offer different levels of SIMD instructions. Not every CPU
in use today provides SSE4, not to talk about AVX.</p>

<p>To make our release packages compatible with most environments, we have had
to make some conservative assumptions about the CPU abilities, which effectively
disables many optimizations that would have been possible when creating a
build that only needs to run on a specific architecture.</p>

<p>To fully exploit the capabilities of a specific target environment, it&rsquo;s required
to build executables for that specific architecture. Most compilers offer an option
<code>-march</code> for that. You normally want to set this to <code>native</code> when compiling an
optimized version. There are also lots of compiler options for enabling or disabling
specific CPU features such as <code>-msse</code>, <code>-msse2</code>, <code>-msse3</code>, <code>-mssse3</code>,
<code>-msse4.1</code>, <code>-msse4.2</code>, <code>-msse4</code>, <code>-mavx</code>, <code>-mavx2</code>, to name just a few.</p>

<p>The good news is that there is no need to deal with such compiler-specific optimization
options in order to get an optimized build. The cmake-based ArangoDB 3.0 builds will
automatically test the local environment&rsquo;s capabilities and set the compiler options
based on which CPU abilities were detected.</p>

<p>For example, a mere <code>(cd build &amp;&amp; cmake -DCMAKE_BUILD_TYPE=Release ..)</code> will run the
CPU ability detection and configure the build to use the features supported by the
local architecture:</p>

<figure class='code'><figcaption><span>configuring a release build</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">(</span>mkdir -p build; <span class="nb">cd </span>build <span class="o">&amp;&amp;</span> cmake -DCMAKE_BUILD_TYPE<span class="o">=</span>Release ..<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>For example, on my laptop this prints:</p>

<figure class='code'><figcaption><span>cmake output</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>-- The CXX compiler identification is GNU 5.3.1
</span><span class='line'>-- The C compiler identification is GNU 5.3.1
</span><span class='line'>...
</span><span class='line'>-- target changed from "" to "auto"
</span><span class='line'>-- Detected CPU: haswell
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__march_core_avx2
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__march_core_avx2 - Success
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse2
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse2 - Success
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse3
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse3 - Success
</span><span class='line'>-- Looking for pmmintrin.h
</span><span class='line'>-- Looking for pmmintrin.h - found
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mssse3
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mssse3 - Success
</span><span class='line'>-- Looking for tmmintrin.h
</span><span class='line'>-- Looking for tmmintrin.h - found
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse4_1
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse4_1 - Success
</span><span class='line'>-- Looking for smmintrin.h
</span><span class='line'>-- Looking for smmintrin.h - found
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse4_2
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse4_2 - Success
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mavx
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mavx - Success
</span><span class='line'>-- Looking for immintrin.h
</span><span class='line'>-- Looking for immintrin.h - found
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse2avx
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__msse2avx - Success
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mavx2
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mavx2 - Success
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mno_sse4a
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mno_sse4a - Success
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mno_xop
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mno_xop - Success
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mno_fma4
</span><span class='line'>-- Performing Test check_cxx_compiler_flag__mno_fma4 - Success
</span><span class='line'>...</span></code></pre></td></tr></table></div></figure>


<p>The detected options will end up in the CMakeCache.txt file in the build
directory. The Makefile generated by cmake will automatically make use of
these options when invoking the C++ compiler.</p>

<p>The compiler options are not shown by default, but they can be made visible
by compiling with the option <code>VERBOSE=1</code>, e.g.</p>

<figure class='code'><figcaption><span>configuring and compiling a build</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">(</span>mkdir -p build; <span class="nb">cd </span>build <span class="o">&amp;&amp;</span> cmake -DCMAKE_BUILD_TYPE<span class="o">=</span>Release ..<span class="o">)</span>
</span><span class='line'><span class="o">(</span><span class="nb">cd </span>build <span class="o">&amp;&amp;</span> make -j4 <span class="nv">VERBOSE</span><span class="o">=</span>1<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that this will be <em>very verbose</em>, so you only want to set the <code>VERBOSE=1</code>
option to check that the compiler options were picked correctly.</p>

<p>On my local laptop, the architecture-specific compiler options that were automatically
detected and used were</p>

<figure class='code'><figcaption><span>compiler architecture options used </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>-march<span class="o">=</span>core-avx2 -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx -msse2avx -mavx2 -mno-sse4a -mno-xop -mno-fma4
</span></code></pre></td></tr></table></div></figure>


<p>The build has detected <code>core-avx2</code>, which in my case is good &ndash; and a lot more
specific than the official packages which for example cannot assume the presence
of either SSE4 or AVX instructions.</p>

<p>And now that we can rely on the presence of specific CPU instructions, some code
parts such as JSON parsing can make use of SSE4.2 instructions, or the compiler
can use some optimized SIMD variants for operations such as memcpy, strlen etc.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using the Address Sanitizer (ASAN) in ArangoDB Development]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/09/using-the-address-sanitizer-asan-in-arangodb-development/"/>
    <updated>2016-06-09T20:07:29+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/09/using-the-address-sanitizer-asan-in-arangodb-development</id>
    <content type="html"><![CDATA[<p>Once you have set up a <a href="http://jsteemann.github.io/blog/2016/06/09/compiling-a-debug-version-of-arangodb/">debug version of ArangoDB</a>,
it is quite helpful to also enable the <a href="https://en.wikipedia.org/wiki/AddressSanitizer">Address Sanitizer</a>
and its companion tools.</p>

<p>These sanitizers provide runtime instrumentation for executables and check
for common C++ programming errors such as buffer overflows, use-after-free
bugs and memory leaks. The sanitizers supported natively by recent versions
of g++ and clang++.</p>

<p>The general runtime overhead of the sanitizers is neglectable compared to
other instrumentation tools such as Valgrind, so it can well be used in the
day-to-day development process.</p>

<!-- more -->


<h2>Configuring the build to use ASAN</h2>

<p>To build with ASAN (Address Sanitizer) and UBSAN (Undefined Behavior Sanitizer),
the following environment variables need to be set when invoking cmake:</p>

<figure class='code'><figcaption><span>environment variables for ASAN and UBSAN</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">CXXFLAGS</span><span class="o">=</span><span class="s2">&quot;-fsanitize=address -fsanitize=undefined -fno-sanitize=alignment -fno-sanitize=vptr&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The full command to configure a debug build with ASAN and UBSAN support is:</p>

<figure class='code'><figcaption><span>configuring the build to use the sanitizers</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">(</span><span class="nb">cd </span>build <span class="o">&amp;&amp;</span> <span class="nv">CXXFLAGS</span><span class="o">=</span><span class="err">&quot;</span>-fsanitize<span class="o">=</span>address -fsanitize<span class="o">=</span>undefined -fno-sanitize<span class="o">=</span>alignment -fno-sanitize<span class="o">=</span>vptr cmake -DCMAKE_BUILD_TYPE<span class="o">=</span>Debug -DUSE_MAINTAINER_MODE<span class="o">=</span>On ..<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will configure the build so it will pass the sanitizer options to the C++
compiler. Note that you&rsquo;ll need a recent version of g++ or clang++ for this
to work.</p>

<p>After that, build arangod normally. The binaries produced will be instrumented
by the sanitizers, allowing many nasty memory errors to be detected very early.</p>

<h2>Checking if an arangod binary is using ASAN</h2>

<p>Whether or not an ArangoDB binary is instrumented can be found out at any
time by calling the binary with the <code>--version</code> option. This will print some
version information for ArangoDB itself and the other required libraries,
and it will also print in the line starting with <code>asan</code> whether the binary
was compiled with ASAN support or not:</p>

<figure class='code'><figcaption><span>checking the ASAN support of an arangod binary</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; build/bin/arangod --version
</span><span class='line'>
</span><span class='line'>3.0.x-devel
</span><span class='line'>
</span><span class='line'>architecture: 64bit
</span><span class='line'>asan: <span class="nb">true</span>                       <span class="c"># !!!!</span>
</span><span class='line'>asm-crc32: <span class="nb">true</span>
</span><span class='line'>boost-version: 1.61.0b1
</span><span class='line'>build-date: 2016-06-09 12:07:07
</span><span class='line'>compiler: gcc
</span><span class='line'>cplusplus: 201103
</span><span class='line'>endianness: little
</span><span class='line'>fd-client-event-handler: poll
</span><span class='line'>fd-setsize: 1024
</span><span class='line'>icu-version: 54.1
</span><span class='line'>jemalloc: <span class="nb">false</span>
</span><span class='line'>libev-version: 4.22
</span><span class='line'>maintainer-mode: <span class="nb">true</span>
</span><span class='line'>openssl-version: OpenSSL 1.0.2g-fips  1 Mar 2016
</span><span class='line'>rocksdb-version: 4.8.0
</span><span class='line'>server-version: 3.0.x-devel
</span><span class='line'>sizeof int: 4
</span><span class='line'>sizeof void*: 8
</span><span class='line'>sse42: <span class="nb">true</span>
</span><span class='line'>tcmalloc: <span class="nb">false</span>
</span><span class='line'>v8-version: 5.0.71.39
</span><span class='line'>vpack-version: 0.1.30
</span><span class='line'>zlib-version: 1.2.8
</span></code></pre></td></tr></table></div></figure>


<h2>Controlling ASAN runtime behavior</h2>

<p>ASAN behavior can also be controlled at runtime, after the binaries have
been produced, by adjusting the environment variable <code>ASAN_OPTIONS</code>.
The setting I use for this is:</p>

<figure class='code'><figcaption><span>adjusting ASAN options</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">ASAN_OPTIONS</span><span class="o">=</span><span class="s2">&quot;handle_ioctl=true:check_initialization_order=true:detect_container_overflow=1:detect_stack_use_after_return=false:detect_odr_violation=1:allow_addr2line=true:strict_init_order=true&quot;</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compiling a Debug Version of ArangoDB]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/09/compiling-a-debug-version-of-arangodb/"/>
    <updated>2016-06-09T20:07:29+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/09/compiling-a-debug-version-of-arangodb</id>
    <content type="html"><![CDATA[<p>Compiling a debug version of ArangoDB is a must for everyone that
wants to modify the C++ ArangoDB source code and test their changes
locally.</p>

<p>How to do this is different in the 2.8 and 3.0 branches of ArangoDB,
but luckily it&rsquo;s really easy to achieve in both branches.</p>

<!-- more -->


<h2>ArangoDB 2.8</h2>

<p>For the 2.8 branch of ArangoDB, clone the 2.8 version of the ArangoDB
repository first:</p>

<figure class='code'><figcaption><span>cloning the 2.8 repository</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git clone -b 2.8 https://github.com/arangodb/arangodb
</span><span class='line'><span class="nb">cd </span>ArangoDB
</span></code></pre></td></tr></table></div></figure>


<p>And in that directory execute the following commands:</p>

<figure class='code'><figcaption><span>compiling a debug version of 2.8</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>make setup
</span><span class='line'><span class="nb">export </span><span class="nv">CFLAGS</span><span class="o">=</span><span class="s2">&quot;-g -Og&quot;</span>
</span><span class='line'><span class="nb">export </span><span class="nv">CXXFLAGS</span><span class="o">=</span><span class="s2">&quot;-g -Og&quot;</span>
</span><span class='line'>./configure --enable-relative --enable-maintainer-mode
</span><span class='line'>make -j4
</span></code></pre></td></tr></table></div></figure>


<p>(note that you&rsquo;ll need a working C++11 compiler and some prerequisites
such as the OpenSSL library, GNU Bison and Flex installed for this to work).</p>

<p>This will compile ArangoDB and all of its dependencies, and finally
make them available in the <code>bin</code> subdirectory of the current directory.
There is no need to install ArangoDB using <code>make install</code>. arangod and
the client tools can be run locally using the following commands:</p>

<figure class='code'><figcaption><span>running arangod and arangosh</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>bin/arangod --console mydb <span class="c"># start arangod in console mode</span>
</span><span class='line'>bin/arangosh <span class="c"># starts an ArangoShell</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, after any modification to the ArangoDB C++ source code you can re-compile
using <code>make</code>. This will only rebuild the things that need to be rebuilt. If the
build is successful, the changes you have made should be visible when restarting
the binaries.</p>

<p>As the <code>-g</code> option used above will have configured a build with debug
symbols, it&rsquo;s also possible to use a debugger such as gdb for stepping
through the executables, attach to the while they are running, or to
obtain stack traces in case any of the executables crashed.</p>

<h2>ArangoDB 3.0</h2>

<p>For the 3.0 branch of ArangoDB, it&rsquo;s required to once clone that version
of the ArangoDB repository into a local directory:</p>

<figure class='code'><figcaption><span>cloning the 3.0 repository</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git clone -b 3.0 https://github.com/arangodb/arangodb
</span><span class='line'><span class="nb">cd </span>ArangoDB
</span></code></pre></td></tr></table></div></figure>


<p>In that directory execute the following commands to configure the build
for debugging:</p>

<figure class='code'><figcaption><span>configuring a debug version of 3.0</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">(</span>mkdir -p build; <span class="nb">cd </span>build <span class="o">&amp;&amp;</span> cmake -DCMAKE_BUILD_TYPE<span class="o">=</span>Debug -DUSE_MAINTAINER_MODE<span class="o">=</span>On ..<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>(again you&rsquo;ll need a working C++11 compiler and some prerequisites
such as the OpenSSL library installed for this to work).</p>

<p>This will configure the build to be a debug build, with debug symbols
but most optimizations disabled. To build ArangoDB, just execute:</p>

<figure class='code'><figcaption><span>compiling a debug version of 3.0</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">(</span><span class="nb">cd </span>build <span class="o">&amp;&amp;</span> make -j4<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>To execute one of the binaries, run them from the checkout directory
as follows:</p>

<figure class='code'><figcaption><span>running arangod and arangosh</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>build/bin/arangod --console mydb <span class="c"># start arangod in console mode</span>
</span><span class='line'>build/bin/arangosh <span class="c"># starts an ArangoShell</span>
</span></code></pre></td></tr></table></div></figure>


<p>After any modification to the ArangoDB C++ source code, build again
using <code>(cd build &amp;&amp; make -j4)</code> to see the changes in effect.
The build is a debug build, meaning you can use a debugger and get
meaningful stack traces for core dumps produced by the binaries.</p>

<p>If you got, then I strongly recommend to also enable the
<a href="https://en.wikipedia.org/wiki/AddressSanitizer">Address Sanitizer</a>
(ASAN) for your debug build. This can greatly help finding common memory usage
errors during development. There is another<br/>
<a href="http://jsteemann.github.io/blog/2016/06/09/using-the-address-sanitizer-asan-in-arangodb-development/">blog post about using ASAN in ArangoDB development</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fastest String-to-uint64 Conversion Method?]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/02/fastest-string-to-uint64-conversion-method/"/>
    <updated>2016-06-02T20:15:27+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/02/fastest-string-to-uint64-conversion-method</id>
    <content type="html"><![CDATA[<p>While doing some optimization work for the upcoming ArangoDB 3.0 release,
we had to figure out what was the &ldquo;ideal&rdquo; way of converting a string
representation of a number into a C++ uint64_t (64 bit unsigned integer type).
This kind of operation is performed a lot during the lifetime of an ArangoDB
server process, so it seemed worthwhile making it as fast as possible.</p>

<!-- more -->


<h2>std::stoull and std::strtoull</h2>

<p>The natural solution for this kind of conversion is the standard library&rsquo;s
<a href="http://en.cppreference.com/w/cpp/string/basic_string/stoul">std::stoull</a>
function. On the one hand, that solution is potentially optimized and definitely
robust. It performs validation of the input characters and is also battle-tested
by millions of users.</p>

<p>On the other hand, std::stoull has a few &ldquo;features&rdquo; that some would consider
&ldquo;anti&rdquo;-features, and that sound like they could negatively influence performance:</p>

<ul>
<li>it may behave locale-dependent</li>
<li>it can parse and will let pass negative input values, which is often not desired
for a result of unsigned type</li>
<li>it can perform base conversion</li>
</ul>


<p>In C++11 there is also the alternative
<a href="http://en.cppreference.com/w/cpp/string/byte/strtoul">std::strtoull</a>.
It should behave about the same as std::stoull except in case of error.
std::stoull will throw and std::strtoull will not.</p>

<p>That&rsquo;s what we get from the standard library for long unsigned integer types.</p>

<h2>Alternative implementations (w/o error checking)</h2>

<p>Another alternative is to roll a string to number conversion function ourselves.
If we hard-code it to base 10 and do not care about error checking, a naive
implementation may look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="kr">inline</span> <span class="n">uint64_t</span> <span class="n">naive</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="k">const</span><span class="o">&amp;</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">uint64_t</span> <span class="n">result</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>  <span class="kt">char</span> <span class="k">const</span><span class="o">*</span> <span class="n">p</span> <span class="o">=</span> <span class="n">value</span><span class="p">.</span><span class="n">c_str</span><span class="p">();</span>
</span><span class='line'>  <span class="kt">char</span> <span class="k">const</span><span class="o">*</span> <span class="n">q</span> <span class="o">=</span> <span class="n">p</span> <span class="o">+</span> <span class="n">value</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
</span><span class='line'>  <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">&lt;</span> <span class="n">q</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">result</span> <span class="o">*=</span> <span class="mi">10</span><span class="p">;</span>
</span><span class='line'>    <span class="n">result</span> <span class="o">+=</span> <span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">++</span><span class="p">)</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Obviously the above will produce wrong results for invalid
input data, but for &ldquo;trusted&rdquo; (known to be valid) input it may
be just fine.</p>

<p>Here&rsquo;s just another implementation for the problem at hand. This one
doesn&rsquo;t perform the times 10 operation, but splits it into two bitshifting
operations:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="kr">inline</span> <span class="n">uint64_t</span> <span class="n">bitshift</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="k">const</span><span class="o">&amp;</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">uint64_t</span> <span class="n">result</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="kt">char</span> <span class="k">const</span><span class="o">*</span> <span class="n">p</span> <span class="o">=</span> <span class="n">value</span><span class="p">.</span><span class="n">c_str</span><span class="p">();</span>
</span><span class='line'>  <span class="kt">char</span> <span class="k">const</span><span class="o">*</span> <span class="n">q</span> <span class="o">=</span> <span class="n">p</span> <span class="o">+</span> <span class="n">value</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
</span><span class='line'>  <span class="k">while</span> <span class="p">(</span><span class="n">p</span> <span class="o">&lt;</span> <span class="n">q</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">result</span> <span class="o">&lt;&lt;</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">result</span> <span class="o">&lt;&lt;</span> <span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="o">++</span><span class="p">)</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Again no error checking is present in the above function, but it
should be ok for trusted inputs.</p>

<p>By manually unrolling the while loop and converting it into a switch
statement, we can also come up with a conversion function that has minimal
branching:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="kr">inline</span> <span class="n">uint64_t</span> <span class="n">unrolled</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="k">const</span><span class="o">&amp;</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">uint64_t</span> <span class="n">result</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">size_t</span> <span class="k">const</span> <span class="n">length</span> <span class="o">=</span> <span class="n">value</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
</span><span class='line'>  <span class="k">switch</span> <span class="p">(</span><span class="n">length</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">20</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">20</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10000000000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">19</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">19</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1000000000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">18</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">18</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100000000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">17</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">17</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10000000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">16</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">16</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1000000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">15</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">15</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">14</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">14</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">13</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">13</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1000000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">12</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">12</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">11</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">11</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span> <span class="mi">10</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span> <span class="mi">10</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1000000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">9</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">9</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">8</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">8</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">7</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">7</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1000000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">6</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">6</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">5</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">5</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">4</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">4</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1000ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">3</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">3</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">2</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10ULL</span><span class="p">;</span>
</span><span class='line'>    <span class="k">case</span>  <span class="mi">1</span><span class="o">:</span>    <span class="n">result</span> <span class="o">+=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">length</span> <span class="o">-</span>  <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="sc">&#39;0&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Performance testing</h2>

<p>To check out how all these alternatives perform, I put them into a small
<a href="http://jsteemann.github.io/downloads/code/stoull-test.cpp">test driver program</a>. To compile it with
g++ and run it, I used this command:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>g++ -Wall -Wextra -march<span class="o">=</span>native -std<span class="o">=</span>c++11 -O3 stdoull-test.cpp <span class="o">&amp;&amp;</span> ./a.out
</span></code></pre></td></tr></table></div></figure>


<p>The test program will convert input strings of different lengths to uint64_t
using the above (and some other) implementations, going up from 10,000 iterations
per string up to 100,000,000. It also prints the wall-clock time spent in each
run. The most interesting output it prints is in the &ldquo;ms&rdquo; column. The &ldquo;result&rdquo;
column can be fully ignored. It&rsquo;s only there so the compiler won&rsquo;t optimize away
the calls to the conversion functions.</p>

<p>The timings from my local laptop (Intel Core i7-4710HQ CPU @ 2.50GHz, Ubuntu 16.04,
g++ 5.3.1 &ndash; your mileage may vary!) for the 100,000,000 conversions are:</p>

<figure class='code'><figcaption><span>execution times for various string-to-uint64 implementations</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>test 'std::stoull'       string '1'                           1209 ms
</span><span class='line'>test 'std::stoull'       string '99'                          1382 ms
</span><span class='line'>test 'std::stoull'       string '1234'                        1725 ms
</span><span class='line'>test 'std::stoull'       string '1234567'                     2257 ms
</span><span class='line'>test 'std::stoull'       string '1234567891'                  2764 ms
</span><span class='line'>test 'std::stoull'       string '12345678901234'              3899 ms
</span><span class='line'>test 'std::stoull'       string '12345678901234678901'        7391 ms
</span><span class='line'>
</span><span class='line'>test 'std::strtoull'     string '1'                           1104 ms
</span><span class='line'>test 'std::strtoull'     string '99'                          1300 ms
</span><span class='line'>test 'std::strtoull'     string '1234'                        1628 ms
</span><span class='line'>test 'std::strtoull'     string '1234567'                     2428 ms
</span><span class='line'>test 'std::strtoull'     string '1234567891'                  2662 ms
</span><span class='line'>test 'std::strtoull'     string '12345678901234'              3705 ms
</span><span class='line'>test 'std::strtoull'     string '12345678901234678901'        6631 ms
</span><span class='line'>
</span><span class='line'>test 'naive'             string '1'                            202 ms
</span><span class='line'>test 'naive'             string '99'                           314 ms
</span><span class='line'>test 'naive'             string '1234'                         475 ms
</span><span class='line'>test 'naive'             string '1234567'                      732 ms
</span><span class='line'>test 'naive'             string '1234567891'                   987 ms
</span><span class='line'>test 'naive'             string '12345678901234'              1343 ms
</span><span class='line'>test 'naive'             string '12345678901234678901'        1862 ms
</span><span class='line'>
</span><span class='line'>test 'bitshift'          string '1'                            188 ms
</span><span class='line'>test 'bitshift'          string '99'                           245 ms
</span><span class='line'>test 'bitshift'          string '1234'                         397 ms
</span><span class='line'>test 'bitshift'          string '1234567'                      462 ms
</span><span class='line'>test 'bitshift'          string '1234567891'                   666 ms
</span><span class='line'>test 'bitshift'          string '12345678901234'               888 ms
</span><span class='line'>test 'bitshift'          string '12345678901234678901'        1277 ms
</span><span class='line'>
</span><span class='line'>test 'unrolled'          string '1'                            289 ms
</span><span class='line'>test 'unrolled'          string '99'                           289 ms
</span><span class='line'>test 'unrolled'          string '1234'                         351 ms
</span><span class='line'>test 'unrolled'          string '1234567'                      408 ms
</span><span class='line'>test 'unrolled'          string '1234567891'                   547 ms
</span><span class='line'>test 'unrolled'          string '12345678901234'               778 ms
</span><span class='line'>test 'unrolled'          string '12345678901234678901'        1068 ms</span></code></pre></td></tr></table></div></figure>


<p>It&rsquo;s no surprise the standard library functions with their generality and
error checking features are slower than the specialized functions that took
the liberty to ignore all that.</p>

<p>Which method is fastest now?</p>

<p>As can be seen above, the &ldquo;bitshift&rdquo; variant was fastest for short and medium
length inputs in my environment. At some point when input values get longer,
the &ldquo;bitshift&rdquo; methods gets overtaken by the &ldquo;unrolled&rdquo; variant. The &ldquo;naive&rdquo;
variant was slower than the two in most cases, but still performs surprisingly
well.</p>

<h2>Conclusion</h2>

<p>The take-away: even though string-to-number conversion routines are
present in the standard library, it can still be beneficial to hand-craft
specialized variants of them for maximum performance. This is especially true
when most of the generality that the standard library routines provide is not
required in your use case.</p>

<p>For example, if you know that the conversion functions will only operate on
&ldquo;trusted&rdquo; input (only numeric digit input characters, length of input string
won&rsquo;t exceed the maximum length of a uint64_t number etc.) then error checking
is not required.</p>

<p>Additionally, if you can limit yourself to numbers of a specific base and
don&rsquo;t negative base conversion nor the handling of negative values, a lot of
the generality and safety overhead of std::stoull may be avoided.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compiling ArangoDB 3.0 on Ubuntu]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/02/compiling-arangodb-3-dot-0-on-ubuntu/"/>
    <updated>2016-06-02T20:15:01+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/02/compiling-arangodb-3-dot-0-on-ubuntu</id>
    <content type="html"><![CDATA[<p>We have spent a lot of time working on ArangoDB 3.0. That
version will not only provide major functionality and performance
improvements, but will also come with an improved, CMake-based
build system.</p>

<p>This post explains how to use CMake to build ArangoDB 3.0 on a recent
Ubuntu Linux. For the impatient there&rsquo;s a command summary at the end of
this post.</p>

<!-- more -->


<h2>Installing prerequisites</h2>

<p>Here&rsquo;s how to build ArangoDB 3.0 with cmake on a recent Ubuntu Linux.</p>

<p>Ubuntu 15.x and 16.x should have recent enough packages so any missing
prerequisites can be installed via a simple <code>sudo apt-get install</code> command:</p>

<figure class='code'><figcaption><span>installing prerequisites</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>sudo apt-get install cmake make build-essential openssl python2.7 g++ gcc
</span></code></pre></td></tr></table></div></figure>


<p>Older versions of Ubuntu can be convinced to work too, but this requires
a g++ version of at least 4.9. Ubuntu 14 ships with older g++
versions by default, so you will first need to install a newer g++ version
(see <a href="http://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu-14-04">here</a> or
<a href="http://askubuntu.com/questions/428198/getting-installing-gcc-g-4-9-on-ubuntu">here</a> for
some external instructions). Once the g++ and gcc compilers are recent enough,
install the other prerequisites.</p>

<h2>Cloning ArangoDB</h2>

<p>After having installed the prerequisites, clone the ArangoDB repository
from Github and then cd into the directory <code>arangodb</code> that the cloning
will have created:</p>

<figure class='code'><figcaption><span>cloning ArangoDB</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git clone https://github.com/arangodb/arangodb
</span><span class='line'><span class="nb">cd </span>arangodb
</span></code></pre></td></tr></table></div></figure>


<p>Then check out the 3.0 branch and pull the latest changes:</p>

<figure class='code'><figcaption><span>cloning ArangoDB</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git checkout 3.0
</span><span class='line'>git pull
</span></code></pre></td></tr></table></div></figure>


<h2>Building ArangoDB</h2>

<p>A convention when using CMake is to not build directly in the source
directory, but use a separate build directory instead. The benefit of
this is that building in a separate directory won&rsquo;t change the source
directory, and the build directory can be disposed easily.</p>

<p>To create an initial build directory named <code>build</code> and cd into it,
just type:</p>

<figure class='code'><figcaption><span>creating a build directory</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>mkdir -p build
</span><span class='line'><span class="nb">cd </span>build
</span></code></pre></td></tr></table></div></figure>


<p>It&rsquo;s now time to invoke CMake. CMake should be executed from the build
directory, but needs to know where it&rsquo;s build instructions file
(named <code>CMakeLists.txt</code> is). This file is located in the source directory.
To run CMake without any specific options, we can use:</p>

<figure class='code'><figcaption><span>running CMake</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>cmake -DCMAKE_BUILD_TYPE<span class="o">=</span>RelWithDebInfo ..
</span></code></pre></td></tr></table></div></figure>


<p>This will run CMake from inside the <code>build</code> directory and tell it to
look for <code>CMakeLists.txt</code> in the source directory. The
<code>-DCMAKE_BUILD_TYPE=RelWithDebInfo</code> part will tell CMake to build
binaries with optimizations and debug symbols. Other useful build
types are:</p>

<ul>
<li><code>-DCMAKE_BUILD_TYPE=Debug</code>: without optimizations, for debugging only</li>
<li><code>-DCMAKE_BUILD_TYPE=Release</code>: with optimizations, no debug symbols,
not useful for debugging</li>
</ul>


<p>Invoking CMake will perform some checks for required components, compiler
and platform features. If anything fails, it should print some error
message. If everything works well, the command should have created a
<code>Makefile</code> in the <code>build</code> directory.</p>

<p><em>side note: CMake on Linux will by default generate UNIX <code>Makefile</code>s,
but it can also be told to generate <a href="https://ninja-build.org/">ninja</a>
files instead. On Windows it can generate solution files for Visual
Studio.</em></p>

<p>Now that the <code>Makefile</code> is there, we can run <code>make</code> as usual (note that
the <code>-j4</code> means to build with 4 parallel processes &ndash; adjust as needed!):</p>

<figure class='code'><figcaption><span>running make</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>make -j4
</span></code></pre></td></tr></table></div></figure>


<h2>Starting arangod and arangosh</h2>

<p>And that&rsquo;s already it for a normal build. Note that the build artefacts
and thus the binaries will also be created inside the <code>build</code> directory.
<code>arangod</code> and <code>arangosh</code> will be located in the <code>bin</code> subdirectory of
the <code>build</code> directory. There is no need to install these binaries, they
can be run without installing them first. However, starting them from
inside the build directory will not work, because they will complain
about missing files. It is better to cd one level up first and then
start them:</p>

<figure class='code'><figcaption><span>starting arangod</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nb">cd</span> ..
</span><span class='line'>build/bin/arangod ~/testdata --server.authentication <span class="nb">false</span>
</span></code></pre></td></tr></table></div></figure>


<p>The former will start arangod with default configuration, with a fresh
database directory located in <code>~/testdata</code>.</p>

<p>If you ran all the commands on your local machine (127.0.0.1) then you
can now open a browser and point it to <a href="http://127.0.0.1:8529/">http://127.0.0.1:8529/</a>.
This should bring up ArangoDB&rsquo;s web interface.</p>

<p>You can alternatively start an ArangoShell to connect to the ArangoDB
server. In another terminal window, cd into the <code>arangodb</code> directory and
start an ArangoShell as follows:</p>

<figure class='code'><figcaption><span>starting arangosh</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nb">cd </span>arangodb
</span><span class='line'>build/bin/arangosh
</span></code></pre></td></tr></table></div></figure>


<p>You should now see the ArangoShell prompt and be able to type some
commands!</p>

<h2>Command summary</h2>

<p>Note: in contrast to the above step-by-step instructions, some commands
here have been put together on a single line to make developer&rsquo;s life
easier.</p>

<figure class='code'><figcaption><span>installing prerequisites</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>sudo apt-get install cmake make build-essential openssl python2.7 g++ gcc
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>initial checkout of the 3.0 branch and build configuration</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git clone https://github.com/arangodb/arangodb
</span><span class='line'><span class="nb">cd </span>arangodb
</span><span class='line'>git checkout 3.0
</span><span class='line'>git pull
</span><span class='line'>mkdir -p build
</span><span class='line'><span class="o">(</span><span class="nb">cd </span>build <span class="o">&amp;&amp;</span> cmake -DCMAKE_BUILD_TYPE<span class="o">=</span>RelWithDebInfo ..<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>Update, build and run cycle </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git pull
</span><span class='line'><span class="o">(</span><span class="nb">cd </span>build <span class="o">&amp;&amp;</span> make -j4<span class="o">)</span>
</span><span class='line'>build/bin/arangod ~/testdata --server.authentication <span class="nb">false</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compiling V8 With G++6]]></title>
    <link href="http://jsteemann.github.io/blog/2016/06/02/compiling-v8-with-g-plus-plus-6/"/>
    <updated>2016-06-02T20:14:46+02:00</updated>
    <id>http://jsteemann.github.io/blog/2016/06/02/compiling-v8-with-g-plus-plus-6</id>
    <content type="html"><![CDATA[<p>With g++ 6 becoming more and more adopted, it&rsquo;s about time to point out an issue
that hit some of us ArangoDB developers and a few of our users that compile ArangoDB
from source with g++ 6.</p>

<p>The problem is that when compiling ArangoDB with g++6 with default options, arangod
starts and almost immediately segfaults.</p>

<!-- more -->


<p>A backtrace of the crashed arangod process shows that the segfaults originate from
the V8 JavaScript engine that ArangoDB uses internally.
The problem seems to have affected other users of V8 as well, indicated by the following
error reports:</p>

<ul>
<li>node.js: <a href="https://github.com/nodejs/node/issues/6724">https://github.com/nodejs/node/issues/6724</a></li>
<li>chromium: <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68853">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68853</a></li>
</ul>


<p>The reason for the error now popping up is a change in the g++ 6 optimizer as stated
in the <a href="https://gcc.gnu.org/gcc-6/changes.html">gcc6 release notes</a>:</p>

<blockquote><p>Value range propagation now assumes that the this pointer of C++ member functions is non-null.
This eliminates common null pointer checks but also breaks some non-conforming code-bases (such as Qt-5, Chromium, KDevelop).
As a temporary work-around -fno-delete-null-pointer-checks can be used. Wrong code can be identified by using -fsanitize=undefined.</p></blockquote>

<p>That means if compiling ArangoDB 2.8 from source with g++ 6 or higher, please be sure to
set the environment variables <code>CFLAGS="-fno-delete-null-pointer-checks"</code> and<br/>
<code>CXXFLAGS="-fno-delete-null-pointer-checks"</code> before invoking the <code>configure</code> command:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>make setup
</span><span class='line'><span class="nv">CFLAGS</span><span class="o">=</span><span class="s2">&quot;-fno-delete-null-pointer-checks&quot;</span> <span class="nv">CXXFLAGS</span><span class="o">=</span><span class="s2">&quot;-fno-delete-null-pointer-checks&quot;</span> ./configure
</span><span class='line'>make
</span></code></pre></td></tr></table></div></figure>


<p>The options will then be passed to the sub-make that builds the V8 engine. If you have
already built V8 without these options, try removing the file <code>.v8-build-64</code> from the
build directory and run <code>configure</code> and <code>make</code> again.</p>

<p>As a reminder, ArangoDB 2.8 will also emit this big notice at the end of its <code>configure</code> output:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>configure: --------------------------------------------------------------------------------
</span><span class='line'>configure:                                                                                 
</span><span class='line'>configure: NOTE: if you are compiling ArangoDB with g++ 6.0 or higher, please make sure to 
</span><span class='line'>configure: set the following environment variables when compiling ArangoDB:                
</span><span class='line'>configure:                                                                                 
</span><span class='line'>configure:   CFLAGS="-fno-delete-null-pointer-checks"                                      
</span><span class='line'>configure:   CXXFLAGS="-fno-delete-null-pointer-checks"                                    
</span><span class='line'>configure:                                                                                 
</span><span class='line'>configure: --------------------------------------------------------------------------------</span></code></pre></td></tr></table></div></figure>


<p>Users of g++ 5.x do not need to set these flags, neither need users of clang++. And in the
ArangoDB 3.0 build the options will be set automatically when compiling V8 if the compiler
is g++. They will not be set for compiling any other parts of ArangoDB as it&rsquo;s only required
for building V8.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Killing a Long-running Query]]></title>
    <link href="http://jsteemann.github.io/blog/2016/01/26/killing-a-long-running-query/"/>
    <updated>2016-01-26T23:13:33+01:00</updated>
    <id>http://jsteemann.github.io/blog/2016/01/26/killing-a-long-running-query</id>
    <content type="html"><![CDATA[<p>Suppose there is an AQL query that&rsquo;s executing in the server
for a long time already and you want to get rid of it. What can
be done to abort that query?</p>

<p>If a connection to the server can still be established, the easiest
is to use the ArangoShell to fetch the list of currently executing
AQL queries and send a <em>kill</em> command to the server for the correct query.</p>

<!-- more -->


<p>To start, we can fetch the list of all running queries and print
their ids, query strings and runtimes. This is only inspection and does
not abort any query:</p>

<figure class='code'><figcaption><span>printing all currently running queries</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">queries</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;org/arangodb/aql/queries&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nx">queries</span><span class="p">.</span><span class="nx">current</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here&rsquo;s an example result for the list of running queries:</p>

<figure class='code'><figcaption><span>example list of currently running queries</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="p">[</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="nt">&quot;id&quot;</span> <span class="p">:</span> <span class="s2">&quot;190&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;query&quot;</span> <span class="p">:</span> <span class="s2">&quot;RETURN SLEEP(1000)&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;started&quot;</span> <span class="p">:</span> <span class="s2">&quot;2016-01-26T22:41:24Z&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;runTime&quot;</span> <span class="p">:</span> <span class="mf">218.49146389961243</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>To now kill a query from the list, we can pass the query&rsquo;s id to <em>kill</em>:</p>

<figure class='code'><figcaption><span>killing a specific query</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">queries</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;org/arangodb/aql/queries&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nx">queries</span><span class="p">.</span><span class="nx">kill</span><span class="p">(</span><span class="s2">&quot;190&quot;</span><span class="p">);</span>  <span class="cm">/* insert actual query id here */</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<p>If a query was actually killed on the server, that call should return without
an error, and the server should have logged a warning in addition.</p>

<p>If we wanted to abort one or many queries from the list solely by
looking at query string patterns or query runtime, we could iterate
over the list of current queries and kill each one that matches
a predicate.</p>

<p>For example, the following snippet will abort all currently running
queries that contain the string <code>SLEEP</code> anywhere inside their query string:</p>

<figure class='code'><figcaption><span>aborting all queries containing the word SLEEP inside the query string</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">queries</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;org/arangodb/aql/queries&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">queries</span><span class="p">.</span><span class="nx">current</span><span class="p">().</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">query</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">query</span><span class="p">.</span><span class="nx">query</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/SLEEP/</span><span class="p">);</span>  <span class="cm">/* predicate based on query string */</span>
</span><span class='line'><span class="p">}).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">query</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">print</span><span class="p">(</span><span class="s2">&quot;killing query: &quot;</span><span class="p">,</span> <span class="nx">query</span><span class="p">);</span>    <span class="cm">/* print what we&#39;re killing */</span>
</span><span class='line'>  <span class="nx">queries</span><span class="p">.</span><span class="nx">kill</span><span class="p">(</span><span class="nx">query</span><span class="p">.</span><span class="nx">id</span><span class="p">);</span>             <span class="cm">/* actually kill query */</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Filtering based on current query runtime is also simple, by adjusting the
predicate. To abort all queries that ran longer than 30 seconds use:</p>

<figure class='code'><figcaption><span>aborting all queries running at least 30 seconds</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">queries</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;org/arangodb/aql/queries&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">queries</span><span class="p">.</span><span class="nx">current</span><span class="p">().</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">query</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">query</span><span class="p">.</span><span class="nx">runTime</span> <span class="o">&gt;</span> <span class="mi">30</span><span class="p">;</span>          <span class="cm">/* predicate based on query runtime */</span>
</span><span class='line'><span class="p">}).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">query</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">print</span><span class="p">(</span><span class="s2">&quot;killing query: &quot;</span><span class="p">,</span> <span class="nx">query</span><span class="p">);</span>    <span class="cm">/* print what we&#39;re killing */</span>
</span><span class='line'>  <span class="nx">queries</span><span class="p">.</span><span class="nx">kill</span><span class="p">(</span><span class="nx">query</span><span class="p">.</span><span class="nx">id</span><span class="p">);</span>             <span class="cm">/* actually kill query */</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Please make sure the predicates are correct so only the actually intended
queries get aborted!</p>

<p>To test a predicate without killing a query, use the above code without the
<code>forEach</code> part that did the killing.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Small Things in 2.8: Explain Improvements]]></title>
    <link href="http://jsteemann.github.io/blog/2016/01/26/small-things-in-28-explain-improvements/"/>
    <updated>2016-01-26T22:08:11+01:00</updated>
    <id>http://jsteemann.github.io/blog/2016/01/26/small-things-in-28-explain-improvements</id>
    <content type="html"><![CDATA[<p>Explaining AQL queries becomes even easier in ArangoDB 2.8.</p>

<p>While previous versions required writing a hard-to-memoize command like</p>

<figure class='code'><figcaption><span>explaining a query in 2.7</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">require</span><span class="p">(</span><span class="s2">&quot;org/arangodb/aql/explainer&quot;</span><span class="p">).</span><span class="nx">explain</span><span class="p">(</span><span class="nx">query</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<p>to explain an AQL query from the ArangoShell, 2.8 reduces this task to
a mere</p>

<figure class='code'><figcaption><span>explaining a query in 2.8</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">_explain</span><span class="p">(</span><span class="nx">query</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Apart from that, explain in 2.8 is smarter when confronted with very lengthy
query strings, and with queries that contain huge hard-coded string, array,
or object values.</p>

<!-- more -->


<p>For example, when creating an array bind variable with 1,000 values and
using them in an explained query, 2.7 would print the entire 1,000 array values
in the explain output:</p>

<figure class='code'><figcaption><span>explaining a query with 1000 array values</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">keys</span> <span class="o">=</span> <span class="p">[];</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="mi">1000</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">keys</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s2">&quot;test&quot;</span> <span class="o">+</span> <span class="nx">i</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">query</span> <span class="o">=</span> <span class="s2">&quot;FOR i IN @keys RETURN i&quot;</span><span class="p">;</span>
</span><span class='line'><span class="nx">require</span><span class="p">(</span><span class="s2">&quot;org/arangodb/aql/explainer&quot;</span><span class="p">).</span><span class="nx">explain</span><span class="p">({</span>
</span><span class='line'>  <span class="nx">query</span><span class="o">:</span> <span class="nx">query</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">bindVars</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">keys</span><span class="o">:</span> <span class="nx">keys</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p><img src="http://jsteemann.github.io/downloads/screenshots/explain-27.png"></p>

<p>2.8 will instead truncate longer arrays and objects in the explain output for
much improved readability:</p>

<p><img src="http://jsteemann.github.io/downloads/screenshots/explain-28.png"></p>

<p>Automatic value truncation will occur for array and object values with
more than 20 elements or for string values longer than 1,024 characters. The
truncation for explain will occur if these values are hard-coded into the
query or are passed via bind parameters.</p>

<p>Truncation only happens inside the explain results processing and thus
cannot affect the actual query results.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Small Things in 2.8: POW]]></title>
    <link href="http://jsteemann.github.io/blog/2016/01/26/small-things-in-28-pow/"/>
    <updated>2016-01-26T21:59:17+01:00</updated>
    <id>http://jsteemann.github.io/blog/2016/01/26/small-things-in-28-pow</id>
    <content type="html"><![CDATA[<p>ArangoDB 2.8 now provides a dedicated AQL function for exponentiation.
This will save users a lot of trouble in case exponentiation is needed
inside an AQL query, which up to 2.7 required writing and registering an
AQL user-defined function.</p>

<p>With 2.8 it becomes as simple as <code>RETURN POW(2, 16)</code> to raise <em>2</em>
to the power of <em>16</em> from inside AQL.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Small Things in 2.8: Collection Type for Arangoimp]]></title>
    <link href="http://jsteemann.github.io/blog/2016/01/26/small-things-in-28-arangoimp-collection-type/"/>
    <updated>2016-01-26T21:59:17+01:00</updated>
    <id>http://jsteemann.github.io/blog/2016/01/26/small-things-in-28-arangoimp-collection-type</id>
    <content type="html"><![CDATA[<p>When trying to import data into ArangoDB from a JSON or CSV file using the
<em>arangoimp</em> binary, there is always the chance that the target collection does
not yet exist.</p>

<p>In order to create a missing target collection <em>arangoimp</em> for long has
provided the option <code>--create-collection true</code>:</p>

<figure class='code'><figcaption><span>importing, optionally creating a missing collection</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>arangoimp                       <span class="se">\</span>
</span><span class='line'>  --file users.json             <span class="se">\</span>
</span><span class='line'>  --collection users            <span class="se">\</span>
</span><span class='line'>  --create-collection <span class="nb">true</span>
</span></code></pre></td></tr></table></div></figure>


<p>However there hasn&rsquo;t been a way of specifying the <strong>type</strong> for the target collection,
so the new collection was always created as <em>document</em> collection.</p>

<p>To import data into an <em>edge</em> collection, the target collection needed to be created
by another means, e.g. by using the ArangoShell. It would have been more handy if
<em>arangoimp</em> were able to create <em>edge</em> collections too.</p>

<p>2.8 finally adds that feature, and it&rsquo;s simple to use: to create an <em>edge</em> collection
if the target collection does not exist, append the <code>--create-collection-type edge</code>
option when invoking <em>arangoimp</em>:</p>

<figure class='code'><figcaption><span>importing, optionally creating a missing edge collection</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>arangoimp                       <span class="se">\</span>
</span><span class='line'>  --file users.json             <span class="se">\</span>
</span><span class='line'>  --collection users            <span class="se">\</span>
</span><span class='line'>  --create-collection <span class="nb">true</span>      <span class="se">\</span>
</span><span class='line'>  --create-collection-type edge
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[AQL Optimizer Improvements for 2.8]]></title>
    <link href="http://jsteemann.github.io/blog/2015/12/22/aql-optimizer-improvements-for-28/"/>
    <updated>2015-12-22T22:39:30+01:00</updated>
    <id>http://jsteemann.github.io/blog/2015/12/22/aql-optimizer-improvements-for-28</id>
    <content type="html"><![CDATA[<p>With the 2.8 beta phase coming to an end it&rsquo;s time to shed some light
on the improvements in the 2.8 AQL optimizer. This blog post summarizes
a few of them, focusing on the query optimizer. There&rsquo;ll be a follow-up
post that will explain dedicated new AQL features soon.</p>

<!-- more -->


<h2>Array indexes</h2>

<p>2.8 allows creating hash and skiplist indexes on attributes which are arrays.
Creating such index works similar to creating a non-array index, with the
exception that the name of the array attribute needs to be followed by a <code>[*]</code>
in the index fields definition:</p>

<figure class='code'><figcaption><span>creating an array index</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">_create</span><span class="p">(</span><span class="s2">&quot;posts&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">ensureIndex</span><span class="p">({</span> <span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;hash&quot;</span><span class="p">,</span> <span class="nx">fields</span><span class="o">:</span> <span class="p">[</span> <span class="s2">&quot;tags[*]&quot;</span> <span class="p">]</span> <span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now if the <code>tags</code> attribute of a document in the <code>posts</code> collection is an array,
each array member will be inserted into the index:</p>

<figure class='code'><figcaption><span>storing an array value</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s2">&quot;arangodb&quot;</span><span class="p">,</span> <span class="s2">&quot;database&quot;</span><span class="p">,</span> <span class="s2">&quot;aql&quot;</span> <span class="p">]</span> <span class="p">});</span>
</span><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s2">&quot;arangodb&quot;</span><span class="p">,</span> <span class="s2">&quot;v8&quot;</span><span class="p">,</span> <span class="s2">&quot;javascript&quot;</span> <span class="p">]</span> <span class="p">});</span>
</span><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s2">&quot;javascript&quot;</span><span class="p">,</span> <span class="s2">&quot;v8&quot;</span><span class="p">,</span> <span class="s2">&quot;nodejs&quot;</span> <span class="p">]</span> <span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>The index on <code>tags[*]</code> will now contain the values <code>arangodb</code>, <code>database</code>, <code>aql</code> and
<code>nosql</code> for the first document, <code>arangodb</code>, <code>v8</code> and <code>javascript</code> for the second, and
<code>javascript</code>, <code>v8</code> and <code>nodejs</code> for the third.</p>

<p>The following AQL will find any documents that have a value of <code>javascript</code> contained
in their <code>tags</code> value:</p>

<figure class='code'><figcaption><span>array index query</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>FOR doc IN posts
</span><span class='line'>  FILTER 'javascript' IN doc.tags[*]
</span><span class='line'>  RETURN doc</span></code></pre></td></tr></table></div></figure>


<p>This will use the array index on <code>tags[*]</code>.</p>

<p>The array index works by inserting all members from an array into the index
separately. Duplicates are removed automatically when populating the index.</p>

<p>An array index can also be created on a sub-attribute of array members. For
example, the following definition will make sure the <code>name</code> sub-attributes of
the <code>tags</code> array values will make it into the index:</p>

<figure class='code'><figcaption><span>creating an array index on a sub-attribute</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">ensureIndex</span><span class="p">({</span> <span class="nx">type</span><span class="o">:</span> <span class="s2">&quot;hash&quot;</span><span class="p">,</span> <span class="nx">fields</span><span class="o">:</span> <span class="p">[</span> <span class="s2">&quot;tags[*].name&quot;</span> <span class="p">]</span> <span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>That will allow storing data as follows:</p>

<figure class='code'><figcaption><span>storing an array value</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;arangodb&quot;</span> <span class="p">},</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;database&quot;</span> <span class="p">},</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;aql&quot;</span> <span class="p">}</span> <span class="p">]</span> <span class="p">});</span>
</span><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;arangodb&quot;</span> <span class="p">},</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;v8&quot;</span> <span class="p">},</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;javascript&quot;</span> <span class="p">}</span> <span class="p">]</span> <span class="p">});</span>
</span><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;javascript&quot;</span> <span class="p">},</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;v8&quot;</span> <span class="p">},</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;nodejs&quot;</span> <span class="p">}</span> <span class="p">]</span> <span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>The (index-based) selection query for this data structure then becomes</p>

<figure class='code'><figcaption><span>array index query using sub-attributes</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>FOR doc IN posts
</span><span class='line'>  FILTER 'javascript' IN doc.tags[*].name
</span><span class='line'>  RETURN doc</span></code></pre></td></tr></table></div></figure>


<p>Contrary to MongoDB, there is no automatic conversion to array values when
inserting non-array values in ArangoDB. For example, the following plain strings
will not be inserted into an array index, simply because the value of the index
attribute is not an array:</p>

<figure class='code'><figcaption><span>storing non array values without indexing</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="s2">&quot;arangodb&quot;</span> <span class="p">});</span>
</span><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="s2">&quot;javascript&quot;</span> <span class="p">});</span>
</span><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">posts</span><span class="p">.</span><span class="nx">insert</span><span class="p">({</span> <span class="nx">tags</span><span class="o">:</span> <span class="s2">&quot;nodejs&quot;</span> <span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that in this case a non-array index can still be used.</p>

<h2>Use of multiple indexes per collection</h2>

<p>The query optimizer can now make use of multiple indexes if multiple filter
conditions are combined with logical ORs, and all of them are covered by
indexes of the same collection.</p>

<p>Provided there are separate indexes present on <code>name</code> and <code>status</code>, the
following query can make use of index scans in 2.8, as opposed to full
collection scans in 2.7:</p>

<figure class='code'><figcaption><span>using multiple indexes</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>FOR doc IN users 
</span><span class='line'>  FILTER doc.name == 'root' || doc.status == 'active' 
</span><span class='line'>  RETURN doc</span></code></pre></td></tr></table></div></figure>


<p><img src="http://jsteemann.github.io/downloads/screenshots/multiple-indexes.png"></p>

<p>If multiple filter conditions match for the same document, the result will
automatically be deduplicated, so each document is still returned at most once.</p>

<h2>Sorted IN comparison</h2>

<p>Another improvement for the optimizer is to pre-sort comparison values for <code>IN</code> and
<code>NOT IN</code> so these operators can use a (much faster) binary search instead of a linear search.</p>

<p>The optimization will be applied automatically for <code>IN</code> / <code>NOT IN</code> comparison values used
in filters, which are used inside of a <code>FOR</code> loop, and depend on runtime values. For example,
the optimization will be applied for the following query:</p>

<figure class='code'><figcaption><span>using multiple indexes</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>LET values = /* some runtime expression here */
</span><span class='line'>FOR doc IN collection
</span><span class='line'>  FILTER doc.value IN values
</span><span class='line'>  RETURN doc</span></code></pre></td></tr></table></div></figure>


<p>The optimization will not be applied for <code>IN</code> comparison values that are value
literals and those that are used in index lookups. For these cases the comparison values
were already deduplicated and sorted.</p>

<p>&ldquo;sort-in-values&rdquo; will appear in the list of applied optimizer rules if the optimizer
could apply the optimization:</p>

<p><img src="http://jsteemann.github.io/downloads/screenshots/sorted-in.png"></p>

<h2>Optimization for LENGTH(collection)</h2>

<p>There are multiple ways for counting the total number of documents in a collection from
inside an AQL query. One obvious way is to use <code>RETURN LENGTH(collection)</code>.</p>

<p>That variant however was inefficient as it fully materialized the documents before
counting them. In 2.8 calling <code>LENGTH()</code> for a collection will get automatically replaced
by a call to a special function that can efficiently determine the number of documents.
For larger collections, this can be several thousand times faster than the naive 2.7
solution.</p>

<h2>C++ implementation for many AQL functions</h2>

<p>Many existing AQL functions have been backed with a C++ implementation that removes
the need for some data conversion that would otherwise happen if the function were
implemented in V8/JavaScript only. More than 30+ functions have been changed, including
several that may produce bigger result sets (such as <code>EDGES()</code>, <code>FULLTEXT()</code>, <code>WITHIN()</code>,
<code>NEAR()</code>) and that will hugely benefit from this.</p>

<h2>Improved skip performance</h2>

<p>2.8 improves the performance of skipping over many documents in case no indexes and no
filters are used. This might sound like an edge case, but it is quite common when the
task is to fetch documents from a big collection in chunks and there is certainty that
there will be no parallel modifications.</p>

<p>For example, the following query runs about 3 to 5 times faster in 2.8, and this
improvements can easily sum up to notable speedups if the query is called repeatedly
with increasing offset values for <code>LIMIT</code>:</p>

<figure class='code'><figcaption><span>query with huge skip</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>FOR doc IN collection
</span><span class='line'>  LIMIT 1000000, 10
</span><span class='line'>  RETURN doc</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[AQL Function Speedups]]></title>
    <link href="http://jsteemann.github.io/blog/2015/11/20/aql-function-speedups/"/>
    <updated>2015-11-20T16:55:34+01:00</updated>
    <id>http://jsteemann.github.io/blog/2015/11/20/aql-function-speedups</id>
    <content type="html"><![CDATA[<p>While working on the upcoming ArangoDB 2.8, we have reimplemented some AQL
functions in C++ for improved performance. AQL queries using these functions
may benefit from using the new implementation of the function.</p>

<p>The following list shows the AQL functions for which a C++ implementation has
been added in 2.8. The other C++-based AQL function implementations added since
ArangoDB 2.5 are also still available. Here&rsquo;s the list of functions added in 2.8:</p>

<!-- more -->


<ul>
<li>document-related functions: DOCUMENT, EDGES, PARSE_IDENTIFIER</li>
<li>numerical functions: ABS, FLOOR, RAND, ROUND, SQRT</li>
<li>statistical functions: MEDIAN, PERCENTILE, STDDEV_POPULATION, STDDEV_SAMPLE, VARIANCE_POPULATION, VARIANCE_SAMPLE</li>
<li>geo functions: NEAR, WITHIN</li>
<li>array functions: APPEND, FIRST, FLATTEN, LAST, MINUS, NTH, POP, POSITION, PUSH, REMOVE_NTH, REMOVE_VALUE, REMOVE_VALUES, SHIFT, UNSHIFT</li>
<li>informational functions: COLLECTIONS, CURRENT_DATABASE, FIRST_DOCUMENT, FIRST_LIST, NOT_NULL</li>
<li>object-related functions: MERGE_RECURSIVE, ZIP</li>
</ul>


<p>Following are a few example queries that benefit from using the C++ variants of some
of the above functions:</p>

<h3>Fetching documents programmatically using the <code>DOCUMENT</code> function:</h3>

<ul>
<li>query: <code>FOR i IN 1..10000 RETURN DOCUMENT(test, CONCAT('test', i))</code></li>
<li>2.7: 0.3005 s</li>
<li>2.8: 0.1050 s</li>
</ul>


<h3>Fetching edges programmatically using the <code>EDGES</code> function:</h3>

<ul>
<li>query: <code>FOR i IN 1..100000 RETURN EDGES(edges, CONCAT('test/test', i), 'outbound')</code>:</li>
<li>2.7: 4.3590 s</li>
<li>2.8: 1.4469 s</li>
</ul>


<h3>Fetching many documents from a geo index, post-filtering most of them:</h3>

<ul>
<li>query: <code>FOR doc IN WITHIN(locations, 0, 0, 100000) FILTER doc.value2 == 'test1001' LIMIT 1 RETURN doc</code></li>
<li>2.7: 2.9876 s</li>
<li>2.8: 0.4087 s</li>
</ul>


<h3>Generating random numbers:</h3>

<ul>
<li>query: <code>FOR value IN 1..100000 RETURN RAND() * 50</code></li>
<li>2.7: 0.1743 s</li>
<li>2.8: 0.1364 s</li>
</ul>


<p>Please note that not in every case there will be a tremendous speedup. As usual,
it depends on how often a function is called inside a query and what other constructs
are used. Your mileage may vary.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Multiple Indexes Per Collection]]></title>
    <link href="http://jsteemann.github.io/blog/2015/11/20/using-multiple-indexes-per-collection/"/>
    <updated>2015-11-20T15:30:05+01:00</updated>
    <id>http://jsteemann.github.io/blog/2015/11/20/using-multiple-indexes-per-collection</id>
    <content type="html"><![CDATA[<p>The query optimizer in ArangoDB 2.8 has been improved in terms of how it can
make use of indexes. In previous versions of ArangoDB, the query optimizer could
use only one index per collection used in an AQL query. When using a logical OR
in a FILTER condition, the optimizer did not use any index for the collection in
order to ensure the result is still correct.</p>

<p>This is much better in 2.8. Now the query optimizer can use multiple indexes on
the same collection for FILTER conditions that are combined with a logical OR.</p>

<!-- more -->


<p>For all following queries, I have set up a collection named <code>test</code>, which has
two isolated hash indexes on the attributes <code>value1</code> and <code>value2</code>, and a skiplist
index on attribute <code>value3</code>.</p>

<p>Let&rsquo;s first try an AQL queries that uses a logical OR on two different attributes of the
collection:</p>

<figure class='code'><figcaption><span>example query </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>FOR doc IN test
</span><span class='line'>  FILTER doc.value1 == 11 || doc.value2 == 19 
</span><span class='line'>  RETURN doc</span></code></pre></td></tr></table></div></figure>


<p>The execution plan for this query in 2.7 reveals that query will perform a full
collection scan and cannot use indexes because of the logical OR on two different
attributes:</p>

<figure class='code'><figcaption><span>2.7 query execution plan</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Execution plan:
</span><span class='line'> Id   NodeType                  Est.   Comment
</span><span class='line'>  1   SingletonNode                1   * ROOT
</span><span class='line'>  2   EnumerateCollectionNode      0     - FOR doc IN test   /* full collection scan */
</span><span class='line'>  3   CalculationNode              0       - LET #1 = doc.`value1` == 11 || doc.`value2` == 19
</span><span class='line'>  4   FilterNode                   0       - FILTER #1
</span><span class='line'>  5   ReturnNode                   0       - RETURN doc
</span><span class='line'>
</span><span class='line'>Indexes used:
</span><span class='line'> none
</span><span class='line'>
</span><span class='line'>Optimization rules applied:
</span><span class='line'> none</span></code></pre></td></tr></table></div></figure>


<p></p>

<p>Running the same query in 2.8 / devel will produce a much better execution plan:</p>

<figure class='code'><figcaption><span>2.8 query execution plan</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Execution plan:
</span><span class='line'> Id   NodeType          Est.   Comment
</span><span class='line'>  1   SingletonNode        1   * ROOT
</span><span class='line'>  6   IndexNode            2     - FOR doc IN test   /* hash index scan, hash index scan */
</span><span class='line'>  3   CalculationNode      2       - LET #1 = doc.`value1` == 11 || doc.`value2` == 19  
</span><span class='line'>  4   FilterNode           2       - FILTER #1
</span><span class='line'>  5   ReturnNode           2       - RETURN doc
</span><span class='line'>
</span><span class='line'>Indexes used:
</span><span class='line'> By   Type   Collection   Unique   Sparse   Selectivity   Fields         Ranges
</span><span class='line'>  6   hash   test         false    false       100.00 %   [ `value1` ]   doc.`value1` == 11
</span><span class='line'>  6   hash   test         false    false       100.00 %   [ `value2` ]   doc.`value2` == 19
</span><span class='line'>
</span><span class='line'>Optimization rules applied:
</span><span class='line'> Id   RuleName
</span><span class='line'>  1   use-indexes</span></code></pre></td></tr></table></div></figure>


<p>Multiple indexes will also be used if different index types are accessed, or for non-equality
filter conditions. For example, the following query will make use of the two hash indexes
and also the skiplist index:</p>

<figure class='code'><figcaption><span>example query</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>FOR doc IN test 
</span><span class='line'>  FILTER doc.value1 == 11 || doc.value2 == 19 || doc.value3 &gt; 42 
</span><span class='line'>  RETURN doc</span></code></pre></td></tr></table></div></figure>


<p>Here is its execution plan from 2.8:</p>

<figure class='code'><figcaption><span>2.8 query execution plan</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Execution plan:
</span><span class='line'> Id   NodeType          Est.   Comment
</span><span class='line'>  1   SingletonNode        1   * ROOT
</span><span class='line'>  6   IndexNode            3     - FOR doc IN test   /* hash index scan, hash index scan, skiplist index scan */
</span><span class='line'>  3   CalculationNode      3       - LET #1 = doc.`value1` == 11 || doc.`value2` == 19 || doc.`value3` &gt; 42 
</span><span class='line'>  4   FilterNode           3       - FILTER #1
</span><span class='line'>  5   ReturnNode           3       - RETURN doc
</span><span class='line'>
</span><span class='line'>Indexes used:
</span><span class='line'> By   Type       Collection   Unique   Sparse   Selectivity   Fields         Ranges
</span><span class='line'>  6   hash       test         false    false       100.00 %   [ `value1` ]   doc.`value1` == 11
</span><span class='line'>  6   hash       test         false    false       100.00 %   [ `value2` ]   doc.`value2` == 19
</span><span class='line'>  6   skiplist   test         false    false            n/a   [ `value3` ]   doc.`value3` &gt; 42
</span><span class='line'>
</span><span class='line'>Optimization rules applied:
</span><span class='line'> Id   RuleName
</span><span class='line'>  1   use-indexes</span></code></pre></td></tr></table></div></figure>


<p>For comparison, here is the non-optimized plan from 2.7 for the same query:</p>

<figure class='code'><figcaption><span>2.7 query execution plan</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Execution plan:
</span><span class='line'> Id   NodeType                  Est.   Comment
</span><span class='line'>  1   SingletonNode                1   * ROOT
</span><span class='line'>  2   EnumerateCollectionNode      0     - FOR doc IN test   /* full collection scan */
</span><span class='line'>  3   CalculationNode              0       - LET #1 = doc.`value1` == 11 || doc.`value2` == 19 || doc.`value3` &gt; 42
</span><span class='line'>  4   FilterNode                   0       - FILTER #1
</span><span class='line'>  5   ReturnNode                   0       - RETURN doc
</span><span class='line'>
</span><span class='line'>Indexes used:
</span><span class='line'> none
</span><span class='line'>
</span><span class='line'>Optimization rules applied:
</span><span class='line'> none</span></code></pre></td></tr></table></div></figure>


<p>Still the query optimizer will not be able to use any indexes on a collection when
there are multiple FILTER conditions combined with logical OR and at least one of them
is not satisfisable by an index of the collection. In this case it has no other choice
but to do a full collection scan.</p>

<p>For queries that combine multiple FILTER conditions with a logical AND, the optimizer
will still try to pick the most selective index for the query and use it for the collection.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Index Speedups in 2.8]]></title>
    <link href="http://jsteemann.github.io/blog/2015/11/20/index-speedups-for-28/"/>
    <updated>2015-11-20T15:11:00+01:00</updated>
    <id>http://jsteemann.github.io/blog/2015/11/20/index-speedups-for-28</id>
    <content type="html"><![CDATA[<p>The upcoming 2.8 version of ArangoDB will provide several improvements in
the area of index usage and query optimization.</p>

<p>First of all, hash and skiplist indexes can now index individual array values.
A dedicated post on this will follow shortly. Second, the query optimizer can
make use multiple indexes per collection for queries with OR-combined filter
conditions. This again is a subject for <a href="http://jsteemann.github.io/blog/2015/11/20/using-multiple-indexes-per-collection/">another post</a>.
Third, there have been some speed improvements due to changes in the general
index handling code. This is what this post is about.</p>

<!-- more -->


<p>In order to assess the speedups in 2.8, I have run some already existing performance
tests that I initially ran when comparing ArangoDB 2.5 with 2.6. The test cases and
methodology are detailed in <a href="http://jsteemann.github.io/blog/2015/05/20/the-great-aql-shootout-arangodb-25-vs-26/">this earlier blog post</a>.</p>

<p>For measuring the index-related performance improvements, I simply re-ran the
index related tests in 2.7 and in 2.8 / devel. I did not bother re-running all
tests from the original blog article because only some are index-related. In
particular, I only ran these tests again:</p>

<ul>
<li><em>join-key</em>: for each document in the collection, perform a join on the <code>_key</code> attribute on the collection
 itself (i.e. <code>FOR c1 IN @@c FOR c2 IN @@c FILTER c1._key == c2._key RETURN c1</code>)</li>
<li><em>join-id</em>: ditto, but perform the join using the <code>_id</code> attribute</li>
<li><em>join-hash-number</em> and <em>join-hash-string</em>: ditto, but join using a hash index on a numeric or string
 attribute</li>
<li><em>join-skiplist-number</em> and <em>join-skiplist-string</em>: ditto, but join using a skiplist index on a numeric or
 string attribute</li>
<li><em>lookup-key</em>, <em>lookup-hash-number</em>, <em>lookup-hash-string</em>, <em>lookup-skiplist-number</em>, <em>lookup-skiplist-string</em>:
 compile an IN-list of 10,000 lookup values and search these 10,000 documents in the collection using
 either the primary index (<code>_key</code> attribute), a hash index or a skiplist index. The latter two are tested
 on numeric and string attributes.</li>
</ul>


<p>The test queries were run 5 times each on collections containing 10,000, 100,000 and
1,000,000 documents.</p>

<p>Here are the query execution times from 2.7 and 2.8 for the individual tests, in
seconds (less is better):</p>

<figure class='code'><figcaption><span>test results</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>test name                collection     avg 2.7 (s)      avg 2.8 (s)
</span><span class='line'>--------------------------------------------------------------------
</span><span class='line'>join-key                 10k                 0.0997           0.0612
</span><span class='line'>join-key                 100k                1.0611           0.6538
</span><span class='line'>join-key                 1000k              10.2975           6.2507
</span><span class='line'>join-id                  10k                 0.1088           0.0606
</span><span class='line'>join-id                  100k                1.1126           0.6694
</span><span class='line'>join-id                  1000k              10.8180           6.5336
</span><span class='line'>join-hash-number         10k                 0.1044           0.0679
</span><span class='line'>join-hash-number         100k                1.1137           0.7430
</span><span class='line'>join-hash-number         1000k              10.7923           7.1534
</span><span class='line'>join-hash-string         10k                 0.1193           0.0712
</span><span class='line'>join-hash-string         100k                1.2656           0.7915
</span><span class='line'>join-hash-string         1000k              12.3075           7.6667
</span><span class='line'>join-skiplist-number     10k                 0.1387           0.1030
</span><span class='line'>join-skiplist-number     100k                1.6693           1.3062
</span><span class='line'>join-skiplist-number     1000k              18.0406          15.5203
</span><span class='line'>join-skiplist-string     10k                 0.1928           0.1469
</span><span class='line'>join-skiplist-string     100k                2.3166           1.8997
</span><span class='line'>join-skiplist-string     1000k              27.1513          23.2058
</span><span class='line'>lookup-key               10k                 1.4996           1.5245
</span><span class='line'>lookup-key               100k                1.4951           1.5189
</span><span class='line'>lookup-key               1000k               1.5545           1.5662
</span><span class='line'>lookup-hash-number       10k                 1.5572           1.5526
</span><span class='line'>lookup-hash-number       100k                1.5595           1.5435
</span><span class='line'>lookup-hash-number       1000k               1.5436           1.5648
</span><span class='line'>lookup-hash-string       10k                 1.6023           1.5623
</span><span class='line'>lookup-hash-string       100k                1.5892           1.5741
</span><span class='line'>lookup-hash-string       1000k               1.5841           1.5770
</span><span class='line'>lookup-skiplist-number   10k                 1.5978           1.6145
</span><span class='line'>lookup-skiplist-number   100k                1.5782           1.6269
</span><span class='line'>lookup-skiplist-number   1000k               1.5891           1.6258
</span><span class='line'>lookup-skiplist-string   10k                 1.6443           1.6840
</span><span class='line'>lookup-skiplist-string   100k                1.6787           1.6985
</span><span class='line'>lookup-skiplist-string   1000k               1.7319           1.7562
</span><span class='line'>in-key                   10k                 0.1076           0.0754
</span><span class='line'>in-key                   100k                0.1083           0.0775
</span><span class='line'>in-key                   1000k               0.1104           0.0773
</span><span class='line'>in-hash-number           10k                 0.0898           0.0696
</span><span class='line'>in-hash-number           100k                0.0889           0.0674
</span><span class='line'>in-hash-number           1000k               0.0877           0.0675
</span><span class='line'>in-hash-string           10k                 0.1174           0.0867
</span><span class='line'>in-hash-string           100k                0.1188           0.0878
</span><span class='line'>in-hash-string           1000k               0.1174           0.0853
</span><span class='line'>in-skiplist-number       10k                 0.1095           0.0849
</span><span class='line'>in-skiplist-number       100k                0.1110           0.0873
</span><span class='line'>in-skiplist-number       1000k               0.1106           0.0910
</span><span class='line'>in-skiplist-string       10k                 0.1744           0.1315
</span><span class='line'>in-skiplist-string       100k                0.1990           0.1594
</span><span class='line'>in-skiplist-string       1000k               0.2369           0.1968</span></code></pre></td></tr></table></div></figure>


<p>It looks like joins and IN list lookups got significantly faster in 2.8, whereas
the performance for point lookups is more or less the same as in 2.7.</p>

<p>Note that the changes to the index code in 2.8 only affect how indexes are accessed
from within AQL queries and how filtering works. No changes have been made for other
index operations such as insert, updates, and removals.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Improved Deadlock Detection]]></title>
    <link href="http://jsteemann.github.io/blog/2015/11/18/improved-deadlock-detection/"/>
    <updated>2015-11-18T19:24:24+01:00</updated>
    <id>http://jsteemann.github.io/blog/2015/11/18/improved-deadlock-detection</id>
    <content type="html"><![CDATA[<p>The upcoming ArangoDB version 2.8 (currently in devel) will
provide a much better deadlock detection mechanism than its
predecessors.</p>

<p>The new deadlock detection mechanism will kick in automatically
when it detects operations that are mutually waiting for each other.
In case it finds such deadlock, it will abort one of the operations
so that the others can continue and overall progress can be made.</p>

<!-- more -->


<p>In previous versions of ArangoDB, deadlocks could make operations
wait forever, requiring the server to be stopped and restarted.</p>

<h2>How deadlocks can occur</h2>

<p>Here&rsquo;s a simple example for getting into a deadlock state:</p>

<p>Transaction A wants to write to collection c1 and to read from collection
c2. In parallel, transaction B wants to write to collection c2 and read
from collection c1. If the sequence of operations is interleaved as follows,
then the two transactions prevent each other from making progress:</p>

<ul>
<li>transaction A successfully acquires write-lock on c1</li>
<li>transaction B sucessfull acquires write-lock on c2</li>
<li>transaction A tries to acquire read-lock on c2 (and must wait for B)</li>
<li>transaction B tries to acquire read-lock on c1 (and must wait for A)</li>
</ul>


<p>Here&rsquo;s these such two transactions being started from two ArangoShell
instances in parallel (left is A, right is B):</p>

<p><img src="http://jsteemann.github.io/downloads/screenshots/deadlock.png"></p>

<p>(note that this screenshot is from 2.8 and the automatic deadlock detection
had already detected the deadlock and aborted one of the transactions)</p>

<p>In general, deadlocks can occur only when multiple operations (AQL
queries or other transactions) try to access the same resources
(collections) at the same time, and only if the operations already
have already acquired some locks on these resources. And finally
each operation needs to involve more than one collection, so there
is the potential for already having acquired some locks but having
to wait for others.</p>

<h2>Dynamically added collections</h2>

<p>Most operations will just work fine and will not cause any deadlocks.
This is especially true for all operations that involve only a single
collection. This leaves multi-collection AQL queries and multi-collection
userland transactions.</p>

<p>Normally these will also work fine. This is because when a query or
transaction starts, it will tell the transaction manager about the resources
(collections) it will need. The transaction manager can then acquire the
required resources in a deterministic fashion that prevents deadlocks.
If all queries and transactions properly announce upfront which collections
they will access, there will also be no deadlocks.</p>

<p>But for some operations its hard to predict at transaction start which
collections will be accessed. This includes some AQL functions that
can dynamically access collection data without having to specify the
collection name anywhere in the query.</p>

<p>A good example for this is the <code>GRAPH_EDGES</code> AQL function, which will get
a graph name as its first input parameter, but not the names of the underlying
edge collection(s). When this function is used in an AQL query, the
query parser will just find a function parameter containing a graph name
but doesn&rsquo;t know it&rsquo;s a collection name.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>GRAPH_EDGES("myEdges", [ { type: "friend" } ])</span></code></pre></td></tr></table></div></figure>


<p>The <code>"myEdges"</code> graph name will look like any other string to the parser.
It does not know about the contexts in which strings may have special meanings.</p>

<p>Note that even if this would be fixed, the problem won&rsquo;t go away entirely:
a function call parameter in a query isn&rsquo;t necessarily a constant but can
be an arbitrary expression:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>FOR doc IN collection
</span><span class='line'>  RETURN GRAPH_EDGES(CONCAT(doc.graphName, '-test'), [ doc.example ])</span></code></pre></td></tr></table></div></figure>


<p>At least in this case the AQL query parser won&rsquo;t find a collection name,
so when the AQL query starts it is yet unknown which collections will be
accessed. Only at runtime when the function is actually executed, the
collection names will be looked up by finding the graph description in the
<code>_graphs</code>system collection. Then the edge collections participating in
the graph will be added to the query dynamically. Only this dynamic addition
adds the potential for deadlock.</p>

<p>This dynamic addition of collections in unavoidable for conveniently
querying data from collections whose names are unknown when the query starts.</p>

<h2>Deadlock detection</h2>

<p>Whenever transaction manager detects a deadlock in ArangoDB 2.8, it will
automatically abort one of the blocking transactions. The transaction will
be rolled back and all modifications it has made will be reverted. The
operation will fail with error code 29 (<em>deadlock detected</em>) and raise an
exception that the user can handle in the calling code.</p>

<p>Deadlocks will be found if two transactions mutually lock each other as
seen in the screenshot above, but also for more complex setups. The following
screenshot shows four parallel transactions that block each other indirectly.</p>

<p><img src="http://jsteemann.github.io/downloads/screenshots/threeway-deadlock.png"></p>

<p>The top left window (transaction 1) will block the one in the top right
(transaction 2), and is itself blocked by the transaction in the bottom left
(transaction 3).</p>

<p>The transaction in the top right window (transaction 2) blocks the one in the
bottom left (transaction 3), and is itself blocked by the one in the top left
(transaction 1).</p>

<p>Transaction 3 (bottom left) is blocked by transaction 2 (top right).
Transaction 4 (bottom right) does exactly the same as transaction 3.</p>

<p>With these transactions, we end up in this waiting state:</p>

<ul>
<li>T1 waits for T3 and T4</li>
<li>T2 waits for T1</li>
<li>T3 waits for T2</li>
<li>T4 waits for T2</li>
</ul>


<p>This waiting state is cyclic (T1 &lt; T3 &lt; T2 &lt; T1) and therefore no progress
can be made. This is exactly a situation in which the transaction manager
will abort one of the transactions.</p>

<p>No configuration is required for the deadlock detection mechanism. It will
always be active and cannot be configured or turned off.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Bind Parameters in the AQL Editor]]></title>
    <link href="http://jsteemann.github.io/blog/2015/11/18/bind-parameters-in-aql-editor/"/>
    <updated>2015-11-18T19:24:24+01:00</updated>
    <id>http://jsteemann.github.io/blog/2015/11/18/bind-parameters-in-aql-editor</id>
    <content type="html"><![CDATA[<p>The AQL editor in the web interface is useful for running ad hoc AQL
queries and trying things out. It provides a feature to <em>explain</em> the
query and inspect its execution plan. This can be used to check if the
query uses indexes, and which.</p>

<p>So far the AQL editor only supported using query string literals, but <strong>it
lacked support for bind parameters</strong>. Queries issued by application code
however often <strong>will use bind parameters</strong> for security reasons. Often
enough this prevented copying &amp; pasting queries from the application code into
the AQL editor and vice versa without making manual adjustments.</p>

<!-- more -->


<p>This has been fixed in the upcoming ArangoDB version 2.8 (currently in
development). Bind parameters can now be used in the AQL editor as well.
Bind parameters can be entered as JSON values, the same format that is
used for bind parameters in the HTTP REST API and in (JavaScript) application
code.</p>

<p>The queries can also be saved in the AQL editor along with their bind parameter
values for later reuse.</p>

<p>Screenshot from the feature in 2.8:</p>

<p><img src="http://jsteemann.github.io/downloads/screenshots/bind-parameters.png"></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[C++ Constructors and Memory Leaks]]></title>
    <link href="http://jsteemann.github.io/blog/2015/11/18/on-exception-handling/"/>
    <updated>2015-11-18T01:10:51+01:00</updated>
    <id>http://jsteemann.github.io/blog/2015/11/18/on-exception-handling</id>
    <content type="html"><![CDATA[<h2>Preventing leaks in throwing constructors</h2>

<p>The easiest way to prevent memory leaks is to create all objects on
the stack and not using dynamic memory at all. However, often this is not
possible, for example because stack size is limited or objects need to
outlive the caller&rsquo;s scope.</p>

<p>Another way to prevent memory leaks and leaks of other resources is
obviously to employ the RAII pattern. How can it be used safely and
easily in practice, so memory leaks can be avoided?</p>

<p>This post will start with a few seemingly working but subtly ill-formed
techniques that a few common pitfalls. Later on it will provide a few
very simple solutions for getting it right.</p>

<!--more -->


<p>None of the solutions here are new or original.</p>

<p>I took some inspiration from the excellent <a href="http://gotw.ca/gotw/066.htm">constructor failures GotW post</a>.
That doesn&rsquo;t cover smart pointers and is not explicitly about preventing
preventing memory leak, so I put together this overview myself.</p>

<h2>Naive implementation</h2>

<p>Let&rsquo;s pretend we have a simple test program <code>main.cpp</code>, which
creates an object of class <em>MyClass</em> on the stack like this:</p>

<figure class='code'><figcaption><span>main.cpp</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="cp">#include &lt;iostream&gt;</span>
</span><span class='line'><span class="cp">#include &quot;MyClass.h&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="n">main</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">try</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">MyClass</span> <span class="n">myClass</span><span class="p">;</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;NO EXCEPTION&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CAUGHT EXCEPTION&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The above code creates the <em>myClass</em> instance on the stack, so
itself will not leak any memory. When the creating of the <em>myClass</em>
instance fails for whatever reason, the instance newer existed so
the memory for holding a <em>MyClass</em> object will be freed automatically.
If object creation succeeds and the object goes out of scope at the
end of the <em>try</em> block, then the object&rsquo;s destructor will be called
and resources can be freed, too.</p>

<p>Obviously this is already good, so let&rsquo;s keep it as it is and have a
look at the implementation of <em>MyClass</em> now. This class will manage
two heap objects of type <em>A</em>, which are created using the helper
function <em>createInstance</em>:</p>

<figure class='code'><figcaption><span>MyClass.h</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="cp">#include &lt;iostream&gt;</span>
</span><span class='line'><span class="cp">#include &quot;A.h&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">MyClass</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">A</span><span class="o">*</span> <span class="n">a1</span><span class="p">;</span>
</span><span class='line'>  <span class="n">A</span><span class="o">*</span> <span class="n">a2</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">MyClass</span> <span class="p">()</span>
</span><span class='line'>    <span class="o">:</span> <span class="n">a1</span><span class="p">(</span><span class="n">createInstance</span><span class="p">()),</span>
</span><span class='line'>      <span class="n">a2</span><span class="p">(</span><span class="n">createInstance</span><span class="p">())</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="o">~</span><span class="n">MyClass</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;DTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>    <span class="k">delete</span> <span class="n">a1</span><span class="p">;</span>
</span><span class='line'>    <span class="k">delete</span> <span class="n">a2</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>For completeness, here is class <em>A</em>. It won&rsquo;t manage any resources
itself:</p>

<figure class='code'><figcaption><span>A.h</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="cp">#include &lt;iostream&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">A</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">A</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR A&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="o">~</span><span class="n">A</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;DTOR A&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// helper method for creating an instance of A</span>
</span><span class='line'><span class="n">A</span><span class="o">*</span> <span class="n">createInstance</span> <span class="p">(</span><span class="kt">bool</span> <span class="n">shouldThrow</span> <span class="o">=</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">shouldThrow</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">throw</span> <span class="s">&quot;THROWING AN EXCEPTION&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">new</span> <span class="n">A</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>During this complete post, the code of <em>A.h</em> will remain unchanged.</p>

<p>Compiling and running the initial version of <code>main.cpp</code> will produce the
following output:</p>

<figure class='code'><figcaption><span>output of naive implementation</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>CTOR A
</span><span class='line'>CTOR A
</span><span class='line'>CTOR MYCLASS
</span><span class='line'>NO EXCEPTION
</span><span class='line'>DTOR MYCLASS
</span><span class='line'>DTOR A
</span><span class='line'>DTOR A</span></code></pre></td></tr></table></div></figure>


<p>Valgrind also reports no memory leaks. Are we done already?</p>

<h2>Introducing exceptions</h2>

<p>No, because everything still went well. Let&rsquo;s introduce exceptions into
the picture and check what happens then.</p>

<p>Let&rsquo;s first introduce an exception in the constructor of <em>MyClass</em>.
We&rsquo;ll make the <em>createInstance</em> function throw on second invocation (we do
this by passing a value of <em>true</em> to it):</p>

<figure class='code'><figcaption><span>constructor throwing an exception</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="n">MyClass</span> <span class="p">()</span>
</span><span class='line'>  <span class="o">:</span> <span class="n">a1</span><span class="p">(</span><span class="n">createInstance</span><span class="p">()),</span>
</span><span class='line'>    <span class="n">a2</span><span class="p">(</span><span class="n">createInstance</span><span class="p">(</span><span class="kc">true</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Running the program will now emit the following:</p>

<figure class='code'><figcaption><span>output of naive implementation, with exception</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>CTOR A
</span><span class='line'>CAUGHT EXCEPTION</span></code></pre></td></tr></table></div></figure>


<p>As we&rsquo;re throwing in the initializer list already, we don&rsquo;t even
reach the constructor body. This is no problem, but worse is that the
destructor for class <em>MyClass</em> is not being called at all.
Valgrind therefore reports the memory for first <em>A</em> instance as leaked.</p>

<p>By the way, the destructor for the <em>MyClass</em> instance is intentionally
not being called as the object hasn&rsquo;t been fully constructed and logically
never existed.</p>

<p>Will it help if we move the heap allocations from the initializer list
into the constructor body like this?</p>

<figure class='code'><figcaption><span>using the constructor body instead of the initializer list</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="n">MyClass</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="n">a1</span> <span class="o">=</span> <span class="n">createInstance</span><span class="p">();</span>
</span><span class='line'>  <span class="n">a2</span> <span class="o">=</span> <span class="n">createInstance</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Unfortunately not. Still no destructor invocations:</p>

<figure class='code'><figcaption><span>output of constructor body variant</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>CTOR MYCLASS
</span><span class='line'>CTOR A
</span><span class='line'>CAUGHT EXCEPTION</span></code></pre></td></tr></table></div></figure>


<p>Remember: an object&rsquo;s destructor won&rsquo;t be called if its constructor threw
and the exception wasn&rsquo;t caught. That also means releasing an object&rsquo;s
resources solely via the destructor as in implementation above will not be
sufficient if resources are allocated in the constructor and the constructor
can throw.</p>

<p>What can be done about that?</p>

<p>Obviously all resource allocations can be moved into the constructor body so
exceptions can be caught there:</p>

<figure class='code'><figcaption><span>catching exceptions in constructor of MyClass</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="n">MyClass</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="n">a1</span> <span class="o">=</span> <span class="n">createInstance</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">try</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">a2</span> <span class="o">=</span> <span class="n">createInstance</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// must clean up a1 to prevent a leak</span>
</span><span class='line'>    <span class="k">delete</span> <span class="n">a1</span><span class="p">;</span>
</span><span class='line'>    <span class="c1">// and re-throw the exception</span>
</span><span class='line'>    <span class="k">throw</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>While the above will work, it&rsquo;s clumsy, verbose and error-prone. If
more objects need to be managed this will make us end up in deeply
nested try&hellip;catch blocks.</p>

<h2>try&hellip;catch for the initializer list</h2>

<p>But wait, wasn&rsquo;t there a try&hellip;catch feature especially for initializer
list code? Sounds like it could be useful. Maybe we can use this instead
so we can catch exceptions during initialization?</p>

<p>There is indeed something like that: exceptions thrown from the initializer
list  can be caught using the following special syntax:</p>

<figure class='code'><figcaption><span>catching exceptions thrown in the initializer list</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="n">MyClass</span> <span class="p">()</span>
</span><span class='line'>  <span class="k">try</span> <span class="o">:</span> <span class="n">a1</span><span class="p">(</span><span class="n">createInstance</span><span class="p">()),</span>
</span><span class='line'>        <span class="n">a2</span><span class="p">(</span><span class="n">createInstance</span><span class="p">(</span><span class="kc">true</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>  <span class="c1">// catch block for initializer list code</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CATCH BLOCK MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>    <span class="k">delete</span> <span class="n">a1</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Running the program with the above <em>MyClass</em> constructor will also
do what is expected: when creating the second <em>A</em> instance, the
initializer list code will throw, invoking its catch block. Again
code execution won&rsquo;t make it into the constructor body, and we don&rsquo;t
see the destructor code in action.</p>

<p>The output of the program is:</p>

<figure class='code'><figcaption><span>output of initializer list variant </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>CTOR A
</span><span class='line'>CATCH BLOCK MYCLASS
</span><span class='line'>DTOR A
</span><span class='line'>CAUGHT EXCEPTION</span></code></pre></td></tr></table></div></figure>


<p>Valgrind does not report a leak, so are we done now?</p>

<p>No, as the above code has a severe problem. It worked only
because we knew the second invocation of <em>createInstance</em> would fail.</p>

<p>But in the general case, either the first call or the second call
can fail. If the first call fails, then the initializer hasn&rsquo;t
initialized any of the object&rsquo;s members, and it would be unsafe to
delete any object members in the initializer&rsquo;s catch block. If the
second <em>createInstance</em> call fails, then the initializer has created
<em>a1</em> but not <em>a2</em>. To prevent a leak in this case, we should delete <em>a1</em>,
but we better don&rsquo;t delete <em>a2</em> yet.</p>

<p>But how do we tell in the catch block at what stage the initializer
list had thrown? There is no natural way to do this correctly without
introducing more state. And without that, we have the choice between
undefined behavior when deleting the not-yet-initialized object
members, and memory leaks when ignoring them.</p>

<h2>Not using pointers at all</h2>

<p>Note that if we wouldn&rsquo;t have used pointers for our managed <em>A</em> objects,
then we could have used the fact that destructors for all initialized
object members <strong>are</strong> actually called when object construction fails.</p>

<p>However, simple pointers don&rsquo;t have a destructor, so the objects they
point to remain and the memory is lost.</p>

<p>So one obvious solution for preventing memory leaks is to not use pointers,
and get rid of all <code>new</code> and <code>delete</code> statements.</p>

<p>In some situations we can probably get away with making the managed objects
regular class members of the class that manages them:</p>

<figure class='code'><figcaption><span>not using pointers</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="k">struct</span> <span class="n">MyClass</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">A</span> <span class="n">a1</span><span class="p">;</span> <span class="c1">// no pointer anymore!</span>
</span><span class='line'>  <span class="n">A</span> <span class="n">a2</span><span class="p">;</span> <span class="c1">// no pointer anymore!</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">MyClass</span> <span class="p">()</span>
</span><span class='line'>    <span class="o">:</span> <span class="n">a1</span><span class="p">(),</span>
</span><span class='line'>      <span class="n">a2</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="o">~</span><span class="n">MyClass</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;DTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>    <span class="c1">// no delete statements needed anymore!</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now if any of the <em>A</em> constructors will throw an exception during
initialization, everything will be cleaned up properly. Now we can make
use of the destructor of <em>A</em>. If <em>A</em> instances are not pointers but
regular objects, the destructors for already created instances will
be called normally, and no destructors will be called for the not-yet-initialized
<em>A</em> instances. That&rsquo;s how it should be. We don&rsquo;t get this benefit with
regular pointers, which don&rsquo;t have a destructor.</p>

<p>As an aside, we got rid of the <code>delete</code> statements in the destructor
and may even get away with the default destructor.</p>

<p>Obviously this is an easy and safe solution, but it also has a few
downsides. Here are a few (incomplete list):</p>

<ul>
<li>when compiling <em>MyClass</em>, the compiler will now need to know the
definition for class <em>A</em>. You can&rsquo;t get away with a simple forward
declaration for class <em>A</em> anymore as in the case when the class
only contained pointers to <em>A</em>.
So this solution increases the source code dependencies and coupling.</li>
<li>instances of managed objects (e.g. <em>A</em>) will need to be created when
the managing object (e.g. <em>MyClass</em>) is created. There is no way to
postpone the object creation as in the case of when using pointers.</li>
<li>in general, the lifetime of the managed objects is tied to the lifetime
of the managing object. This may or may not be ok, depending on
requirements.</li>
</ul>


<h2>Using smart pointers (e.g. std::unique_ptr)</h2>

<p>In many cases the superior alternative to all the above is using one
of the available smart pointer classes for managing resources.</p>

<p>The promise of smart pointers is that resource management becomes easier,
safer and more flexible with them.</p>

<p>Really useful smart pointers (this excludes <code>std::auto_ptr</code>) are part
of standard C++ since C++11, and to my knowledge they can be used in
all C++11-compatible compilers and even in some older ones. Apart from
that, smart pointers are available in Boost for a long time already.</p>

<p>In the following snippets, I&rsquo;ll be using smart pointers of type
<code>std::unique_ptr</code> as it is the perfect fit for this particular problem.
I won&rsquo;t cover <code>shared_ptr</code>, <code>weak_ptr</code> or other types of smart pointers
here.</p>

<p>When using an <code>std::unique_ptr</code> for managing the resources of <em>MyClass</em>,
the <em>MyClass</em> code becomes:</p>

<figure class='code'><figcaption><span>using std::unique_ptr</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="cp">#include &lt;memory&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">MyClass</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span> <span class="n">a1</span><span class="p">;</span>
</span><span class='line'>  <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span> <span class="n">a2</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">MyClass</span> <span class="p">()</span> <span class="o">:</span>
</span><span class='line'>    <span class="n">a1</span><span class="p">(</span><span class="n">createInstance</span><span class="p">()),</span>
</span><span class='line'>    <span class="n">a2</span><span class="p">(</span><span class="n">createInstance</span><span class="p">(</span><span class="kc">true</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;CTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="o">~</span><span class="n">MyClass</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;DTOR MYCLASS&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>With a <code>unique_ptr</code>, we can still create resources when needed,
either in the initializer list, the constructor or even later. The
resources can still be created dynamically using <code>new</code> (as is still done
by function <em>createInstance</em>). When we&rsquo;re not taking the resources
away from the <code>unique_ptr</code>s, then they will free their managed
objects automatically and safely. We don&rsquo;t need to bother with <code>delete</code>.</p>

<p>And we don&rsquo;t need to bother with nested try&hellip;catch blocks either. If
anything goes wrong during object creation, any already assigned
<code>unique_ptr</code>s will happily release the resources they manage in their
own destructors.</p>

<p>It does not matter if the above code throws an exception in the first
invocation of <em>createInstance</em>, in the second or not at all: in every
case any allocated resources are released properly, and still there
is no need for any explicit exception handling or cleanup code. This is
what a smart pointer will do for us, behind the scenes.</p>

<p>Simply compare the following two code snippets, which both create three
instances of <em>A</em> while making sure no memory will be leaked if the
initialization goes wrong:</p>

<figure class='code'><figcaption><span>solution using smart pointers</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span> <span class="n">a1</span><span class="p">(</span><span class="n">createInstance</span><span class="p">());</span>
</span><span class='line'><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span> <span class="n">a2</span><span class="p">(</span><span class="n">createInstance</span><span class="p">());</span>
</span><span class='line'><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span> <span class="n">a3</span><span class="p">(</span><span class="n">createInstance</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// now do something with a1, a2, a3</span>
</span><span class='line'><span class="c1">// managed objects will be released automatically when</span>
</span><span class='line'><span class="c1">// the unique_ptrs go out of scope</span>
</span><span class='line'><span class="c1">// note: they may go out of scope unintentionally if</span>
</span><span class='line'><span class="c1">// some code below will throw an exception...</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>solution using nested try&#8230;catch blocks</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="n">A</span><span class="o">*</span> <span class="n">a1</span> <span class="o">=</span> <span class="n">nullptr</span><span class="p">;</span>
</span><span class='line'><span class="n">A</span><span class="o">*</span> <span class="n">a2</span> <span class="o">=</span> <span class="n">nullptr</span><span class="p">;</span>
</span><span class='line'><span class="n">A</span><span class="o">*</span> <span class="n">a3</span> <span class="o">=</span> <span class="n">nullptr</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="n">a1</span> <span class="o">=</span> <span class="k">new</span> <span class="n">A</span><span class="p">;</span>
</span><span class='line'><span class="k">try</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">a2</span> <span class="o">=</span> <span class="k">new</span> <span class="n">A</span><span class="p">;</span>
</span><span class='line'>  <span class="k">try</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">a3</span> <span class="o">=</span> <span class="k">new</span> <span class="n">A</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">delete</span> <span class="n">a2</span><span class="p">;</span>
</span><span class='line'>    <span class="k">throw</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">delete</span> <span class="n">a1</span><span class="p">;</span>
</span><span class='line'>  <span class="k">throw</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// now do something with a1, a2, a3</span>
</span><span class='line'><span class="c1">// objects a1, a2, a3 will not be released automatically</span>
</span><span class='line'><span class="c1">// when a1, a2, a3 go out of scope. any user of a1, a2, a3</span>
</span><span class='line'><span class="c1">// below must make sure to release the objects when they</span>
</span><span class='line'><span class="c1">// go out of scope or when an exception is thrown...</span>
</span></code></pre></td></tr></table></div></figure>


<p>Obviously the smart pointer-based solution is less verbose,
but it is also safer and hard to get wrong. It is especially
useful for initializing and managing dynamically allocated
object members, because as we&rsquo;ve seen most of the other
ways to do this are either subtly broken or much more complex.</p>

<p>Apart from that, we can take the managed object from out of a
<code>unique_ptr</code> and take over responsibility for managing its
lifetime.</p>

<p>Further on the plus side, a class definition that contains
<code>unique_ptr</code>s can be compiled with only forward declarations
for the managed types. However, when the <code>unique_ptr</code> is a
regular object member, at least the class destructor
implementation will need to know the size of the managed type
so it can call <code>delete</code> properly.</p>

<p>The downside of using smart pointers is that they may impose
minimal overhead when compared to the pure pointer-based
solution. However in most cases this overhead should be
absolutely negligible or even be optimized away by the compiler.
It may make a difference though when compiling without any
optimizations, but this shouldn&rsquo;t matter too much in reality.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ArangoDB-PHP Driver Improvements]]></title>
    <link href="http://jsteemann.github.io/blog/2015/09/10/arangodb-php-driver-improvements/"/>
    <updated>2015-09-10T14:05:40+02:00</updated>
    <id>http://jsteemann.github.io/blog/2015/09/10/arangodb-php-driver-improvements</id>
    <content type="html"><![CDATA[<p>While preparing the release of ArangoDB 2.7, some improvements were made for the
<a href="https://github.com/arangodb/arangodb-php">PHP driver for ArangoDB</a>.</p>

<p>The 2.7 version of the PHP driver now supports the AQL query results cache. The
cache can be turned on or off globally, or be set to demand mode. The demand mode will
allow controlling caching on a per-AQL-query basis.</p>

<p>Additionally, the HTTP transport layer in the PHP driver was improved. Some internal
string handling methods were optimized so that the transport part becomes cheaper. All
driver operations that communicate with the ArangoDB server will benefit from this.</p>

<p>For a demonstration of the improvements, here is a script that creates 100,000
documents in a local ArangoDB database via the PHP driver. As we&rsquo;re interested in assessing
the HTTP layer improvements, the script intentionally issues 100,000 HTTP requests
instead of using the specialized <code>import</code> method provided by the driver.</p>

<p>The script code can be found <a href="https://github.com/arangodb/arangodb-php/blob/devel/examples/http-test.php">here</a>.</p>

<p>The baseline for the improvments is the (non-optimized) 2.6 version of the PHP
driver. Here are the results for issuing 100,000 requests with the 2.6 driver
(script was run twice to see if there are variations in execution time):</p>

<figure class='code'><figcaption><span>execution times with 2.6 driver</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 55.144556999207 s
</span><span class='line'>
</span><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 54.476955890656 s</span></code></pre></td></tr></table></div></figure>


<p>Running it with the 2.7 version of the PHP driver now shows the improvements.
Execution time for the same script goes down from 54 seconds to 42 seconds:</p>

<figure class='code'><figcaption><span>execution times with 2.7 driver</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 42.886090040207 s
</span><span class='line'>
</span><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 42.578990936279 s</span></code></pre></td></tr></table></div></figure>


<p>The PHP version used here was:</p>

<figure class='code'><figcaption><span>PHP version details</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>PHP 5.5.12-2ubuntu4.6 (cli) (built: Jul  2 2015 15:27:14) 
</span><span class='line'>Copyright (c) 1997-2014 The PHP Group
</span><span class='line'>Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
</span><span class='line'>    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies</span></code></pre></td></tr></table></div></figure>


<p>Following are the results from a different machine, this time using PHP 5.6:</p>

<figure class='code'><figcaption><span>execution times with 2.6 driver</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 48.394731044769 s
</span><span class='line'>
</span><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 47.618598937988 s</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>execution times with 2.7 driver</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 40.535583972931 s
</span><span class='line'>
</span><span class='line'>creating 100000 documents
</span><span class='line'>creating documents took 40.041265010834 s</span></code></pre></td></tr></table></div></figure>


<p>The PHP version details for this machine were:</p>

<figure class='code'><figcaption><span>PHP version details</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>PHP 5.6.4-4ubuntu6.2 (cli) (built: Jul  2 2015 15:29:28) 
</span><span class='line'>Copyright (c) 1997-2014 The PHP Group
</span><span class='line'>Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
</span><span class='line'>    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies</span></code></pre></td></tr></table></div></figure>


<p>The actual improvements depend on many factors, so your exact mileage may vary.
The improvements may not be noticable for applications that issue only a few
requests with the driver, but they will be significant when performing lots of
requests, as in the above examples.</p>
]]></content>
  </entry>
  
</feed>
