<?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>Labix Blog &#187; C/C++</title>
	<atom:link href="http://blog.labix.org/tag/cc/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.labix.org</link>
	<description>by Gustavo Niemeyer</description>
	<lastBuildDate>Mon, 16 Jan 2012 04:02:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Ensemble, Go, and MongoDB at Canonical</title>
		<link>http://blog.labix.org/2011/08/05/ensemble-go-and-mongodb-at-canonical</link>
		<comments>http://blog.labix.org/2011/08/05/ensemble-go-and-mongodb-at-canonical#comments</comments>
		<pubDate>Fri, 05 Aug 2011 03:49:14 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=706</guid>
		<description><![CDATA[About 1 year after development started in Ensemble, today the stars finally aligned just the right way (review queue mostly empty, no other pressing needs, etc) for me to start writing the specification about the repository system we&#8217;ve been jointly &#8230; <a href="http://blog.labix.org/2011/08/05/ensemble-go-and-mongodb-at-canonical">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>About 1 year after development started in <a href="https://ensemble.ubuntu.com">Ensemble</a>, today the stars finally aligned just the right way (review queue mostly empty, no other pressing needs, etc) for me to start writing the specification about the repository system we&#8217;ve been jointly planning for a long time. This is the system that the Ensemble client will communicate with for discovering which <a href="https://ensemble.ubuntu.com/docs/formula.html">formulas</a> are available, for publishing new formulas, for obtaining formula files for deployment, and so on.</p>
<p><span id="more-706"></span>We of course would have liked for this part of the project to have been specified and written a while ago, but unfortunately that wasn&#8217;t possible for several reasons. That said, there are also good sides of having an important piece flying around in minds and conversations for such a long time: sitting down to specify the system and describe the inner-working details has been a breeze. Even details such as the namespacing of formulas, which hasn&#8217;t been entirely clear in my mind, was just streamed into the document as the ideas we&#8217;ve been evolving finally got together in a written form. </p>
<p>One curious detail: this is the first long term project at <a href="https://www.canonical.com">Canonical</a> that will be developed in <a href="http://golang.org">Go</a>, rather than Python or C/C++, which are the most used languages for projects within Canonical. Not only that, but we&#8217;ll also be using <a href="http://www.mongodb.org">MongoDB</a> for a change, rather than the traditional <a href="http://www.postgresql.com">PostgreSQL</a>, and will also use (you guessed) the <a href="http://labix.org/mgo">mgo driver</a> which I&#8217;ve been pushing entirely as a personal project for about 8 months now.</p>
<p>Naturally, with so many moving parts that are new to the company culture, this is still being seen as a closely watched experiment. Still, this makes me highly excited, because when I started developing mgo, the MongoDB driver for Go, my hopes that the Go, MongoDB, and mgo trio would eventually be used at Canonical were very low, precisely because they were all alien to the culture. We only got here after quite a lot of internal debate, experiments, and trust too.</p>
<p>All of that means these are happy times. Important feature in Ensemble being specified and written, very exciting tools, home grown software being useful..</p>
<p>Awesomeness.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2011/08/05/ensemble-go-and-mongodb-at-canonical/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Efficient algorithm for expanding circular buffers</title>
		<link>http://blog.labix.org/2010/12/23/efficient-algorithm-for-expanding-circular-buffers</link>
		<comments>http://blog.labix.org/2010/12/23/efficient-algorithm-for-expanding-circular-buffers#comments</comments>
		<pubDate>Thu, 23 Dec 2010 12:57:40 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Article]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=580</guid>
		<description><![CDATA[Circular buffers are based on an algorithm well known by any developer who&#8217;s got past the &#8220;Hello world!&#8221; days. They offer a number of key characteristics with wide applicability such as constant and efficient memory use, efficient FIFO semantics, etc. &#8230; <a href="http://blog.labix.org/2010/12/23/efficient-algorithm-for-expanding-circular-buffers">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Circular buffers are based on an algorithm well known by any developer who&#8217;s got past the <i>&#8220;Hello world!&#8221;</i> days.  They offer a number of key characteristics with wide applicability such as constant and efficient memory use, efficient FIFO semantics, etc.</p>
<p>One feature which is not always desired, though, it the fact that circular buffers traditionally will either overwrite the last element, or raise an overflow error, since they are generally implemented as a buffer of <i>constant</i> size.  This is an unwanted property when one is attempting to <i>consume</i> items from the buffer and it is not an option to blindly drop items, for instance.</p>
<p>This post presents an efficient (and potentially novel) algorithm for implementing circular buffers which preserves most of the key aspects of the traditional version, while also supporting dynamic expansion when the buffer would otherwise have its oldest entry overwritten. It&#8217;s not clear if the described approach is novel or not (most of my novel ideas seem to have been written down 40 years ago), so I&#8217;ll publish it below and let you decide.</p>
<p><span id="more-580"></span><b>Traditional circular buffers</b></p>
<p>Before introducing the variant which can actually expand during use, let&#8217;s go through a quick review on traditional circular buffers, so that we can then reuse the nomenclature when extending the concept.  All the snippets provided in this post are written in Python, as a better alternative to pseudo-code, but the concepts are naturally portable to any other language.</p>
<p>So, the most basic circular buffer needs the buffer itself, its total capacity, and a position where the next write should occur.  The following snippet demonstrates the concept in practice:</p>
<pre>
buf = [None, None, None, None, None]
bufcap = len(buf)
pushi = 0   

for elem in range(7):
    buf[pushi] = elem
    pushi = (pushi + 1) % bufcap

print buf # => [5, 6, 2, 3, 4]
</pre>
<p>In the example above, the first two elements of the series (0 and 1) were overwritten once the pointer wrapped around. That&#8217;s the specific feature of circular buffers which the proposal in this post will offer an alternative for.</p>
<p>The snippet below provides a full implementation of the traditional approach, this time including both the pushing and popping logic, and raising an error when an overflow or underflow would occur.  Please note that these snippets are not necessarily idiomatic Python.  The intention is to highlight the algorithm itself.</p>
<pre>
class CircBuf(object):

    def __init__(self):
        self.buf = [None, None, None, None, None]
        self.buflen = self.pushi = self.popi = 0
        self.bufcap = len(self.buf)

    def push(self, x):
        assert self.buflen == 0 or self.pushi != self.popi, \
               "Buffer overflow!"
        self.buf[self.pushi] = x
        self.pushi = (self.pushi + 1) % self.bufcap
        self.buflen += 1

    def pop(self):
        assert self.buflen != 0, "Buffer underflow!"
        x = self.buf[self.popi]
        self.buf[self.popi] = None
        self.buflen -= 1
        self.popi = (self.popi + 1) % self.bufcap
        return x
</pre>
<p>With the basics covered, let&#8217;s look at how to extend this algorithm to support dynamic expansion in case of overflows.</p>
<p><b>Dynamically expanding a circular buffer</b></p>
<p>The approach consists in imagining that the same buffer can contain both a circular buffer area (referred to as <i>the ring area</i> from here on), and an overflow area, and that it is possible to transform a mixed buffer back into a pure circular buffer again.  To clarify what this means, some examples are presented below.  The full algorithm will be presented afterwards.</p>
<p>First, imagine that we have an empty buffer with a capacity of 5 elements as per the snippet above, and then the following operations take place:</p>
<pre>
for i in range(5):
    circbuf.push(i)

circbuf.pop() # => 0
circbuf.pop() # => 1

circbuf.push(5)
circbuf.push(6)

print circbuf.buf # => [<font style="color: blue">5, 6, 2, 3, 4</font>]
</pre>
<p>At this point we have a full buffer, and with the original implementation an additional push would raise an assertion error. To implement expansion, the algorithm will be changed so that those items will be appended at the end of the buffer.  Following the example, pushing two additional elements would behave the following way:</p>
<pre>
circbuf.push(7)
circbuf.push(8)

print circbuf.buf # => [<font style="color: blue">5, 6, 2, 3, 4,</font> <font color="red">7, 8</font>]
</pre>
<p>In that example, elements 7 and 8 are part of the overflow area, and the ring area remains with the same capacity and length of the original buffer. Let&#8217;s perform a few additional operations to see how it would behave when items are popped and pushed while the buffer is split:</p>
<pre>
circbuf.pop() # => 2
circbuf.pop() # => 3
circbuf.push(9)

print circbuf.buf # => [<font style="color: blue">5, 6,</font> None, None, <font style="color: blue">4,</font> <font style="color: red">7, 8, 9</font>]
</pre>
<p>In this case, even though there are two free slots available in the ring area, the last item pushed was still appended at the overflow area.  That&#8217;s necessary to preserve the FIFO semantics of the circular buffer, and means that the buffer may expand more than strictly necessary given the space available. In most cases this should be a reasonable trade off, and should stop happening once the circular buffer size stabilizes to reflect the production vs. consumption pressure (if you have a producer which constantly operates faster than a consumer, though, please look at the literature for plenty of advice on the problem).</p>
<p>The remaining interesting step in that sequence of events is the moment when the ring area capacity is expanded to cover the full allocated buffer again, with the previous overflow area being integrated into the ring area.  This will happen when the content of the previous partial ring area is fully consumed, as shown below:</p>
<pre>
circbuf.pop() # => 4
circbuf.pop() # => 5
circbuf.pop() # => 6
circbuf.push(10)

print circbuf.buf # => [<font style="color: blue">10,</font> None, None, None, None, <font style="color: blue">7, 8, 9</font>]
</pre>
<p>At this point, the whole buffer contains just a ring area and the overflow area is again empty, which means it becomes a traditional circular buffer.</p>
<p><b>Sample algorithm</b></p>
<p>With some simple modifications in the traditional implementation presented previously, the above semantics may be easily supported. Note how the additional properties did not introduce significant overhead. Of course, this version will incur in additional memory allocation to support the buffer expansion, bu that&#8217;s inherent to the problem being solved.</p>
<pre>
class ExpandingCircBuf(object):

    def __init__(self):
        self.buf = [None, None, None, None, None]
        self.buflen = self.ringlen = self.pushi = self.popi = 0
        self.bufcap = self.ringcap = len(self.buf)

    def push(self, x):
        if self.ringlen == self.ringcap or \
           self.ringcap != self.bufcap:
            self.buf.append(x)
            self.buflen += 1
            self.bufcap += 1
            if self.pushi == 0: # Optimization.
                self.ringlen = self.buflen
                self.ringcap = self.bufcap
        else:
            self.buf[self.pushi] = x
            self.pushi = (self.pushi + 1) % self.ringcap
            self.buflen += 1
            self.ringlen += 1

    def pop(self):
        assert self.buflen != 0, "Buffer underflow!"
        x = self.buf[self.popi]
        self.buf[self.popi] = None
        self.buflen -= 1
        self.ringlen -= 1
        if self.ringlen == 0 and self.buflen != 0:
            self.popi = self.ringcap
            self.pushi = 0
            self.ringlen = self.buflen
            self.ringcap = self.bufcap
        else:
            self.popi = (self.popi + 1) % self.ringcap
        return x
</pre>
<p>Note that the above algorithm will allocate each element in the list individually, but in sensible situations it may be better to allocate additional space for the overflow area in advance, to avoid potentially frequent reallocation.  In a situation when the rate of consumption of elements is about the same as the rate of production, for instance, there are advantages in doubling the amount of allocated memory per expansion.  Given the way in which the algorithm works, the previous ring area will be exhausted before the mixed buffer becomes circular again, so with a constant rate of production and an equivalent consumption it will effectively have its size doubled on expansion.</p>
<p><b>UPDATE:</b> Below is shown a version of the same algorithm which not only allows allocating more than one additional slot at a time during expansion, but also incorporates it in the overflow area immediately so that the allocated space is used optimally.</p>
<pre>
class ExpandingCircBuf2(object):

    def __init__(self):
        self.buf = []
        self.buflen = self.ringlen = self.pushi = self.popi = 0
        self.bufcap = self.ringcap = len(self.buf)

    def push(self, x):
        if self.ringcap != self.bufcap:
            expandbuf = (self.pushi == 0)
            expandring = False
        elif self.ringcap == self.ringlen:
            expandbuf = True
            expandring = (self.pushi == 0)
        else:
            expandbuf = False
            expandring = False

        if expandbuf:
            self.pushi = self.bufcap
            expansion = [None, None, None]
            self.buf.extend(expansion)
            self.bufcap += len(expansion)
            if expandring:
                self.ringcap = self.bufcap

        self.buf[self.pushi] = x
        self.buflen += 1
        if self.pushi < self.ringcap:
            self.ringlen += 1
        self.pushi = (self.pushi + 1) % self.bufcap

    def pop(self):
        assert self.buflen != 0, "Buffer underflow!"
        x = self.buf[self.popi]
        self.buf[self.popi] = None
        self.buflen -= 1
        self.ringlen -= 1
        if self.ringlen == 0 and self.buflen != 0:
            self.popi = self.ringcap
            self.ringlen = self.buflen
            self.ringcap = self.bufcap
        else:
            self.popi = (self.popi + 1) % self.ringcap
        return x
</pre>
<p><b>Conclusion</b></p>
<p>This blog post presented an algorithm which supports the expansion of circular buffers while preserving most of their key characteristics.  When not faced with an overflowing buffer, the algorithm should offer very similar performance characteristics to a normal circular buffer, with a few additional instructions and constant space for registers only. When faced with an overflowing buffer, the algorithm maintains the FIFO property and enables using contiguous allocated memory to maintain both the original circular buffer and the additional elements, and follows up reusing the full area as part of a new circular buffer in an attempt to find the proper size for the given use case.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2010/12/23/efficient-algorithm-for-expanding-circular-buffers/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Integrating Go with C: the ZooKeeper binding experience</title>
		<link>http://blog.labix.org/2010/12/10/integrating-go-with-c-the-zookeeper-binding-experience</link>
		<comments>http://blog.labix.org/2010/12/10/integrating-go-with-c-the-zookeeper-binding-experience#comments</comments>
		<pubDate>Fri, 10 Dec 2010 16:17:16 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Article]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=534</guid>
		<description><![CDATA[ZooKeeper is a clever generic coordination server for distributed systems, and is one of the core softwares which facilitate the development of Ensemble (project for automagic IaaS deployments which we push at Canonical), so it was a natural choice to &#8230; <a href="http://blog.labix.org/2010/12/10/integrating-go-with-c-the-zookeeper-binding-experience">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://zookeeper.apache.org">ZooKeeper</a> is a clever generic coordination server for distributed systems, and is one of the core softwares which facilitate the development of Ensemble (project for automagic <a href="http://en.wikipedia.org/wiki/Cloud_computing">IaaS</a> deployments which we push at <a href="http://www.canonical.com">Canonical</a>), so it was a natural choice to experiment with.</p>
<p><a href="https://wiki.ubuntu.com/gozk">Gozk</a> is a complete binding for ZooKeeper which explores the native features of Go to facilitate the interaction with a ZooKeeper server.  To avoid reimplementing the well tested bits of the protocol in an unstable way, Gozk is built on top of the standard C ZooKeeper library.</p>
<p>The experience of integrating ZooKeeper with Go was certainly valuable on itself, and worked as a nice way to learn the details of integrating the Go language with a C library. If you&#8217;re interested in learning a bit about Go, ZooKeeper, or other details related to the creation of bindings and asynchronous programming, please fasten the seatbelt now.</p>
<p><span id="more-534"></span><b>Basics of C wrapping in Go</b></p>
<p>Creating the binding on itself was a pretty interesting experiment already.  I have worked on the creation of quite a few bindings and language bridges before, and must say I was pleasantly surprised with the experience of creating the Go binding.  With <i>Cgo</i>, the name given to the &#8220;<i>foreign function interface</i>&#8221; mechanism for C integration, one basically declares a special import statement which causes a pre-processor to look at the comment preceding it.  Something similar to this:</p>
<pre>
// #include &lt;zookeeper.h&gt;
import "C"
</pre>
<p>The comment doesn&#8217;t have to be restricted to a single line, or to <i>#include</i> statements even.  The C code contained in the comment will be transparently inserted into a helper C file which is compiled and linked with the final object file, and the given snippet will also be parsed and inclusions processed.  In the Go side, that &#8220;C&#8221; import is simulated as if it were a normal Go package so that the C functions, types, and values are all directly accessible.</p>
<p>As an example, a C function with this prototype:</p>
<pre>
int zoo_wexists(zhandle_t *zh, const char *path, watcher_fn watcher,
                void *context, struct Stat *stat);
</pre>
<p>In Go may be used as:</p>
<pre>
cstat := C.struct_Stat{}
rc, cerr := C.zoo_wexists(zk.handle, cpath, nil, nil, &#038;cstat)
</pre>
<p>When the C function is used in a context where two result values are requested, as done above, Cgo will save the well known <i>errno</i> variable after the function has finished executing and will return it wrapped into an <i>os.Errno</i> value.</p>
<p>Also, note how the C struct is defined in a way that can be passed straight to the C function.  Interestingly, the allocation of the memory backing the structure is going to be performed and tracked by the Go runtime, and will be garbage collected appropriately once no more references exist <i>within the Go runtime</i>. This fact has to be kept in mind since the application will crash if a value allocated normally within Go is saved with a foreign C function and maintained after all the Go references are gone.  The alternative in these cases is to call the usual C functions to get hold of memory for the involved values.  That memory won&#8217;t be touched by the garbage collector, and, of course, must be explicitly freed when no longer necessary.  Here is a simple example showing explicit allocation:</p>
<pre>
cbuffer := (*C.char)(C.malloc(bufferSize))
defer C.free(unsafe.Pointer(cbuffer))
</pre>
<p>Note the use of the <i>defer</i> statement above. Even when dealing with foreign functionality, it comes in handy. The above call will ensure that the buffer is deallocated right before the current function returns, for instance, so it&#8217;s a nice way to ensure no leaks happen, even if in the future the function suddenly gets a new exit point which didn&#8217;t consider the allocation of resources.</p>
<p>In terms of typing, Go is more strict than C, and Cgo-based logic will also ensure that the types returned and passed into the foreign C functions are correctly typed, in the same way done for the native types.  Note above, for instance, how the call to the <i>free()</i> function has to explicitly convert the value into an <i>unsafe.Pointer</i>, even though in C no casting would be necessary to pass a pointer into a <i>void *</i> parameter.</p>
<p>The <i>unsafe.Pointer</i> is in fact a very special type within Go. Using it, one can convert any pointer type into any other pointer type in an unsafe way (thus the package name), and also back and forth into a <i>uintptr</i> value with the address of the memory referenced by the pointer.  For every other type conversion, Go will ensure at compilation time that doing the conversion at runtime is a safe operation.</p>
<p>With all of these resources, including the ability to use common Go syntax and functionality even when dealing with foreign types, values, and function calls, the integration task turns out to be quite a pleasant experience.  That said, some of the things may still require some good thinking to get right, as we&#8217;ll see shortly.</p>
<p><b>Watch callbacks and channels</b></p>
<p>One of the most interesting (and slightly tricky) aspects of mapping the ZooKeeper concepts into Go was the &#8220;watch&#8221; functionality.  ZooKeeper allows one to attach a &#8220;watch&#8221; to a node so that the server will report back when changes happen to the given node.  In the C library, this functionality is exposed via a callback function which is executed once the monitored node aspect is modified.</p>
<p>It would certainly be possible to offer this functionality in Go using a similar mechanism, but <a href="http://golang.org/doc/go_spec.html#Channel_types">Go channels</a> provide a number of advantages for that kind of asynchronous notification: waiting for multiple events via the <a href="http://golang.org/doc/go_spec.html#Select_statements">select statement</a>, synchronous blocking until the event happens, testing if the event is already available, etc.</p>
<p>The tricky bit, though, isn&#8217;t the use of channels.  That part is quite simple.  The tricky detail is that the C callback function execution happens in a C thread started by the ZooKeeper library, and happens asynchronously, while the Go application is doing its business elsewhere.  Right now, there&#8217;s no straightforward way to transfer the execution of this asynchronous C function back into the Go land.  The solution for this problem was found with some help from the folks at the <a href="">golang-nuts</a> mailing list, and luckily it&#8217;s not that hard to support or understand.  That said, this is a good opportunity to get some coffee or your preferred focus-enhancing drink.</p>
<p>The solution works like this: when the ZooKeeper C library gets a watch notification, it executes a C callback function which is inside a Gozk helper file. Rather than transferring control to Go right away, this C function simply appends data about the event onto a queue, and signals a pthread condition variable to notify that an event is available.  Then, on the Go side, once the first ZooKeeper connection is initialized, a new goroutine is fired and loops waiting for events to be available.  The interesting detail about this loop, is that it blocks <i>within a foreign C function</i> waiting for an event to be available, through the signaling of the shared pthread condition variable.  In the Go side, that&#8217;s how the call looks like, just to give a more practical feeling:</p>
<pre>
// This will block until there's a watch available.
data := C.wait_for_watch()
</pre>
<p>Then, on the C side, here is the function definition:</p>
<pre>
watch_data *wait_for_watch() {
    watch_data *data = NULL;
    pthread_mutex_lock(&#038;watch_mutex);
    if (first_watch == NULL)
        pthread_cond_wait(&#038;watch_available, &#038;watch_mutex);
    data = first_watch;
    first_watch = first_watch->next;
    pthread_mutex_unlock(&#038;watch_mutex);
    return data;
}
</pre>
<p>As you can see, not really a big deal.  When that kind of blocking occurs inside a foreign C function, the Go runtime will correctly continue the execution of other goroutines within other operating system threads.</p>
<p>The result of this mechanism is a nice to use interface based on channels, which may be explored in different ways depending on the application needs.  Here is a simple example blocking on the event synchronously, for instance:</p>
<pre>
stat, watch, err := zk.ExistsW("/some/path")
if stat == nil &#038;&#038; err == nil {
    event := <-watch
    // Use event ...
}
</pre>
<p><b>Concluding</b></p>
<p>Those were some of the interesting aspects of implementing the ZooKeeper binding.  I would like to speak about some additional details, but this post is rather long already, so I'll keep that for a future opportunity.  The code is available under the LGPL, so if you're curious about some other aspect, or would like to use ZooKeeper with Go, please move on and <a href="https://wiki.ubuntu.com/gozk">check it out</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2010/12/10/integrating-go-with-c-the-zookeeper-binding-experience/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing The Hacking Sandbox</title>
		<link>http://blog.labix.org/2010/09/25/introducing-the-hacking-sandbox</link>
		<comments>http://blog.labix.org/2010/09/25/introducing-the-hacking-sandbox#comments</comments>
		<pubDate>Sat, 25 Sep 2010 16:33:54 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=410</guid>
		<description><![CDATA[When I started programming in Python long ago, one of the features which really hooked me up was the quality interactive interpreter offered with the language implementation. It was (and still is) a fantastic way to experiment with syntax, semantics, &#8230; <a href="http://blog.labix.org/2010/09/25/introducing-the-hacking-sandbox">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I started programming in Python long ago, one of the features which really hooked me up was the quality interactive interpreter offered with the language implementation. It was (and still is) a fantastic way to experiment with syntax, semantics, modules, and whatnot.  So much so that many first-class Python practitioners will happily tell you that the interactive interpreter is used not only as a programming sandbox, but many times as the their personal calculator too.  This kind of interactive interpreter is also known as a <a href="http://en.wikipedia.org/wiki/Read-eval-print_loop">REPL</a>, standing for <i>Read Eval Print Loop</i>, and many languages have pretty advanced choices in that area by now.</p>
<p>After much rejoice with Python&#8217;s REPL, though, and as a normal human being, I&#8217;ve started wishing for more.  The problem has a few different levels, which are easy to understand.</p>
<p><span id="more-410"></span>First, we&#8217;re using <a href="http://twistedmatrix.com/">Python Twisted</a> in Ensemble, one of the projects being pushed at Canonical.  Twisted is an event-driven framework, which among other things means it works a lot with closures and callbacks.  Having to redefine multi-line functions frequently to drive experiments isn&#8217;t exactly fun in a line-based interactive interpreter.  Then, some of the languages I&#8217;ve started playing with, such as <a href="http://erlang.org">Erlang</a>, have limited REPLs which differ in functionality significantly compared to what may be done in a text file. And finally, other languages I&#8217;ve been programming with recently, such as <a href="http://golang.org">Go</a>, lack a reasonable REPL altogether (there are only unusable hacks around).</p>
<p>Alright, so here is the idea: what if instead of being given an interactive REPL, you were presented with your favorite text editor, and whenever you wrote the file down, it was executed and results presented?  That&#8217;s The Hacking Sandbox, or <a href="http://labix.org/hsandbox">hsandbox</a>.  It supports 11 different programming languages out of the box, and given its nature it should be trivial to support any other language.</p>
<p>Here is a screenshot to clarify the idea:</p>
<p><a href="http://blog.labix.org/wp-content/uploads/2010/09/hsandbox.png"><img src="http://blog.labix.org/wp-content/uploads/2010/09/hsandbox.png" alt="" title="hsandbox screenshot" width="600" height="359" class="aligncenter size-full wp-image-417" /></a></p>
<p>Note that if you open a sandbox for a language like C or Go, the skeleton of what&#8217;s needed to run a program will already be in place, so you just have to &#8220;fill the blanks&#8221;.</p>
<p>For more details and download information, please check the <a href="http://j.mp/hsandbox">hsandbox web page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2010/09/25/introducing-the-hacking-sandbox/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Changing people or changing rules</title>
		<link>http://blog.labix.org/2009/05/16/changing-people-or-changing-rules</link>
		<comments>http://blog.labix.org/2009/05/16/changing-people-or-changing-rules#comments</comments>
		<pubDate>Sat, 16 May 2009 13:02:20 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=109</guid>
		<description><![CDATA[In my previous post I made an open statement which I&#8217;d like to clarify a bit further: (&#8230;) when the rules don’t work for people, the rules should be changed, not the people. This leaves a lot of room for &#8230; <a href="http://blog.labix.org/2009/05/16/changing-people-or-changing-rules">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In my previous post I made an open statement which I&#8217;d like to clarify a bit further:</p>
<blockquote><p>(&#8230;) when the rules don’t work for people, the rules should be changed, not the people.</p></blockquote>
<p>This leaves a lot of room for personal interpretation of what was actually meant, and TIm Hoffman pointed that out nicely with the following questioning in a comment:</p>
<blockquote><p>I wonder when the rule is important enough to change the people though. For instance [, if your] development process is oriented to TDD and people don’t write the tests or do the job poorly will you change them then?</p></blockquote>
<p>This is indeed a nice scenario to explore the idea.  If it happens at some point that a team claims to be using TDD, but if in practice no developer actually writes tests first, the rules are clearly not working.  If everyone in the team hates doing TDD, enforcing it most probably won&#8217;t show its intended benefits, and that was the heart of my comment.  You can&#8217;t simply keep the rule as is if no one follows it, unless you don&#8217;t really care about the outcome of the rule.</p>
<p>One interesting point, though, is that when you have a high level of influence over the environment in which people are, it may be possible to tweak the rules <i>or</i> the processes to adapt to reality, and tweaking the processes may change the way that people feel about the rules as a consequence (arguably, <i>changing people</i> as a side effect).</p>
<p>As a more concrete example, if I found myself in the described scenario,  I&#8217;d try to understand why TDD is not working, and would try to discuss with the team to see how we should change the process so that it starts to work for us somehow.  Maybe what would be needed is more discussion to show the value of TDD, and perhaps some pair programming with people that do TDD very well so that the joy of doing it becomes more visible.</p>
<p>In either case, I wouldn&#8217;t be simply asking people &#8220;<i>Everyone has to do TDD from now on!</i>&#8220;, I&#8217;d be tweaking the process so that it feels better and more natural to people.  Then, if nothing similar works either, well, let&#8217;s change the rule.  I&#8217;d try to use more conventional unit testing or some other system which people do follow more naturally and that presents similar benefits.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2009/05/16/changing-people-or-changing-rules/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class member access control: enforcement vs. convention</title>
		<link>http://blog.labix.org/2009/05/15/class-member-access-control-enforcement-vs-convention</link>
		<comments>http://blog.labix.org/2009/05/15/class-member-access-control-enforcement-vs-convention#comments</comments>
		<pubDate>Fri, 15 May 2009 09:26:06 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=99</guid>
		<description><![CDATA[For a long time I&#8217;ve been an advocate of Python&#8217;s notion of controlling access to private and protected members (attributes, methods, etc) with conventions, by simply naming them like &#8220;_name&#8221;, with an initial underline.  Even though Python does support the &#8230; <a href="http://blog.labix.org/2009/05/15/class-member-access-control-enforcement-vs-convention">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a long time I&#8217;ve been an advocate of Python&#8217;s notion of controlling access to private and protected members (attributes, methods, etc) with conventions, by simply naming them like &#8220;_name&#8221;, with an initial underline.  Even though Python does support the &#8220;__name&#8221; (with double underscore) for &#8220;private&#8221; members (this actually mangles the name rather than hiding it), you&#8217;ll notice that even this is rarely used in practice, and the largely agreed mantra is that convention should be enough and thus one underscore suffices.  This always resonated quite well with me, since I generally prefer to handle situations by agreement rather than enforcement.  Well, I&#8217;m now changing my opinion.that this works well for this purpose, at least in certain situations.</p>
<p>This methodology may work quite well in situations where the code scope is within a very controlled environment, with one or more teams which follow strictly a single development guideline, and have the power to refactor the affected code base somewhat easily when the original decisions are too limiting.</p>
<p>Having worked on a few major projects now, and some of them being libraries which are used by several teams within the same company or outside, I now perceive that people very often take shortcuts over these decisions for getting their job done quickly.  It&#8217;s way easier to simply read the code and get to the private guts of a library than to try to get agreement over the right way to do something, or sending a patch with a suggested change which was carefully architected.</p>
<p>Many people by now are probably thinking: &#8220;Well, that&#8217;s <i>their</i> problem, isn&#8217;t it?  If their code base breaks on the next upgrade they&#8217;ll get burden and won&#8217;t be able to upgrade cleanly.&#8221;, and I can honestly understand this feeling, since I shared it.  But, for a number of reasons, I now understand that this isn&#8217;t just <i>their</i> problem, it&#8217;s very much <i>my</i> problem too.</p>
<p>Most importantly, on any serious software, these problems will usually come back to the implementors, and many times the problem will have a much larger magnitude by then than they had at the time a change could have been done &#8220;the right way&#8221; on the implementation, because code dependent on the private bits will have settled.</p>
<p>Most people are optimist by nature and believe that the implementation won&#8217;t change, but, of course, one of the reasons why private information is made private in the first place is exactly because the implementor believes that having the freedom to change these details in the future is important, and not rarely there&#8217;s already a plan of evolution in place for these private pieces, which may include revamping the implementation entirely for scalability or for other goals.</p>
<p>In the best case, the careless people will get burden on the upgrade and will ask for support or simply won&#8217;t upgrade silently, and both cases hurt implementors, because providing support for broken software takes time and energy, and amazingly can even hurt the software image. Lack of upgrades also means more ancient versions in the wild to give support for.  Besides these, in the worst case scenario, the careless people have enough influence on the affected project to cause as much burden on it as if the private data was public in the first place.</p>
<p>As much as I&#8217;m a believer in handling situation by agreement rather than enforcement, I&#8217;m also a believer that when the rules don&#8217;t work for people, the rules should be changed, not the people.  So my positioning now is that the language supported access constraints (public, protected, private), as available in languages like Java and C++, are a better alternative when compared to convention as used today in Python, since they provide an additional layer of encouragement for people to not break the rules carelessly, and that helps in the maintenance and reuse of software that has greater visibility.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2009/05/15/class-member-access-control-enforcement-vs-convention/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Comparing package versions in PostgreSQL</title>
		<link>http://blog.labix.org/2006/07/30/comparing-package-versions-in-postgresql</link>
		<comments>http://blog.labix.org/2006/07/30/comparing-package-versions-in-postgresql#comments</comments>
		<pubDate>Mon, 31 Jul 2006 00:46:04 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.labix.org/2006/07/30/comparing-package-versions-in-postgresql/</guid>
		<description><![CDATA[This weekend I&#8217;ve played a bit with PostgreSQL extensions written in C. A while ago I wrote a Python C extension for Smart to compare Debian package versions. Now I was trying to do something similar inside PostgreSQL, and thus &#8230; <a href="http://blog.labix.org/2006/07/30/comparing-package-versions-in-postgresql">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This weekend I&#8217;ve played a bit with <a href="http://www.postgresql.org/docs/8.1/interactive/extend.html">PostgreSQL extensions</a> written in C.</p>
<p>A while ago I wrote a Python C extension for <a href="http://labix.org/smart">Smart</a> to compare <a href="http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version">Debian package versions</a>. Now I was trying to do something similar inside PostgreSQL, and thus ported the original Python C extension code to a PostgreSQL C extension. It enables queries like the following:</p>
<pre>
# SELECT 'Matched' WHERE deb_version_match('1.2', '<=', '2.0');
  ?column?
----------
 Matched
</pre>
<p>The implementation of the PostgreSQL C extension was quite straightforward, but I'm a bit disappointed by the performance of <a href="http://www.postgresql.org/docs/8.1/interactive/plpgsql.html">PL/PGSQL</a>.</p>
<p>I've made tests using two environments. One of them is a <a href="http://www.postgresql.org/docs/8.1/interactive/plpython.html">PL/Python</a> function executed as a <a href="http://www.postgresql.org/docs/8.1/interactive/triggers.html">trigger</a>, which calls the original Python C extension and executes SQL back in PostgreSQL using the <a href="http://www.postgresql.org/docs/8.1/interactive/plpython-database.html">plpy</a> module. The other is a  PL/PGSQL function which uses the implemented PostgreSQL C extension directly.</p>
<p>Considering that the function logic consisted of one loop over a SELECT statement, a few tests, and an INSERT statement, I was expecting that the overhead introduced by going back and forth between the PostgreSQL state and the Python interpreter state would be a lot more noticeable when compared with PL/PGSQL executing an internal PostgreSQL function. Tests have shown about 10% of improvement roughly, when doing a similar logic over about 5000 items.</p>
<p>I'm not yet sure if the speed improvement pays off the limited debugging feedback provided by the PL/PGSQL interpreter on errors.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2006/07/30/comparing-package-versions-in-postgresql/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Smart Package Manager is out!</title>
		<link>http://blog.labix.org/2004/12/09/smart-package-manager-is-out</link>
		<comments>http://blog.labix.org/2004/12/09/smart-package-manager-is-out#comments</comments>
		<pubDate>Thu, 09 Dec 2004 07:07:00 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.labix.org/2004/12/09/smart-package-manager-is-out/</guid>
		<description><![CDATA[After 6 months of fun working on the project in silence, Smart Package Manager was finally released. As the README says: The Smart Package Manager project has the ambitious objective of creating smart and portable algorithms for solving adequately the &#8230; <a href="http://blog.labix.org/2004/12/09/smart-package-manager-is-out">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After 6 months of fun working on the project in silence, <a href="http://smartpm.org">Smart Package Manager</a> was finally released.</p>
<p>As the <a href="http://linux-br.conectiva.com.br/~niemeyer/smart/doc/README.html">README</a> says:</p>
<p><i>The Smart Package Manager project has the ambitious objective of creating smart and portable algorithms for solving adequately the problem of managing software upgrading and installation. This tool works in all major distributions, and will bring notable advantages over native tools currently in use (APT, APT-RPM, YUM, URPMI, etc).</i></p>
<p>Some of the interesting features, which are covered in more detail in the README file: </p>
<ul>
<li>Smart transactions
<li>Multiple interfaces
<li>Support for several channel formats
<li>Priority handling
<li>Autobalancing mirror system
<li>Parallel downloading mechanism
<li>Flexible removable media support
</ul>
<p>Have fun! <img src='http://blog.labix.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2004/12/09/smart-package-manager-is-out/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embedding Lua interpreter into RPM</title>
		<link>http://blog.labix.org/2004/03/23/embedding-lua-interpreter-into-rpm</link>
		<comments>http://blog.labix.org/2004/03/23/embedding-lua-interpreter-into-rpm#comments</comments>
		<pubDate>Tue, 23 Mar 2004 06:59:00 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Patch]]></category>

		<guid isPermaLink="false">http://blog.labix.org/2004/03/23/embedding-lua-interpreter-into-rpm/</guid>
		<description><![CDATA[I&#8217;ve recently committed into the RPM CVS HEAD the internal support for Lua scripts. Please, notice that this is experimental stuff. Why embedding Lua in RPM? Many scripts execute simple operations which in an internal interpreter require no forking at &#8230; <a href="http://blog.labix.org/2004/03/23/embedding-lua-interpreter-into-rpm">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently committed into the <a href="http://www.rpm.org">RPM</a> CVS HEAD the internal support for <a href="http://www.lua.org">Lua</a> scripts.</p>
<p>Please, notice that this is experimental stuff.</p>
<p><b>Why embedding Lua in RPM?</b></p>
<ul>
<li>Many scripts execute simple operations which in an internal interpreter require no forking at all
<li>Internal scripts reduce or eliminate external dependencies related to script slots
<li>Internal scripts operate even under unfriendly situations like stripped chroots (anyone said installers?)
<li>Internal scripts in Lua are really fast
<li>Syntax errors in internal scripts are detected at package building time
</ul>
<p><b>How it works?</b></p>
<p>Just use <tt>-p &lt;lua&gt;</tt> in any script slot (%pre, %post, etc).</p>
<p>For example: </p>
<pre>
%pre -p &lt;lua&gt;
print("Wow! It really works!")
</pre>
<p><b>What is accessible from Lua?</b></p>
<p>The standard Lua library, the posix module (basic system access, by Luiz Henrique de Figueiredo and Claudio Terra), and the rex module (regular expressions, by Reuben Thomas).</p>
<p><b>Macro support</b></p>
<p>Support for Lua macros was also introduced. It means that one can create custom content using Lua macros anywhere.</p>
<p>For example: </p>
<pre>
%{lua: print("Requires: hello-world &gt; 1.0") }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2004/03/23/embedding-lua-interpreter-into-rpm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More additions to APT-RPM Lua interface</title>
		<link>http://blog.labix.org/2004/03/14/more-additions-to-apt-rpm-lua-interface</link>
		<comments>http://blog.labix.org/2004/03/14/more-additions-to-apt-rpm-lua-interface#comments</comments>
		<pubDate>Sun, 14 Mar 2004 06:54:00 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Project]]></category>

		<guid isPermaLink="false">http://blog.labix.org/2004/03/14/more-additions-to-apt-rpm-lua-interface/</guid>
		<description><![CDATA[The APT-RPM Lua interface is constantly being improved. This time, the following functions were added: pkgid() and verid() Return a unique integer identifying a package or a version. verpkg() Returns the parent package of some given version. verdeplist() Returns a &#8230; <a href="http://blog.labix.org/2004/03/14/more-additions-to-apt-rpm-lua-interface">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The APT-RPM Lua interface is constantly being improved. This time, the following functions were added: </p>
<dl>
<dt><b>pkgid()</b> and <b>verid()</b></p>
<dd>Return a unique integer identifying a package or a version. </p>
<dt><b>verpkg()</b></p>
<dd>Returns the parent package of some given version.</p>
<dt><b>verdeplist()</b></p>
<dd>Returns a list of dependencies for a given package, including complete information about it.<br /> 
</dl>
<p>These new functions were introduced to give support for something which is frequently asked by APT-RPM users: the ability to discover which installed packages are not required by any other installed package.</p>
<p>Here is a script using these functions to list these packages. This script will be called list-nodeps, and will be available in the contrib/ directory of the next APT-RPM release. </p>
<pre>
-- Collect dependencies from installed packages
deplist = {}
verlist = {}
for i, pkg in ipairs(pkglist()) do
    ver = pkgvercur(pkg)
    if ver then
        table.insert(verlist, ver)
        for i, dep in ipairs(verdeplist(ver)) do
            for i, depver in ipairs(dep.verlist) do
                deplist[verid(depver)] = true
            end
        end
    end
end

-- Now list all versions which are not dependencies
for i, ver in ipairs(verlist) do
    if not deplist[verid(ver)] then
        name = pkgname(verpkg(ver))
        if name ~= "gpg-pubkey" then
            -- Print package name and version without epoch
            -- (rpm -e friendly <img src='http://blog.labix.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .
            print(name.."-"..string.gsub(verstr(ver), "^%d+:", ""))
        end
    end
end
</pre>
<p>More information about the introduced functions is available in https://moin.conectiva.com.br/AptRpm/Scripting</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2004/03/14/more-additions-to-apt-rpm-lua-interface/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

