<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michał Górny</title>
	<atom:link href="http://blogs.gentoo.org/mgorny/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.gentoo.org/mgorny</link>
	<description>Retroactively fixing the world</description>
	<lastBuildDate>Tue, 27 Nov 2012 05:24:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>A few words on the topic of optimization</title>
		<link>http://blogs.gentoo.org/mgorny/2012/08/23/a-few-words-on-the-topic-of-optimization/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/08/23/a-few-words-on-the-topic-of-optimization/#comments</comments>
		<pubDate>Thu, 23 Aug 2012 08:27:22 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Programmer's digest]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=176</guid>
		<description><![CDATA[Optimization is a very broad topic when referring to compiled languages like C or C++. There are many good guides on that topic. However, I see that people nevertheless forget about a few basic principles; thus I&#8217;d like to write a few short words myself, explaining how to avoid common pitfalls when optimizing or at [...]]]></description>
				<content:encoded><![CDATA[<p>Optimization is a very broad topic when referring to compiled languages like C or C++. There are many good guides on that topic. However, I see that people nevertheless forget about a few basic principles; thus I&#8217;d like to write a few short words myself, explaining how to avoid common pitfalls when optimizing or at least trying to.</p>
<p>First of all, I&#8217;d like to notice that I will be considering <cite>optimization</cite> as targeted towards making the program execution time shorter through use of faster code (algorithms, methods). I will not cover the area in detail but just give a few tips or remainders which should be taken into account when optimizing.</p>
<h2>Know your assembly!</h2>
<p>Measurement is a very important tool when it comes to optimize. Measure before, measure after and see whether there was improvement. Developers like measures because they give numbers, and numbers simply say <q>faster</q> or <q>slower</q>, or sometimes <q>not sure but whatever</q>. But remember that numbers can be mis-interpreted easily, and then they can lead to wrong (or even ridiculous) conclusions.</p>
<p>To be honest, measurement is a very broad topic itself, and it is really hard to perform good and trustworthy measures. And I believe that in order to do so, you first need to know deeply what you are measuring. And in fact you are measuring code; but more than the code you have just written, you are measuring the assembly generated by the compiler.</p>
<p>Thus, I believe that before starting to measure anything, you should obtain the resulting assembly and analyze it. Or at least compare with the previous result. Otherwise, you can end up trying to measure a difference between two variants of code resulting in <em>the same</em> assembly! And trying to improve your measurement methods to get a consistent, meaningful difference.</p>
<p>Of course, you shouldn&#8217;t trust the assembly by itself either. Sometimes seemingly insignificant code changes can cause noticeable differences in execution time. Sometimes four instructions (with longer execution time) actually execute faster than two instructions because the former were done in parallel while the latter couldn&#8217;t; and this is when measurement can come in handy.</p>
<h2>Optimize the compiler, do not perform its job</h2>
<p>There are people who believe that the only possible way of getting good code is by writing the assembly themselves. Shortly saying — <em>don&#8217;t do it!</em> Most importantly, you are making your program less portable; if the assembly is optional, the replacement part becomes not well-tested. And anyway, you are writing the assembly for your CPU. If you are writing for Intel, then AMD can work better with another code; if you are writing for AMD, then VIA may like yet another solution. And I&#8217;m just considering x86 here.</p>
<p>If you are writing in high-level programming language like C or C++, write in it. If the compiler doesn&#8217;t generate desired assembly, try to change the code to give it a hint. Sometimes you just need to reorder the variables a little, or introduce a helper (mid-result) variable.</p>
<p>Often it is actually <em>beneficial</em> to replace your complex, <q>optimized</q> construct with a simpler one. The latter will benefice both the users (which will find it easier to understand it, and fiddle with it) and the compiler (which will find it easier to understand it, and replace it with a much better optimized code).</p>
<p>And if you still can&#8217;t get your desired assembly, and you are sure that it will be actually better and beneficial, report a bug. Get your compiler fixed so that it could do its job better, and benefit all the projects using it, not only yours.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/08/23/a-few-words-on-the-topic-of-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The impact of C++ templates on library ABI</title>
		<link>http://blogs.gentoo.org/mgorny/2012/08/20/the-impact-of-cxx-templates-on-library-abi/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/08/20/the-impact-of-cxx-templates-on-library-abi/#comments</comments>
		<pubDate>Mon, 20 Aug 2012 10:38:03 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Programmer's digest]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=163</guid>
		<description><![CDATA[Author: Michał Górny Date: 20 Aug 2012 Copyright: http://creativecommons.org/licenses/by/3.0/ Source: http://dev.gentoo.org/~mgorny/articles/the-impact-of-cxx-templates-on-library-abi.rst Preamble The general aspect of maintaining binary compatibility of C++ library interfaces has been already covered thoroughly multiple times. A good reference of articles on the topic can be found on wiki page of ABI compliance checker tool [1]. Sadly, those articles usually consider [...]]]></description>
				<content:encoded><![CDATA[<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr>
<th class="docinfo-name">Author:</th>
<td>Michał Górny</td>
</tr>
<tr>
<th class="docinfo-name">Date:</th>
<td>20 Aug 2012</td>
</tr>
<tr>
<th class="docinfo-name">Copyright:</th>
<td><a class="first last reference external" href="http://creativecommons.org/licenses/by/3.0/">http://creativecommons.org/licenses/by/3.0/</a></td>
</tr>
<tr>
<th class="docinfo-name">Source:</th>
<td><a class="first last reference external" href="http://dev.gentoo.org/~mgorny/articles/the-impact-of-cxx-templates-on-library-abi.rst">http://dev.gentoo.org/~mgorny/articles/the-impact-of-cxx-templates-on-library-abi.rst</a></td>
</tr>
</tbody>
</table>
<div class="section" id="preamble">
<h2>Preamble</h2>
<p>The general aspect of maintaining binary compatibility of C++ library interfaces has been already covered thoroughly multiple times.  A good reference of articles on the topic can be found on wiki page of <cite>ABI compliance checker</cite> tool <a class="footnote-reference" href="#id2" id="id1">[1]</a>. Sadly, those articles usually consider the topic of C++ templates only briefly, if at all.</p>
<p>While in fact the topic is fairly complex, and I believe that considering the overall usefulness and popularity of the templates, it should be considered more thoroughly. Thus, in this article I will try to address the issues arising from use of templates, methods of dealing with them and trying to prevent them.</p>
<p>Both the overall topic of templates in respect to the programming techniques, and the wide topic of ABI are already explained in detail in many other articles and guides. Moreover, I believe that myself I am not fluent enough to be able to cover those topics in detail here. Thus, I will assume that a reader of this article is already familiar with both the general topic of templates in C++, and the basic aspects of an ABI and its compatibility.</p>
<p>Moreover, in the solutions and problems listed here I will assume that a particular toolchain in question does conform to the C++98 standard, and is able to properly support templates with regard to multiple instantiations.</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id1">[1]</a></td>
<td><a class="reference external" href="http://ispras.linuxbase.org/index.php/ABI_compliance_checker#Articles">http://ispras.linuxbase.org/index.php/ABI_compliance_checker#Articles</a></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="abi-consistency-in-regular-functions-and-class-methods">
<h2>ABI consistency in regular functions and class methods</h2>
<p>Before proceeding further, I wish to quickly summarize the aspects of ABI compliance in regard to regular (<cite>non-template</cite> and <cite>non-inline</cite>) functions and class methods.</p>
<p>In shared libraries, the interface of library functions is defined within the header files which are installed along with the library.  The implementation code, however, is defined in the source files which are compiled into the shared library itself. Effectively, the implementation is kept opaque to the library consumers; and those consumers (either programs or other shared libraries) dynamically link to the library <a class="footnote-reference" href="#id4" id="id3">[2]</a>.</p>
<p>Nevertheless, if the function ABI is to change, such a change can be signaled through changing the <cite>SONAME</cite> of the library. As executables and other shared libraries are linked to a specific <cite>SONAME</cite>, they have to be rebuilt in order to use the new version of the library. That way, it is ensured that the consumers will be rebuilt to use the new ABI before using the library.</p>
<p>Effectively, it is quite straightforward to maintain the ABI of a shared library using regular functions and classes. The number of ABI changes is minimized through keeping the implementation opaque to the consumers; and the actual ABI changes are propagated through use of <cite>SONAME</cite> and other symbol versioning techniques.</p>
<table class="docutils footnote" frame="void" id="id4" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id3">[2]</a></td>
<td>The topic of static linking is not covered here as of no relevance.</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="the-problem-of-handling-template-functions-and-classes">
<h2>The problem of handling template functions and classes</h2>
<p>The requirements put on the compiler for template support made the solution presented above no longer possible. In order to achieve generic programming, the compiler must be able to adjust both the interface and the implementation of template functions and classes according to the template arguments. Considering that those arguments can practically hold any consumer-provided type or value (including types defined by consumers themselves), it is no longer possible to precompile the implementation code and provide it in a opaque shared library.</p>
<p>A completely different solutions need to be used in order to support templates. Firstly, both the interface and the implementation code must be public to the consumers; thus both are provided in the headers installed by the library. Effectively, a number of C++ libraries, Eigen <a class="footnote-reference" href="#id6" id="id5">[3]</a> for instance, actually consist of header files only.</p>
<p>Secondly, compiler must be able to instantiate and compile the templates at will. Unlike with regular function which are compiled within a single source unit, templates have to be compiled on use, and can be used in multiple independent files. Effectively, the compiler must be able to handle multiple definitions of the same template sanely.</p>
<p>To sum up, templates basically no longer form a uniform shared library.  They are propagated through header files, and can be instantiated practically anywhere — either in the library providing them (through use in other functions, or through forced instantiation), in a shared library using them or in the final executable. Moreover, the same template can be instantiated in multiple locations at the same time.  The implementation part can no longer be opaque, and is spread along with the instantiations.</p>
<p>The lack of ability to provide opaque implementation makes it harder to change the code without breaking ABI compatibility. Moreover, the decentralization makes it even harder both to propagate implementation changes (including bug fixes) and to handle ABI changes.</p>
<table class="docutils footnote" frame="void" id="id6" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id5">[3]</a></td>
<td><a class="reference external" href="http://eigen.tuxfamily.org/">http://eigen.tuxfamily.org/</a></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="avoiding-multiple-incompatible-instantiations">
<h2>Avoiding multiple, incompatible instantiations</h2>
<p>One of the most common issues being result of a library ABI change is the occurrence of multiple, incompatible instantiations of the same template. I will explain this on a recent example.</p>
<p>With the recent improvement of C++11 standard support in gcc 4.7.1, the <tt class="docutils literal"><span class="pre">std::list&lt;T&gt;</span></tt> template class has been changed to hold the list length. The change has been made conditional to the C++11 standard being enabled; effectively, to the <tt class="docutils literal"><span class="pre">-std=c++11</span></tt> option <a class="footnote-reference" href="#id8" id="id7">[4]</a>.</p>
<p>Now, if I have a particular program which uses <tt class="docutils literal"><span class="pre">std::list&lt;int&gt;</span></tt>, and I compile that program in the C++11 mode, the extended template will be used. To that moment, everything is fine.</p>
<p>However, if I would like to use a shared library using <tt class="docutils literal"><span class="pre">std::list&lt;int&gt;</span></tt> as well, and that library is compiled for another C++ standard (<tt class="docutils literal"><span class="pre">-std=c++98</span></tt>, for example), the program may no longer work correctly.</p>
<p>This is because both the program executable and the shared library would try to instantiate an incompatible versions of <tt class="docutils literal"><span class="pre">std::list&lt;int&gt;</span></tt>. Only one of those versions will be actually used, and the code written for the other one may break randomly.</p>
<p>The main issue solving that incompatibility is that if the library providing the template is based only on header files, it is no longer able to enforce propagation of the ABI change through rebuilding all of the libraries and executables using it.</p>
<table class="docutils footnote" frame="void" id="id8" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id7">[4]</a></td>
<td>As a side note, the change has been reverted and postponed to be introduced unconditionally in the next standard C++ library version.</td>
</tr>
</tbody>
</table>
<div class="section" id="using-templates-in-final-executables-only">
<h3>Using templates in final executables only</h3>
<p>A one particular way in which the problem could be avoided is to limit the template instantiation to a single executable. For practical reasons, that executable would supposedly be the final program executable, allowing the programmer to use templates freely and all the libraries to use them equally.</p>
<p>This can be achieved through making all the implementation using templates… a template as well. For example, consider the following code:</p>
<pre lang="c" class="literal-block"><code>#include &lt;vector&gt;

class C
{
public:
        int f();

        // ...
};

int C::f()
{
        std::vector&lt;int&gt; value_vector;

        // ...
}
</code></pre>
<p>Here function <cite>f()</cite> from class <cite>C</cite> uses a <tt class="docutils literal"><span class="pre">std::vector&lt;int&gt;</span></tt> internally. In order to avoid local instantiation of the vector, we need to make the class <cite>C</cite> a template class. In this particular case, we can achieve an additional benefit from that — allowing the user to specify the allocator for <cite>value_vector</cite>:</p>
<pre lang="c" class="literal-block"><code>#include &lt;memory&gt;
#include &lt;vector&gt;

template &lt; class allocator = std::allocator&lt;void&gt; &gt;
class C
{
public:
        int f()
        {
                typedef typename allocator::template rebind&lt;int&gt;
                        ::other alloc_type;
                std::vector&lt;int, alloc_type&gt; value_vector;

                // ...
        }
};
</code></pre>
<p>This way, as long as all classes using <cite>C</cite> are templates as well, the only template instantiation will take place in the final executable where the last template class is used. Of course, this has many implications for quite a little benefit. Most importantly:</p>
<ul class="simple">
<li>all template classes have to be used along with template parameters; instead of <tt class="docutils literal">C</tt>, one has to use <tt class="docutils literal">C&lt;&gt;</tt>;</li>
<li>all template classes have to make the implementation public, and start to suffer from all template-related issues explained here;</li>
<li>the code generation is deferred to the final executable. Code duplication occurs, shared libraries no longer serve a purpose and compiling the program becomes time- and memory-consuming.</li>
</ul>
<p>Just as a note, the fore-mentioned approach can be used when there is no logical use for a template as well. An unused template parameter can be introduced then:</p>
<pre lang="c" class="literal-block"><code>template &lt;int unused = 0&gt;
class C
{
        // ...
};
</code></pre>
</div>
<div class="section" id="using-custom-types-in-shared-library-templates">
<h3>Using custom types in shared library templates</h3>
<p>An alternate and possibly better approach is to prevent non-local instantiations of a particular template specializations through scoping of the types used in them.</p>
<p>Please consider the following example:</p>
<pre lang="c" class="literal-block"><code>#include &lt;list&gt;

// class C is a public class provided by the library

void f()
{
        std::list&lt;C&gt; l;

        // ...
}
</code></pre>
<p>Here, the <tt class="docutils literal"><span class="pre">std::list&lt;C&gt;</span></tt> template is instantiated. However, the type <cite>C</cite> is part of the public library API and an API consumer may want to create a list of that type as well. In order to avoid the list being instantiated twice, the library may replace <cite>C</cite> with a local-scope derived class:</p>
<pre lang="c" class="literal-block"><code>#include &lt;list&gt;

namespace
{
        class C_ : public C
        {
                // ...
        };
};

void f()
{
        std::list&lt;C_&gt; l;

        // ...
}
</code></pre>
<p>Since <cite>C</cite> and <cite>C_</cite> are distinct types, <tt class="docutils literal"><span class="pre">std::list&lt;C_&gt;</span></tt> will instantiate a different specialization than <tt class="docutils literal"><span class="pre">std::list&lt;C&gt;</span></tt>.  And as <cite>C_</cite> is limited to the local scope, no consumer should be able to<br />
create a list of that type.</p>
</div>
<div class="section" id="using-inline-methods-and-functions">
<h3>Using inline methods and functions</h3>
<p>There is one more solution which I am ought to mention, yet its use is limited and it is not guaranteed to work at all. The one remaining way of preventing multiple instantiations is to enforce inline instantiations of template methods.</p>
<p>This approach has a few limitations. Firstly, it requires modifying the template itself, so it won&#8217;t work with external templates, like fore-mentioned <tt class="docutils literal"><span class="pre">std::list</span></tt>. Additionally, since the compiler is free to ignore the <cite>inline</cite> keyword at will, this solution may only work partially or not work at all.</p>
<p>For completeness, I will present an example:</p>
<pre lang="c" class="literal-block"><code>template &lt;class T&gt;
class C
{
        T sth;

public:
        inline T&amp; get()
        {
                return sth;
        }

        inline void set(T&amp; new_val)
        {
                sth = new_val;
        }
};
</code></pre>
<p>The above code uses the <cite>inline</cite> keyword extensively, hoping that the compiler will inline all the method calls rather than instantiating them as separate methods. If it succeeds for all of the methods, external instantiations will not have anything to collide with.</p>
</div>
</div>
<div class="section" id="achieving-a-partially-opaque-implementation-with-templates">
<h2>Achieving a (partially) opaque implementation with templates</h2>
<p>While the solutions listed above address a real issue, none of them could be considered a really good solution. They mostly try to circumvent a specific issue rather than addressing the deeper one which I would like to point out.</p>
<p>Those solutions are good as long as we&#8217;re only interested in keeping the executables <cite>mostly</cite> working. However, the template library works alike a <cite>static library</cite> there, suffering from the same limitations that a static library has.</p>
<p>Most importantly, it is impossible to change the implementation of a particular function or method, and propagate the change without rebuilding all the libraries and programs using the template. And this becomes important if the particular code suffers from security vulnerabilities or bugs which could result in data loss.</p>
<p>In order to solve that problem, we need to take a step back and see what can we do to make template implementation more opaque once again.</p>
<div class="section" id="explicitly-instantiating-opaque-templates">
<h3>Explicitly instantiating opaque templates</h3>
<p>One particular solution which solves the issue completely is to keep the implementation completely opaque, and force the compiler to explicitly instantiate necessary templates. However, a very important disadvantage of this solution is that the possible uses of the template are limited to the ones provided by the library.</p>
<p>For an example, please consider we&#8217;re having a <tt class="docutils literal">MyList&lt;T&gt;</tt> container which works similarly to <tt class="docutils literal"><span class="pre">std::list&lt;T&gt;</span></tt>. In order to make the list implementation opaque, the header files contain only the interface of <tt class="docutils literal">MyList&lt;T&gt;</tt>:</p>
<pre lang="c" class="literal-block"><code>template &lt;class T&gt;
class MyList
{
        struct priv_type;
        priv_type* priv;

public:
        // ...
};
</code></pre>
<p>As you can see in the above example, the PImpl (<cite>private implementation</cite>) idiom is used there to hide the private class members.  That kind of PImpl implementation does not make sense with regular templates, since the private members would have to be explicitly listed along with the implementation. However, in this case we are keeping the implementation private.</p>
<p>The implementation along with the supported instantiations is provided in the source file of the library:</p>
<pre lang="c" class="literal-block"><code>#include &quot;mylist&quot;

template class MyList&lt;int&gt;;
template class MyList&lt;double&gt;;
template class MyList&lt;void*&gt;;

// ...
</code></pre>
<p>With the above declarations, followed by the method and <cite>struct priv_type</cite> implementations will result in compiler outputting the template code for <cite>int</cite>, <cite>double</cite> and <cite>void*</cite> types. That code will be placed in the shared library, and made available for the consumers of the shared library.</p>
<p>Now, if a consumer uses <tt class="docutils literal">MyList&lt;int&gt;</tt> and links to the shared library, it will use the opaque implementation from the library. Alike with regular functions, any ABI-safe changes will be propagated to the programs; and ABI-unsafe will result in necessity of rebuilding them, propagated through <cite>SONAME</cite> change.</p>
<p>However, if the consumer decides to use <tt class="docutils literal">MyList&lt;char&gt;</tt>, the compiler will no longer be able to satisfy that requirement. The linker will refuse to link the program because of no implementation of <tt class="docutils literal">MyList&lt;char&gt;</tt> methods.</p>
</div>
<div class="section" id="using-common-base-classes-and-functions">
<h3>Using common base classes and functions</h3>
<p>Sadly, the fore-mentioned approach limits the possible uses of the template to a predefined parameter sets. This may be acceptable in very specific cases but usually it just invalidates the whole point of using templates.</p>
<p>An alternative approach is to use a partially opaque implementation, with the opaque being subject to <cite>easy</cite> implementation changes, and the public behaving like a regular template. This could be accomplished in a number of ways.</p>
<p>One of them is to develop a common, opaque base class which performs the most important tasks in a type-agnostic way and a derived template class which wraps the former in a nice API. For example, consider a pointer list like the following:</p>
<pre lang="c" class="literal-block"><code>template &lt;class T&gt;
class PointerList;

template &lt;&gt;
class PointerList&lt;void&gt;
{
        struct priv_data;
        priv_data* priv;

public:
        // ...
};

template &lt;class T&gt;
class PointerList
        : private PointerList&lt;void&gt;
{
public:
        // ...
};
</code></pre>
<p>The pointer list template is first specialized for the <tt class="docutils literal">void*</tt> type.  That specialization uses a PImpl idiom, private code and explicit instantiation (in the source file) to provide an opaque implementation of <tt class="docutils literal">void*</tt> pointer list. On top of that, a public generic pointer list template is built.</p>
<p>With such a solution, the most important parts of implementation (in this case that would be the list handling code) are kept opaque, and thus changes in that code are propagated the usual way.  The remaining public parts (in this case, that part would probably just cast types) suffer the regular issues with template classes.</p>
<p>It should be noted that the example presented above was very optimistic.  Usually, providing a common opaque implementation is a more demanding task. Sometimes even it is not possible to split out a common amount of code for the base class, and only small pieces of code could be considered common. In these cases, the presented approach may not be beneficial at all.</p>
</div>
</div>
<div class="section" id="enforcing-abi-versioning-with-template-libraries">
<h2>Enforcing ABI versioning with template libraries</h2>
<p>Up to the moment, I have mostly considered avoiding issues resulting from ABI incompatibilities, and avoiding introducing those incompatibilities. But even if the author protects executables from the fore-mentioned issues and provides a mostly-opaque implementation, he should still prepare the library for future ABI (or implementation) changes.</p>
<p>As mentioned before, the most common method used in POSIX-compliant systems is based on <cite>interface versioning</cite> (or even a more fine-grained <cite>symbol versioning</cite> in GNU systems). The concept is mostly straightforward — a shared library declares a range of supported interface versions, and the programs linking against it copy the current version into themselves. When the library is upgraded, and does not support the old interface anymore, the interface version in programs does not match the one in the library, and they refuse to use it without being rebuilt.</p>
<p>Although this technique is used in regular C and C++ libraries to handle ABI changes (effectively propagating the ABI change to consumers), it can be used to enforce implementation changes for templates as well.  This can become particularly useful if a particular template code suffers from serious vulnerabilities or bugs.</p>
<div class="section" id="the-as-needed-problem">
<h3>The <cite>&#8211;as-needed</cite> problem</h3>
<p>The fore-mentioned interface versioning technique requires the program using templates to link against a shared library where the version is defined. This becomes problematic if the template class does not use opaque implementation in the used code (i.e. all methods used by a particular consumer are defined in the template) and the <cite>&#8211;as-needed</cite> GNU linker option is used.</p>
<p>The intent of that option is to link the program executable only with those libraries which it uses directly. Although this often is advantageous and thus the option gained popularity, it thwarts our plans. Because the code using the templates does not call any function from the library we are forcing it to link, the linker will simply skip that library. Effectively, the <cite>ABI link</cite> will be broken, and interface changes from the library won&#8217;t be propagated to template consumers.</p>
<p>Although this specific problem could supposedly be worked around through disabling that option, that is not really feasible in this case. Most importantly, the task of disabling it would either need to be done by the library consumers directly (effectively, making it unlikely to happen) or handled through an external <tt class="docutils literal"><span class="pre">-config</span></tt> application which will hurt cross-compilation.</p>
<p>To cover the topic completely, I&#8217;ll note that such a solution would first have to check whether <cite>&#8211;as-needed</cite> is actually enabled. And due to the specific synopsis of <cite>-Wl</cite> compiler option, finding that would be a semi-difficult task (and different compilers will have different options). Then, it would have to add <cite>-Wl,&#8211;no-as-needed</cite>, the relevant libraries and re-enable <cite>-Wl,&#8211;as-needed</cite>.</p>
<p>Since that thing is neither easy to implement, nor portable, and we can&#8217;t forget that other (future) linkers can inhibit similar behavior using different options or even unconditionally, I believe we should look for another solution. One which works around the actual issue rather than trying to disable the feature exposing it.</p>
</div>
<div class="section" id="using-dummy-symbols-to-enforce-linking">
<h3>Using dummy symbols to enforce linking</h3>
<p>As mentioned earlier, the reason why linker silently ignores our library is that the program doesn&#8217;t actually use any symbol from it. So, in order to work around that, we simply need to define a dummy symbol and use it. Although that may look simple at first, we should give it some thought.</p>
<p>First of all, considering that the symbol has no real meaning, we should try hard to avoid unnecessary impact on the code, both in terms of performance and its size. Preferably, it should be as short as possible, and it should appear on unused (or likely-unused) code paths. However, we should also make sure that the code will actually be reachable — and thus the compiler won&#8217;t optimize it out.</p>
<p>For example, an obvious solution seems to add a <tt class="docutils literal">dummy()</tt> method to our template class, and access the symbol there. Sadly, since the method is never actually used, the compiler doesn&#8217;t even instantiate it. After some thinking, you may reach a consensus that the only safe place for the call is in the constructor or destructor — but if the template class is used as a static class, these may not be used as well.</p>
<p>I described a similar problem on stackoverflow <a class="footnote-reference" href="#id10" id="id9">[5]</a>, and was given a pretty good tip there. Thanks to that, I assembled the following universal solution:</p>
<pre lang="c" class="literal-block"><code>namespace mylib
{
        extern int dummy;

        namespace
        {
                class Dummy
                {
                public:
                        inline Dummy()
                        {
                                dummy = 0;
                        }
                };

                static const Dummy dummy;
        }
}
</code></pre>
<p>With the <tt class="docutils literal">int dummy</tt> variable being declared in the library code.  The main (and possibly the only one) advantage of that solution is that it works with any code. The variable setting code may have side effects, thus the compiler is not allowed to optimize it out. It can however practically optimize it to a single <tt class="docutils literal">mov</tt> instruction.</p>
<p>The disadvantage of that solution is that the assignment operation will be performed once for every single file including the header file. Thus, I would consider the fore-mentioned solution as a last resort, while preferring placing similar assignments in rarely-used (but always compiled) code, whenever possible.</p>
<p>For example, such a code may be within a rare error or exception handler. Just make sure that the compiler will not be allowed to assume that a particular branch will never be executed. For example, the following may be a bad idea:</p>
<pre lang="c" class="literal-block"><code>template &lt;type T&gt;
void C&lt;T&gt;::f(int always_one = 1)
{
        if (always_one != 1)
        {
                dummy = 0;
                throw MyException();
        }

        // ...
}
</code></pre>
<p>Although most likely this code will work as expected, if a compiler decides to inline the function it may notice that it is always called with <cite>1</cite>, effectively removing the whole branch. Similarly, the code should not be placed in unreachable code (because compiler may notice that) or assertions (because with <cite>NDEBUG</cite> they will be removed).  And don&#8217;t forget that if <cite>f()</cite> will not be used directly at all, the whole function can be optimized out.</p>
<p>To sum up: if you have a safe, unlikely-called branch in your code, then it&#8217;s the best place for the assignment. If you don&#8217;t have one, then universal solution is probably the best you can get. Although it will cause unnecessary repetitions of the assignments, every other place is likely to cause even larger number of them.</p>
<table class="docutils footnote" frame="void" id="id10" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id9">[5]</a></td>
<td><a class="reference external" href="http://stackoverflow.com/questions/11917014/enforce-linking-with-a-shared-library-with-wl-as-needed-when-only-templates">http://stackoverflow.com/questions/11917014/enforce-linking-with-a-shared-library-with-wl-as-needed-when-only-templates</a></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="a-short-summary">
<h2>A short summary</h2>
<p>In this article I tried to point out a few common problems related to the ABI in shared libraries using templates. Firstly, I&#8217;d like to note I&#8217;ve focused only on issues arising from their use in the implementation code of a library, without exposing them in the public API.</p>
<p>If templates are exposed in the public API, i.e. used as function arguments or return types, their impact increases rapidly. In that case they become part of the library ABI, and changes in them effectively change the ABI of the library itself. There are solutions which try to avoid that, for example the bridge pattern <a class="footnote-reference" href="#id12" id="id11">[6]</a> but they&#8217;re out of scope of this article.</p>
<p>Still, all the fore-mentioned issues apply. When designing a library of templates, you should be aware of them and know how to handle them.  Most importantly, I believe that you should always be aware that your ABI may change at some point, even if it is very simple and dedicated to a very specific solution, and even if you are going to make it opaque, or take other precautions to avoid it changing in the future.</p>
<p>Thus, the important point here is to design your library in such a way that its consumers will be able to handle these ABI changes gracefully.  The second but nevertheless important problem is actually avoiding ABI changes, either by good design (if possible) or through introducing new classes or methods rather than modifying the existing ones.</p>
<p>However, note that the latter method mentioned is going to require you to either permanently duplicate code (and effectively allowing the consumers to still use the old one), or just delay the ABI change until the compatibility code is removed in favor of requiring your consumers to update their code, thus effectively changing both the API and the ABI.</p>
<table class="docutils footnote" frame="void" id="id12" rules="none">
<colgroup>
<col class="label" />
<col /></colgroup>
<tbody valign="top">
<tr>
<td class="label"><a class="fn-backref" href="#id11">[6]</a></td>
<td><a class="reference external" href="http://en.wikipedia.org/wiki/Bridge_pattern">http://en.wikipedia.org/wiki/Bridge_pattern</a></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/08/20/the-impact-of-cxx-templates-on-library-abi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Research question: integral type sizes on various platforms</title>
		<link>http://blogs.gentoo.org/mgorny/2012/08/15/research-question-integral-type-sizes-on-various-platforms/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/08/15/research-question-integral-type-sizes-on-various-platforms/#comments</comments>
		<pubDate>Wed, 15 Aug 2012 09:23:25 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Programmer's digest]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=151</guid>
		<description><![CDATA[I&#8217;m a bit curious about sizes of various integral types on different platforms, and I&#8217;d really appreciate a little help from people running various non-common architectures/toolchains. I&#8217;ve prepared a little package which just tries to get various type sizes using the C++ compiler, and I&#8217;d really appreciate if you could run it and paste the [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m a bit curious about sizes of various integral types on different platforms, and I&#8217;d really appreciate a little help from people running various non-common architectures/toolchains. I&#8217;ve prepared a little package which just tries to get various type sizes using the C++ compiler, and I&#8217;d really appreciate if you could run it and paste the results in a comment.</p>
<p>To run it:</p>
<pre><code>wget http://dev.gentoo.org/~mgorny/cxx-type-sizes-0.tar.bz2
tar -xf cxx-type-sizes-0.tar.bz2
cd cxx-type-sizes-0/
./configure
make
cat output/_all</code></pre>
<p>It will try to compile a few programs, and then run them. Then it concatenates the results into <code>output/_all</code> and that&#8217;s the file I&#8217;d like to get, along with your platform, toolchain, <ins>CHOST and ARCH, ABI</ins> and everything else you consider relevant.</p>
<p>I&#8217;d really like to get a single output for each architecture, and possibly additional outputs if some toolchain/other magic resulted in different results than the previous one. I&#8217;ll put the results then into a nice table. Thanks in advance.</p>
<p><ins><a rel='external' href='http://dev.gentoo.org/~mgorny/types.html'>Current results</a>.</ins></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/08/15/research-question-integral-type-sizes-on-various-platforms/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>GitHub — or how to re-centralize a DVCS</title>
		<link>http://blogs.gentoo.org/mgorny/2012/07/03/github-or-how-to-re-centralize-a-dvcs/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/07/03/github-or-how-to-re-centralize-a-dvcs/#comments</comments>
		<pubDate>Tue, 03 Jul 2012 09:44:29 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=141</guid>
		<description><![CDATA[What are the most important advantages of git? I think one which should come out pretty early is that it is «distributed», or «decentralized». This simply means that the actual, complete repository moves along with the project. At some point, you could think: where it is hosted shouldn&#8217;t matter that much. Although git doesn&#8217;t facilitate working completely without centralized server (because you need to find updates somewhere), [...]]]></description>
				<content:encoded><![CDATA[<p>What are the most important advantages of git? I think one which should come out pretty early is that it is «distributed», or «decentralized». This simply means that the actual, complete repository moves along with the project. At some point, you could think: where it is hosted shouldn&#8217;t matter that much.</p>
<p>Although git doesn&#8217;t facilitate working completely without centralized server (because you need to find updates somewhere), it should be pretty clear that the repository content should be independent of the hosting service. In other words, hosting service should serve the repository, not enforce its contents.</p>
<p>I think all the madness started on <a rel='external' href='http://code.google.com/'>Google Code</a> project hosting. There, the project wikis were hosted as a subdirectory to svnroot (e.g. in <a rel='external' href='http://code.google.com/p/gecko-mediaplayer/source/browse/'>gecko-mediaplayer sources</a>). I&#8217;m not sure if I can say «it is wrong». On one hand, it&#8217;s bad to keep completely separate codebases in the same repository. On the other, the design of subversion is simply pure madness, and so everything in the repository follows it…</p>
<p>On the other hand, Google got git correctly. When a particular project decides to use git there, it gets three separate repositories (look at <a rel='external' href='http://code.google.com/p/pkgcore/source/browse/'>pkgcore sources</a> for an example).</p>
<p><a rel='external' href='https://github.com'>GitHub</a> got wikis right as well. But <a rel='external' href='http://pages.github.com/'>GitHub Pages</a>… They actually misuse branches in a horrible, messy way. Just look how to <a rel='external' href='https://help.github.com/articles/creating-project-pages-manually'>create project pages manually</a> — they tell you to create an «orphan» branch!</p>
<p>In other words, they tell you to create two repositories in a single repository. Two independent histories. Complete madness! And whether you want it or not, you pull them with every single clone you do. Yes, that could be some kind of advantage but nevertheless it has <em>nothing</em> to do with the source code.</p>
<p>There&#8217;s also this old, ignored issue that they encourage you to rename your <code>README</code> file to their invented suffix just to have it rendered correctly. Once again, hosting services enforces the layout of your repository. And I get really angry getting all those <code>README.md.bz2</code> in my docdir. This is all against the purpose of markup…</p>
<p>Shortly saying, the sole purpose of markup formats like Markdown, reStructuredText, asciidoc is to provide a complete markup on top of plain text. The text which sould be still completely usable for any regular text viewer. And this means that their naming should also follow the common text file naming rules, which means either uppercase names in *nix or <code>.txt</code> suffix in Windows. No custom <code>.md</code>, and certainly not <code>.asciidoc</code>!</p>
<p>It&#8217;s really sad that the very common git hosting sites, instead of encouraging people to use git correctly, force them to hack it around to achieve some minor madness.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/07/03/github-or-how-to-re-centralize-a-dvcs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>vim: smart C/C++ boilerplate templates</title>
		<link>http://blogs.gentoo.org/mgorny/2012/06/13/vim-smart-cc-boilerplate-templates/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/06/13/vim-smart-cc-boilerplate-templates/#comments</comments>
		<pubDate>Wed, 13 Jun 2012 11:33:59 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Programmer's digest]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=130</guid>
		<description><![CDATA[A simple vim scriptie for those who are interested. It is triggered when new C/C++ files are created (e.g. via vim new-file.cxx), and fills it in with boilerplate unit code. What&#8217;s special about it is that it tries to find tips about that code in other files in that or parent directory. The templates The template used for .c/.cxx files is: /* header-comment */ #ifdef HAVE_CONFIG_H [...]]]></description>
				<content:encoded><![CDATA[<p>A simple vim scriptie for those who are interested. It is triggered when new C/C++ files are created (e.g. via <code>vim new-file.cxx</code>), and fills it in with boilerplate unit code. What&#8217;s special about it is that it tries to find tips about that code in other files in that or parent directory.</p>
<h2>The templates</h2>
<p>The template used for .c/.cxx files is:</p>
<pre><code>/* header-comment
 */

#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif</code></pre>
<p>While for .h/.hxx files the following is used:</p>
<pre><code style='overflow:auto'>/* header-comment
 */

#pragma once

#ifndef PREFIX_FN_H
#define PREFIX_FN_H 1

#endif /*PREFIX_FN_H*/</code></pre>
<p>In order to simplify the work flow, the following magic is done:</p>
<ol>
<li><em>header-comment</em> is actually copied from any other file with the same suffix,</li>
<li><em>FN_H</em> is substituted for actual filename,</li>
<li><em>PREFIX</em> is copied from any other file with the same suffix,</li>
<li>if no file is found in the same directory as the new file, the scripts falls back to parent directory, and appends the subdirectory name to <em>PREFIX</em>.</li>
</ol>
<h2>The script</h2>
<p>Just a dirty, quick vimrc. Feel free to improve:</p>
<pre><code>function CTemplate()
  let fext = expand('%:e')
  let ucext = toupper(fext)
  let myprefix = substitute(toupper(expand('%')), '[^A-Z0-9]', '_', 'g')

  let head_done = 0
  let head_line = 0
  let prefix_done = 0

  if fext =~ '^h'
    call append(1, '#pragma once')
    call append(2, '')
    call append(3, '#ifndef @fn@')
    call append(4, '#define @fn@ 1')
    call append(5, '')
    call append(6, '#endif /*@fn@*/')
  else
    call append(1, '#ifdef HAVE_CONFIG_H')
    call append(2, '#  include "config.h"')
    call append(3, '#endif')
    let prefix_done = 1
  endif

  let mywd = expand("%:p:h")
  echo mywd
  let myfiles = glob(mywd . '/*.' . fext, 0, 1)
  let myfiles += glob(mywd . '/../*.' . fext, 0, 1)

  for myfile in myfiles
    if filereadable(myfile)
      for myline in readfile(myfile)
        if !head_done
          if head_line != 0
            call append(head_line, myline)
            if myline =~ '[*][/]'
              let head_done = 1
            else
              let head_line += 1
            endif
          endif

          if head_line == 0 &amp;&amp; myline =~ '^[/][*]'
            call append(0, myline)
            let head_line += 1

            if myline =~ '[*][/]'
              let head_done = 1
            endif
          endif
        endif

        if !prefix_done &amp;&amp; myline =~ '^#ifndef .*_'.ucext
          if myfile =~ '^../'
            let mydir = toupper(expand('%:p:h:t'))

            let myprefix = mydir . '_' . myprefix
          endif

          let myprefix = substitute(myline[8:],
            \ '[^_]*_[^_]*$', '', '') . myprefix
          let prefix_done = 1
        endif
      endfor
    endif

    if head_done &amp;&amp; prefix_done
      break
    endif
  endfor

  if fext =~ '^h'
    exec '%s/@fn@/'.myprefix.'/'
  endif
endfunction</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/06/13/vim-smart-cc-boilerplate-templates/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A C API for C++ and Python ones — or making of libh2o</title>
		<link>http://blogs.gentoo.org/mgorny/2012/06/10/a-c-api-for-c-and-python-ones-or-making-of-libh2o/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/06/10/a-c-api-for-c-and-python-ones-or-making-of-libh2o/#comments</comments>
		<pubDate>Sun, 10 Jun 2012 16:47:02 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Programmer's digest]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=125</guid>
		<description><![CDATA[Lately I spent a lot of time working on a small project of mine called libh2o. Its goal is to provide a library of routines implementing IAPWS IF97 equations for water and steam properties. With the core written in C, and providing a nice-to-use API for C++ and Python. At first, I thought about not providing a «high level» C API at all. It was like: if you want to use plain C, you&#8217;ve gotta [...]]]></description>
				<content:encoded><![CDATA[<p>Lately I spent a lot of time working on a small project of mine called libh2o. Its goal is to provide a library of routines implementing <a rel='external' href='http://iapws.org/'>IAPWS IF97</a> equations for water and steam properties. With the core written in C, and providing a nice-to-use API for C++ and Python.</p>
<p>At first, I thought about not providing a «high level» C API at all. It was like: if you want to use plain C, you&#8217;ve gotta glue all the low-level equations yourself. However, after some thinking I decided to provide one, and built the two remaining APIs (C++ and Python) on top of it.</p>
<p>The main reason for doing this was that Python (well, CPython) is written in C. Although I&#8217;ve seen people writing Python extensions in C++, and even using some of C++ features to make them a little nicer, that&#8217;s still a bunch of ugly C hacks and pointer casts. I don&#8217;t see a really good reason to write a Python extension in C++, nor to make it depend on a C++ compiler when it&#8217;s all limited to C-based CPython API anyway.</p>
<p>And that means that I have either to duplicate all the high-level logic in the Python extension, or just create a C API first and reuse that. Since the whole logic was simple enough to be covered completely and clearly in C, I have chosen this way.</p>
<p>As it happens when people choose C, I had to implement some kind of poor man&#8217;s objectivity. Not something as wide (and ugly) as GObject (someone, <em>please</em> kill it!) but a few bits necessary to keep the state. In other words, a structure keeping the «object» and a bunch of nicely named functions taking it as their first argument.</p>
<p>Before I learnt C++, I would assume that the object structure should be a private (and obscured) blob, and the object type should be an incomplete pointer to it. User should just grab that pointer from a «constructor», pass it around and finally free it through a «destructor». Advantage: the exact struct contents are not the part of ABI.</p>
<p>But now I&#8217;ve decided to go the other way; way similar to how C++ classes work. I&#8217;ve created a structure with explicitly listed private fields (and a very simple <code>/*private:*/</code> comment), and used that as the public type. It doesn&#8217;t need to keep any memory allocated, and is simple enough to be allocated on stack. Advantages: no need for a destructor, and an ability to pack that struct in the C++ class which will wrap it.</p>
<p>Then the usual stuff: a bunch of functions with common prefixes. One prefix for the «namespace», another one for the function (<code>new</code>, <code>get</code>…). All in nice and clear fashion, either to be used directly or wrapped in the C++ or Python APIs.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/06/10/a-c-api-for-c-and-python-ones-or-making-of-libh2o/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A five commandments for XML format designers</title>
		<link>http://blogs.gentoo.org/mgorny/2012/04/25/a-five-commandments-for-xml-format-designers/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/04/25/a-five-commandments-for-xml-format-designers/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 18:32:00 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=121</guid>
		<description><![CDATA[If you&#8217;re designing an XML-based data format, then I beg you, please read the few following rules and obey them. XML may look easy, and even is easy but that doesn&#8217;t mean that writing a good one is. And if you&#8217;re going to invent second HTML, then please, just use JSON or any other random container. That will be easier for you, and easier [...]]]></description>
				<content:encoded><![CDATA[<p>If you&#8217;re designing an XML-based data format, then I beg you, please read the few following rules and obey them. XML may look easy, and even <em>is</em> easy but that doesn&#8217;t mean that writing <em>a good</em> one is. And if you&#8217;re going to invent second HTML, then please, just use JSON or any other random container. That will be easier for you, and easier for us.</p>
<h2>1. Thou shalt always write a schema</h2>
<p>Every XML format should be well described. And no, your ten-stanza poem is not enough. Complete, dedicated Wiki neither. These usually describe nicely (or less nicely) how to <em>write</em> your XML. That could be great if that&#8217;s all you&#8217;re interested in. But if that&#8217;s supposed to be some public format, there is one more important thing…</p>
<p>It&#8217;s called reading. Or parsing. Or just transforming. If you need to handle random XML files, coming from various sources, written by random people, you have to know what you can expect and what can you assume. It&#8217;s not enough to say what <code>&lt;x/&gt;</code> does — I need to know where it can appear and what I can find inside.</p>
<p>There are already well-deployed XML description formats such as DTD, Relax-NG or XML Schema. Please use one of them, I will be grateful. Not only they describe the format strictly and accurately but they also provide a very simple means to validate XML files. It&#8217;s helpful both to us, who parse it, and to people who actually write such XML.</p>
<p>An XML without spec is an XML where every element can appear anywhere in the document. In other words, it&#8217;s not even XML but an ugly <q>tag soup</q>.</p>
<h2>2. Thy XML shalt be structured, not flat</h2>
<p>XML provides means to create neat, hierarchical structures. <em>Use them.</em> If your documents consists of logical parts like sections or chapters, put their complete content in a single <code>&lt;section/&gt;</code> or <code>&lt;chapter&gt;</code>, or any other thing that may come into your head. That&#8217;s the correct way of doing that in XML.</p>
<p>Random headings and separators are not enough. Even if your spec says they always and definitely start a new section, that&#8217;s <em>not enough</em>. If you don&#8217;t believe us, try splitting that thing into parts yourself. Especially when you have sub-headings, sub-sub-headings and so on.</p>
<p>A flat-structured XML is no real XML. It&#8217;s just a text file with a few unnecessary elements.</p>
<h2>3. Thou shalt split text into blocks using XML, not text delimeters</h2>
<p>Even if you think that&#8217;ll make writing much easier, <em>do not</em> ever try to use simple character delimiters to split text into blocks. If you need a list, create a list of XML elements. Like the following:</p>
<pre><code>&lt;l&gt;elem1&lt;/l&gt;
&lt;l&gt;elem2&lt;/l&gt;
&lt;l&gt;elem3&lt;/l&gt;</code></pre>
<p>And yes, I know <code>elem1,elem2,elem3</code> is shorter and easier to type. But guess what — it&#8217;s hell to parse. It isn&#8217;t even XML — you either have to handle it externally or create a complex recursive template which will split it and handle each token separately. That&#8217;s very bad.</p>
<p>An XML which uses random delimeters to create lists is no XML. It&#8217;s called <abbr title="Comma-separated values">CSV</abbr>.</p>
<h2>4. Thou shalt not allow insane structures</h2>
<p>Even if you think noone will create an insane structure in your document, it&#8217;s not enough. Saying it&#8217;s disallowed on your awesome Wiki is not enough either. <em>Forbid it</em> if it&#8217;s supposed to be forbidden.</p>
<p>Otherwise, someone finally will use it. He or she will deliberately ignore your warning because <q>it works</q>. And even if they don&#8217;t, we will have to support it anyway in a compliant parser.</p>
<p>If you expect your data to be interchangeable with widely used formats, take a look at them. Don&#8217;t allow insane things which none of these formats do — or we&#8217;ll have to either refuse to convert some files, convert them incorrectly or waste our time writing complex blocks converting them to sane ones.</p>
<p>Simply, <em>don&#8217;t do it</em>. Even HTML doesn&#8217;t do that… well, that much.</p>
<h2>5. Thou shalt write readable XML, not bytecode</h2>
<p>The major point of using XML is that the data is both readable to machines and humans. Leave it that way. You have the whole human language at your disposal, so don&#8217;t write zeros, ones and other random numbers which are explained on your great Wiki.</p>
<p>Say, an attribute called <code>type</code> should actually name some type. Say, <code>article</code> can be some type. <code>1</code> usually ain&#8217;t. And if that <q>type</q> only describes width of indent, then name it so! Calling it a <q>type</q> is as useful as calling it a <q>thing</q>. Or <code>some-other-thing</code> and <code>a-third-thing</code>.</p>
<p>XML without human-readable text is no XML. Hell, even byte-compiled XML should have readable element names! That&#8217;s the whole point with it. Otherwise, you just end up developing another custom, useless format.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/04/25/a-five-commandments-for-xml-format-designers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The suggested dependencies problem</title>
		<link>http://blogs.gentoo.org/mgorny/2012/04/13/the-suggested-dependencies-problem/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/04/13/the-suggested-dependencies-problem/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 16:46:22 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Gentoo]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=116</guid>
		<description><![CDATA[Optional runtime dependencies (or «suggested dependencies») are one of the late problems we&#8217;re facing in Gentoo. There&#8217;s definitely a need for some standard solution, and it&#8217;d be great to put it in the next EAPI. Sadly, there&#8217;s no consensus how to solve it. The optional dependencies problem Gentoo has a very neat solution for handling optional dependencies and optional features, probably ever since the beginning. It is called «USE flags» and they work [...]]]></description>
				<content:encoded><![CDATA[<p>Optional runtime dependencies (or «suggested dependencies») are one of the late problems we&#8217;re facing in Gentoo. There&#8217;s definitely a need for some standard solution, and it&#8217;d be great to put it in the next EAPI. Sadly, there&#8217;s no consensus how to solve it.</p>
<h2>The optional dependencies problem</h2>
<p>Gentoo has a very neat solution for handling optional dependencies and optional features, probably ever since the beginning. It is called «USE flags» and they work very well with the «traditional» optional dependencies. By that, I mean optional dependencies which are both build- and run-time.</p>
<p>Such a dependencies have to be pulled in before the build process starts, and usually require passing specific options in the configure phase. What&#8217;s important, both enabling and disabling features requires rebuilding the program in question because of code branches being switched. Thus, it&#8217;s perfectly fine if changing USE flags implies rebuilding the package.</p>
<p>Sadly, when it comes to optional runtime dependencies, USE flags are not a perfect solution. «Switching» such a dependency doesn&#8217;t require rebuilding the program anymore. It&#8217;s usually not even switching — the program can determine in runtime whether a particular dependency is available, and either enable or disable respective features. Simple like that.</p>
<p>If one decides to use USE flags for that, they become partially meaningless. Unless flags start stripping off the code (which is a bad idea), feature availability is dependency- rather than flag-based. So, <code>USE=-ssl</code> is irrelevant if, say, <code>pyopenssl</code> is installed. What&#8217;s even worse, flag imply needless rebuilding of such packages just to pull in an additional dependency.</p>
<h2>The simple hack — pkg_postinst() messages</h2>
<p>The simplest solution right now is just listing the suggested dependencies in <code>pkg_postinst()</code> messages. Combined with <code>has_version</code> helper, those messages can give a pretty nice output, pointing out already installed packages — just take a look at <code>sys-apps/systemd</code> ebuild.</p>
<p>Of course, it&#8217;s not a real solution, rather relying on user doing the hard work. The biggest disadvantage is that the dependencies are often going to end up in <em>@world</em>. And then, if user decides to unmerge our package, portage is unable to find and unmerge them as well.</p>
<h2>The SDEPEND solution</h2>
<p>A pretty common idea is to establish a new variable called <code>SDEPEND</code> (for «suggested»). Such a variable would simply list relevant dependencies, and let portage handle the UI part somehow. It is a minimalistic solution, quite consistent with other parts of PMS. Sadly, it has a few big shortcomings.</p>
<p>First, using our current dependency syntax, you can&#8217;t specify that a particular feature requires more than one package; in other words, that two or more suggested dependencies are supposed to be pulled in together. Of course, solving this one would be pretty easy — e.g. by allowing grouping them with parantheses.</p>
<p>A much more important issue is describing what particular dependencies do. Although sometimes this could be guessed by package descriptions pretty well, usually a more friendly text would be great. So, we end up having to implement that somehow.</p>
<p>And that&#8217;s usually when Ciaran comes in with ugly exherbism <code>DEPENDENCIES</code>. Sure, it solves most of the issues pointed out here but, hell, do we really want such a thing? Isn&#8217;t dependency syntax obscure enough already?</p>
<p>And it&#8217;s all rather dependency-oriented. In other words, package comes first, then goes the feature description. «Pass <code>dev-python/pyopenssl</code> or <code>dev-python/python-gnutls</code> to enable secure connections support». I don&#8217;t think that&#8217;s the most user friendly solution.</p>
<h2>The USE flag solution</h2>
<p>Another solution is brining a new category of USE flags. It&#8217;s not important whether they would be specified using a special variable, common <code>USE_EXPAND</code> or another magical features. In fact, that could be a thing totally separate from USE flags. The point is that some of the package flags would be runtime-switchable.</p>
<p>Unlike traditional USE flags, such flags wouldn&#8217;t be stored in vdb. They would be evaluated in place instead, using <code>package.use</code> or similar files, and the dependency tree would use current state of such flags. Of course, they would be allowed for <code>RDEPEND</code> (<code>PDEPEND</code>) use only.</p>
<p>Why reuse USE flags for that? Because it&#8217;s the most user-friendly solution. User doesn&#8217;t have to learn anything new. He/she enables a flag, does <code>emerge -vDtN @world</code> and notices that new dependencies are pulled in but the package doesn&#8217;t have to be rebuilt for that.</p>
<p>If we just add some additional magic for regular USE flags, enabling run-time dependant SSL support could be exactly the same as enabling build-time one — even using the same <code>USE=ssl</code>.</p>
<p>And we could basically even give «backwards» support for older EAPIs. Package managers not supporting the new feature would simply treat runtime-switchable USE flags as regular USE flags, requiring rebuilds of the package.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/04/13/the-suggested-dependencies-problem/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Moving systemd into /usr — the technical side</title>
		<link>http://blogs.gentoo.org/mgorny/2012/01/04/moving-systemd-into-usr-the-technical-side/</link>
		<comments>http://blogs.gentoo.org/mgorny/2012/01/04/moving-systemd-into-usr-the-technical-side/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 22:51:46 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[systemd]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=113</guid>
		<description><![CDATA[Now that I think of it, I really regret I didn&#8217;t make systemd ebuild install it to /usr from the very beginning. But the harm has been done already, and I&#8217;d like to move it ASAP and that&#8217;s why I&#8217;d like to sum up problems with that and possible ways of proceeding with it. The [...]]]></description>
				<content:encoded><![CDATA[<p>Now that I think of it, I really regret I didn&#8217;t make systemd ebuild install it to /usr from the very beginning. But the harm has been done already, and I&#8217;d like to move it ASAP and that&#8217;s why I&#8217;d like to sum up problems with that and possible ways of proceeding with it.</p>
<h2>The idea</h2>
<p>The idea is simple as it is: move systemd install to /usr prefix completely. Right now, there are no technical benefits from keeping it in rootfs. It already depends on libdbus, which is installed in /usr, and I expect more dependencies over time. There&#8217;s no reason to move all those packages into rootfs.</p>
<p>Most importantly, the above information allows me to assume that such move won&#8217;t hurt our split-/usr users — because they already had to have /usr mounted for systemd to run.</p>
<h2>The trouble</h2>
<p>The main problem with the move is that unit files were installed into /lib location by a number of packages. The files can be moved into the new location cleanly only through rebuilding the packages which provide it. They need to stay in search path for systemd to work.</p>
<p>These unit files are symlinked from within /etc/systemd as well. Whenever we move a single unit, we need to update the symlink as well. I&#8217;d really like to avoid forcing users to manually fix that, and the eclass doesn&#8217;t export <code>pkg_postinst()</code> which could help doing it automatically.</p>
<p>The last problem is that people have the systemd location hardcoded into the kernel command-line. This one should be relatively easy to avoid as we can keep compatibility symlink for some time.</p>
<h2>Solution 1: one big move</h2>
<p>The simplest solution for the migration is to move all the relevant units in the systemd ebuild. This way, the unit integrity can be preserved, and symlinks could be updated at once as well. The risk of system breakage should be reduced to minimum.</p>
<p>However, there is an important disadvantage of that method. All those files would be moved out-of-scope of the Package Manager. Thus, after rebuild of every single package providing systemd units, all of our users will have to fight file collisions.</p>
<p>The same will likely apply to our new users, because they will have at least some units installed by random packages already. Users not ever intending to use systemd won&#8217;t be hurt because the move of unit files will be transparent to them as any other package file move.</p>
<h2>Solution 2: temporary support for two locations</h2>
<p>Right now, systemd already supports multiple locations for unit files. As a temporary hack, we could just add /lib/systemd/system to that list. This way, all unit files still installed in /lib will still work as expected when systemd is installed into /usr.</p>
<p>Sadly, this won&#8217;t handle updating /etc symlinks. I could, however, fix that easily by adding a simple <code>.path</code> unit or another solution updating symlinks as soon as files are removed from /lib.</p>
<h2>Other solutions?</h2>
<p>Well, I think the second one is the best we can do. Do you have any other ideas? I guess that udev could face a similar problem if we decide to actually move it into /usr. And there the thing is even worse because the rule install location is usually hardcoded into the ebuild or package build system itself; we will probably need to have even more degree of compatibility.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2012/01/04/moving-systemd-into-usr-the-technical-side/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Why there&#8217;s no IUSE=systemd, logrotate, bash-completion…</title>
		<link>http://blogs.gentoo.org/mgorny/2011/12/16/why-theres-no-iusesystemd-logrotate-bash-completion/</link>
		<comments>http://blogs.gentoo.org/mgorny/2011/12/16/why-theres-no-iusesystemd-logrotate-bash-completion/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 17:09:46 +0000</pubDate>
		<dc:creator>Michał Górny</dc:creator>
				<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[PM utils]]></category>

		<guid isPermaLink="false">https://blogs.gentoo.org/mgorny/?p=103</guid>
		<description><![CDATA[Next tides of users slowly notice that a number of unneeded files is installed on their systems. They enumerate systemd unit files, logrotate files, take their pitchforks and start their cruciates against Gentoo developers wasting their precious disk space. Let me tell you a story. The story starts when Uncle Scarabeus wants to add bash-completion support into libreoffice ebuild. He [...]]]></description>
				<content:encoded><![CDATA[<p>Next tides of users slowly notice that a number of unneeded files is installed on their systems. They enumerate systemd unit files, logrotate files, take their pitchforks and start their cruciates against Gentoo developers wasting their precious disk space.</p>
<p>Let me tell you a story. The story starts when Uncle Scarabeus wants to add bash-completion support into libreoffice ebuild. He considers this a minor addon, not worth the half a day necessary to rebuild libreoffice, so he doesn&#8217;t revbump it. He simply assumes the change will be propagated nicely when users upgrade to the next version.</p>
<p>Of course, some users will already come shouting here: <q>that&#8217;s against the policy!</q> Yes, indeed it is. But is it worth the hassle? Should all libreoffice users be forced to rebuild that huge package just to get a single tiny file installed? He could wait and add that along with the next version. Well, if he wouldn&#8217;t forget about it.</p>
<p>But that&#8217;s not really important part here. Because, to his surprise, many users have actually noticed the change. That&#8217;s because the use of <code>bash-completion.eclass</code> has caused the ebuild to have <code>IUSE=bash-completion</code>; and many of the <code>--newuse</code> Portage option users have rebuilt the package. A few others, like me, just stopped using that option.</p>
<p>That&#8217;s when the discussion started. We — the few devs actually caring about discussing — decided that it is quite pointless to control installing tiny files through USEflags. Of course, the libreoffice is an elephant-case here but so-called regular packages aren&#8217;t much better here. Is there really a reason to rebuild even 10 C files when the only thing going to change is a single, tiny text file being installed or not?</p>
<p>Another solution is to split those files into separate ebuilds. But that&#8217;s usually inconvenient both for users and devs. Users have to notice that they need to emerge an additional package to get the particular file installed, and devs need to maintain that additional package. That starts to become really ridiculous with files like systemd units which are often generated during build-time and store installation paths.</p>
<p>So what to use? <code>INSTALL_MASK</code>, obviously. It&#8217;s an ancient Portage magic which allows you to control which files will be punted from installed files. You can use <code>app-portage/install-mask</code> to quickly set it for the files you don&#8217;t want. It&#8217;s as simple as:</p>
<pre><code># install-mask -a systemd logrotate</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gentoo.org/mgorny/2011/12/16/why-theres-no-iusesystemd-logrotate-bash-completion/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
