<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2014:07:12</title>
        <description></description>
        <link>https://baszerr.eu/</link>
        <lastBuildDate>Fri, 01 May 2026 09:47:24 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://baszerr.eu/</link>
        </image>
        <item>
            <title>faster_by_encapsulation</title>
            <link>https://baszerr.eu/doku.php?id=blog:2014:07:12:faster_by_encapsulation</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;faster_by_encapsulation&quot;&gt;2014-07-12 - faster by encapsulation&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
encapsulation usually means for ppl abstraction and abstraction in turn is often blamed for decreasing performance. this is not &lt;a href=&quot;https://en.wikipedia.org/wiki/C++&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++&quot;&gt;C++&lt;/a&gt; style. C++ is all about lightweight abstractions. the same way one can have abstract algorithms like &lt;em&gt;sort&lt;/em&gt;, that are highly optimized and fast, the same way encapsulation can be used for speeding things up.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;2014-07-12 - faster by encapsulation&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;faster_by_encapsulation&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:1,&amp;quot;range&amp;quot;:&amp;quot;1-403&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit2&quot; id=&quot;input_example&quot;&gt;input example&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
as an example consider raw pointer to some type &lt;em&gt;X&lt;/em&gt; and two functions operating on it:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; foo&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;X&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; ptr&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;not ptr&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    throw std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;runtime_error&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;unexpected null&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; ptr&lt;span class=&quot;sy0&quot;&gt;-&amp;gt;&lt;/span&gt;getNumber&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;double&lt;/span&gt; bar&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;X&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; ptr&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;not ptr&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    throw std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;runtime_error&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;unexpected null&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; ptr&lt;span class=&quot;sy0&quot;&gt;-&amp;gt;&lt;/span&gt;averageValue&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nu16&quot;&gt;4.2&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
what do they have in common? they both do simple things, obfuscated by constant not-null checking.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;input example&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;input_example&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:2,&amp;quot;range&amp;quot;:&amp;quot;404-879&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit3&quot; id=&quot;encapsulation&quot;&gt;encapsulation&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
now imagine class, that would ensure pointer held inside is not null. something as simple as this:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;typename T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; NotNull
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  explicit NotNull&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; t&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;:&lt;/span&gt;
    t_&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;t&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;not t_&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
      throw std&lt;span class=&quot;sy0&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;runtime_error&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;pointer is null&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;co1&quot;&gt;// more operators, c-tors, etc...&lt;/span&gt;
&amp;nbsp;
  T&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; operator&lt;span class=&quot;sy0&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; t_&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;co1&quot;&gt;// more getters, swaps, etc...&lt;/span&gt;
private&lt;span class=&quot;sy0&quot;&gt;:&lt;/span&gt;
T&lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; t_&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
now lets use it in our previous code:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; foo&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;NotNull&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;X&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; ptr&lt;span class=&quot;sy0&quot;&gt;-&amp;gt;&lt;/span&gt;getNumber&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;double&lt;/span&gt; bar&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;NotNull&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;X&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; ptr&lt;span class=&quot;sy0&quot;&gt;-&amp;gt;&lt;/span&gt;averageValue&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nu16&quot;&gt;4.2&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
code just got shorter and more readable. it also auto-protects from mistakes – null-pointers dereferences are no longer a problem.
&lt;/p&gt;

&lt;p&gt;
but how it got faster? notice that now you need to do not-null check only once – in a constructor. from that point, merely knowing type ensures you the pointer cannot be null, so you no longer need to check it! it means not only less code, but a faster code – not chance of branch miss-prediction. :) this may not appear to be lot, but when you multiply this by number of method/functions in your project, gain will became obvious. plus you get shorter code, for free. :D
&lt;/p&gt;

&lt;p&gt;
presented idea is a &lt;a href=&quot;https://en.wikipedia.org/wiki/RAII&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/RAII&quot;&gt;RAII&lt;/a&gt; way of expressing properties, as an extension of usual “ownerships”. logically it works exactly the same way RAII constructors do: once constructor is done, each method needs not to check if object is initialized. this way property can be encapsulated in a type, to free user from checking certain invariants at each step.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;encapsulation&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;encapsulation&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:1,&amp;quot;secid&amp;quot;:3,&amp;quot;range&amp;quot;:&amp;quot;880-2448&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit4&quot; id=&quot;genesis&quot;&gt;genesis&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
the idea is not new to me. it first crossed my mind when we were developing &lt;a href=&quot;https://en.wikipedia.org/wiki/ACARM-ng&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/ACARM-ng&quot;&gt;ACARM-ng&lt;/a&gt;. as we had a chance to make the whole system from scratch, many interesting ideas popped out at that time. on of this was to make boost::shared_ptr version, that cannot be null. since system is open source, you can &lt;a href=&quot;https://github.com/el-bart/ACARM-ng/blob/master/src/commons/Commons/SharedPtrNotNULL.hpp&quot; class=&quot;urlextern&quot; title=&quot;https://github.com/el-bart/ACARM-ng/blob/master/src/commons/Commons/SharedPtrNotNULL.hpp&quot; rel=&quot;ugc nofollow&quot;&gt;check out the original implementation&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
it all worked like a charm! simple, valid code without extra checks, where not needed. exactly what one needs! :)
&lt;/p&gt;

&lt;p&gt;
this was back in &lt;a href=&quot;https://en.wikipedia.org/wiki/C++03&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++03&quot;&gt;C++03&lt;/a&gt; era. now we can do more. i plan to implement general pointer wrapper (NotNull&amp;lt;&amp;gt;) as a part of &lt;a href=&quot;https://github.com/el-bart/but&quot; class=&quot;urlextern&quot; title=&quot;https://github.com/el-bart/but&quot; rel=&quot;ugc nofollow&quot;&gt;Basic Utilities and Tools&lt;/a&gt; library.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;genesis&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;genesis&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:3,&amp;quot;secid&amp;quot;:4,&amp;quot;range&amp;quot;:&amp;quot;2449-&amp;quot;} --&gt;</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:08:53 +0000</pubDate>
        </item>
    </channel>
</rss>
