<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>valeriu.palos.ro</title>
	
	<link>http://valeriu.palos.ro</link>
	<description>Milk, cookies and segmentation faults.</description>
	<lastBuildDate>Mon, 20 Jun 2011 06:58:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/valeriupalos" /><feedburner:info uri="valeriupalos" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>A simple(r) approach for class-based OOP in JavaScript</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/9_l4jreyzms/</link>
		<comments>http://valeriu.palos.ro/1076/a-simpler-approach-for-class-based-oop-in-javascript/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 16:49:34 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Dynamic scripting]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Techniques and algorithms]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[inheritance chains]]></category>
		<category><![CDATA[instance]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[OOP inheritance]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=1076</guid>
		<description><![CDATA[Like others before me, I found JavaScript&#8217;s prototype-based OOP support &#8211; while very powerful &#8211; quite cumbersome in some situations where I needed a certain structure or hierarchy of objects, situations in which one quickly comes to the conclusion that the best answer would be the concept of a Class. However, while I am well [...]]]></description>
			<content:encoded><![CDATA[<p>Like others before me, I found JavaScript&#8217;s prototype-based OOP support &#8211; while very powerful &#8211; quite cumbersome in some situations where I needed a certain structure or hierarchy of objects, situations in which one quickly comes to the conclusion that the best answer would be the concept of a <strong>Class</strong>.</p>
<p>However, while I am <strong>well</strong> aware that the web is filled with <a href="http://dean.edwards.name/weblog/2006/03/base/" target="_blank">various</a> <a href="http://www.sitepoint.com/javascript-inheritance/" target="_blank">implementations</a> of classical (i.e. class-based) object orientation in JavaScript, I can not help the feeling that they are over-engineered and over-complicated without need, quite often attempting to provide more functionality than is actually necessary. In short, I don&#8217;t feel they&#8217;re &#8220;classical&#8221; at all.</p>
<p>This is why I started working on my own implementation of classical OOP  in JavaScript, which is shown below. Currently this is only a proposal, although I already started using it in one of my projects. Anyway, let&#8217;s look at it first then I&#8217;ll try and explain how it works.</p>
<h3>The Code</h3>
<pre>/** Class factory. */
function Class(members) {

    // proxy constructor
    var Proxy = function() {
        for (var item in members) {
            this[item] = members[item];
        }
    };

    // proxy inheritance
    Proxy.prototype = (members.base || Class).prototype;

    // class constructor
    var Build = members.init || function() {};

    // class inheritance
    Build.prototype = new Proxy();
    Build.prototype.base = Proxy.prototype;
    Build.prototype.constructor = Build;

    // ready
    return Build;
}</pre>
<p>That is all, and this little function can have a whole world of nifty consequences like deep inheritance or access to the super-class via this.base (and others). But here is what happens: the basic principle at work here is the Proxy object, which essentially, contains all the members of our new class (the ones that are passed to the Class factory function as the <code>members</code> parameter).<span id="more-1076"></span></p>
<p>So, instead of copying the given members straight into the newly created class, we put them into another object (i.e. the Proxy) and make <strong>that</strong> the prototype of our new class. This means that <strong>every class will point </strong>(via the <code>prototype</code> property)<strong> to an internal Proxy object holding the actual class members</strong>. The trick is that this Proxy, in turn, points to the ancestor&#8217;s Proxy object (again via the <code>.prototype</code> property) and so on.</p>
<p>The code (and further updates) may be found <a href="https://gist.github.com/1033002">here</a>.</p>
<h3>The Concept</h3>
<p>Here is a visual depiction of the system:</p>
<p style="text-align: center;"><img class="size-full wp-image-1098 aligncenter" title="Inheritance via Proxy objects" src="http://valeriu.palos.ro/wp-content/uploads/2011/06/classjs.png" alt="" width="540" height="215" /></p>
<p>This trick enables one very important feature: classic class inheritance via the <code>prototype</code> property. Class members are never duplicated and the only additional objects ever created are the Proxy objects (one per class). Here are some features:</p>
<h3>The Simplicity</h3>
<p>You can create a class very easily, using a constructor (i.e. <code>init()</code>) and this class can have instance members, class members and/or static members:</p>
<pre>var Person = Class({

    // constructor
    init: function(fname, lname, age, job) {

        // class member
        this.first_name = fname;
        this.last_name = lname;

        // instance members
        this.age = age;
        this.job = job;
    },

    // class members
    first_name: '',
    last_name: '',
});

// static member
Person.nationality = 'Romanian';

// create instance
var man = new Person('George', 'Enescu', 33, 'Classical Composer');</pre>
<h3>The Deep Inheritance</h3>
<p>Classes can have single, but <strong>deep</strong>, inheritance (i.e. inheritance chains) where each object has direct access to the super-class via the <code>.base</code> property:</p>
<pre>var A = Class({
    hello: function() {
        console.log('Hello world!');
    }
});
var B = Class({ // inherit A
    base: A,
    aloha: function() {
        console.log('Aloha world!');
    }
});
var C = Class({ // inherit B
    base: B,
    aloha: function() { // override method from B
        console.log('Goodbye world!');
    }
});

var c = new C();
c.hello(); // 'Hello world!'
c.aloha(); // 'Goodbye world!'
c.base.aloha.call(c); // 'Aloha world!'</pre>
<p>Note: the <code>c.base.aloha()</code> method was invoked via <code>call()</code> in order to apply it onto the <code>c</code> object; invoking it as <code>c.base.aloha()</code> would have applied it onto the <code>c.base</code> object.</p>
<h3>The Prototypal Concepts</h3>
<p>The regular functionality of the <code>prototype</code> property is kept intact, so that changes to <code>A.prototype</code> will implicitly propagate to <strong>everything</strong> derived from class <code>A</code> (all instances, all sub-classes, all instances of sub-classes etc.):</p>
<pre>// extend class A
A.prototype.doStuff = function() {
    console.log('Am doing some extra stuff!');
}

// check any child
c.doStuff(); // 'Am doing some extra stuff!'</pre>
<p><span style="color: #800000;">[Update]</span> This also means that you may affect the prototype of <strong>any</strong> class form an inheritance chain and it will do what you expect (i.e. propagate changes down the chain to all the children <strong>below that class</strong>).</p>
<p>Also the <code>instanceof</code> operator behaves as expected:</p>
<pre>console.log(c instanceof A); // true
console.log(c instanceof B); // true
console.log(c instanceof C); // true</pre>
<p>The <code>.prototype.constructor</code> property is also properly configured, so if you want, you can create new instances starting from an initial object, by extracting the object&#8217;s constructor:</p>
<pre>// identical to C.prototype.constructor
var Cons = Object.getPrototypeOf(c).constructor; 

// create new object
var c2 = new Cons();</pre>
<h3>The Efficiency</h3>
<p>Inheritance is provided without creating a new instance of the base class for every prototype. This is unnatural anyway since it calls the base constructor without arguments, which may have unwanted side-effects:</p>
<pre>NewClass.prototype = new BaseClass(); // the weird way of prototyping</pre>
<p>Also, class members are never copied between classes and no duplicates are ever created, thus saving memory and ensuring high access speed for the class members.</p>
<p>In addition, the entire code is only a few lines long (no, I&#8217;m not going to count), so it has a certain elegance to it.</p>
<h3>The Conclusion</h3>
<p>Maybe this technique is neither new, nor very full featured, but honestly, if I want mixins, extensible objects and other such exotic features, then why would I bother with old-school OOP classes anyway; the JavaScript prototypal approach is capable of <a href="http://webreflection.blogspot.com/2010/01/better-javascript-classes.html" target="_blank">all that</a>, and more!</p>
<p>I&#8217;m not saying that this is *the* way of doing classic OOP in JavaScript, but I certainly feel it is more in-line with what I perceive to be the JavaScript mindset: keep it simple, use what you have at your disposal and don&#8217;t kill yourself trying to emulate other languages!</p>
<p>That being said I also think this code may be improved, so pitch in a comment or two if you like :D&#8230;</p>
<p><span style="color: #808080;">P.S.: For example, the Build() constructor may be easily modified to enforce the concept of abstract classes, effectively refusing to create the instance (i.e. via throw) if it detects that any of its members (inherited or not) is purely virtual (i.e. set to <code>null</code> or <code>undefined</code>). So a sub-class must either implement all the purely virtual members it inherits, or it will simply become abstract as well (I actually use this trick in production).</span></p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/9_l4jreyzms" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/1076/a-simpler-approach-for-class-based-oop-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/1076/a-simpler-approach-for-class-based-oop-in-javascript/</feedburner:origLink></item>
		<item>
		<title>Command-line option parser in JavaScript</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/CabzgAVy9c4/</link>
		<comments>http://valeriu.palos.ro/1026/command-line-option-parser-in-javascript/#comments</comments>
		<pubDate>Fri, 20 May 2011 05:24:59 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Command line tools]]></category>
		<category><![CDATA[Dynamic scripting]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[getopt]]></category>
		<category><![CDATA[getopts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[option parser]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=1026</guid>
		<description><![CDATA[Here&#8217;s a small but very effective command-line option parser written in JavaScript (uses NodeJs). You can get the up-to-date source code at http://gist.github.com/982499. I wrote this because I needed it for a project and I did not think it was worth it to install some npm package just for this. The comment should explain most [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a small but very effective command-line option parser written in JavaScript (uses NodeJs). You can get the up-to-date source code at <a href="https://gist.github.com/982499">http://gist.github.com/982499</a>.</p>
<p>I wrote this because I needed it for a project and I did not think it was worth it to install some npm package just for this. The comment should explain most things you will need but ask me if you don&#8217;t understand something. Note that this code is not well tested (yet!) since I just wrote it this morning so post a comment when you find a bug.</p>
<p>Supports:</p>
<ul>
<li><span style="color: #333399;">Short and long options (i.e. &#8216;<code>-t|--test</code>&#8216;).</span></li>
<li><span style="color: #333399;">Option parameters (i.e. &#8216;<code>-f /etc/resolv.conf</code>&#8216;).</span></li>
<li><span style="color: #333399;">Mandatory options.</span></li>
<li><span style="color: #333399;">Implicit help option.</span></li>
<li><span style="color: #333399;">Option callback functions.</span></li>
<li><span style="color: #333399;">Cumulated short options (i.e. &#8216;<code>-ab</code>&#8216;).</span></li>
<li><span style="color: #333399;">Repeatable options (i.e. &#8216;<code>--add value1 --add value2</code>&#8216;).</span></li>
<li><span style="color: #333399;">Non-option arguments.</span></li>
</ul>
<p>Notes (extracted from the comments):</p>
<ul>
<li>Parser is case-sensitive.</li>
<li>The &#8216;-h|&#8211;help&#8217; option is provided implicitly.</li>
<li>Successfully parsed options will be available as fields in the &#8220;options&#8221; object.</li>
<li>Non-option arguments found will be available in order in the &#8220;arguments&#8221; array.</li>
<li>Options and their parameters must be separated by space.</li>
<li>Either one of «short» or «long» must always be provided.</li>
<li>The «callback» function is optional and is invoked each time the option is encountered.</li>
<li>Cumulated short options are supported (i.e. &#8216;<code>-tv</code>&#8216;).</li>
<li>If an error occurs, the process is halted and the help message is shown.</li>
<li>Options with the &#8220;multiple&#8221; attribute will be cumulated into arrays (even if found only once).</li>
<li>The parser does *not* test for duplicate definitions in the schema array.</li>
</ul>
<p>See <a href="https://gist.github.com/982499">the code</a> for everything else you need to use it (i.e. a sample schema definition). I will not waste any more time talking about it; if you like it, paste it at the top of your NodeJS script and start playing with it, e.g.:</p>
<pre>$ node options.js -tf /some/file.txt foo bar</pre>
<p>Also, please tell me about any improvements or fixes you produce. Thanks and&#8230; enjoy! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/CabzgAVy9c4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/1026/command-line-option-parser-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/1026/command-line-option-parser-in-javascript/</feedburner:origLink></item>
		<item>
		<title>Anastasia</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/G1wXLipKzHU/</link>
		<comments>http://valeriu.palos.ro/1004/anastasia/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 22:28:59 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[2011]]></category>
		<category><![CDATA[Anastasia]]></category>
		<category><![CDATA[January 5th]]></category>
		<category><![CDATA[newborn]]></category>
		<category><![CDATA[speech]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=1004</guid>
		<description><![CDATA[My blog is about programming, that&#8217;s a fact and while I never broke this tradition so far, today I feel like making an exception. Why, you ask? For her: mademoiselle&#8230; Anastasia. She was born on the 5th of January, 2011 and she really wanted to say &#8220;Hi!&#8221; on daddy&#8217;s blog. Who am I to say [...]]]></description>
			<content:encoded><![CDATA[<p>My blog is about programming, that&#8217;s a fact and while I never broke this tradition so far, today I feel like making an exception.</p>
<p>Why, you ask? For her: mademoiselle&#8230; <strong>Anastasia.</strong> <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  She was born on the 5th of January, 2011 and she really wanted to say &#8220;Hi!&#8221; on daddy&#8217;s blog. Who am I to say no to such a gorgeous girl.</p>
<div id="attachment_1005" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/resize-1.jpg"><img class="size-medium wp-image-1005 " title="Anastasia giving a speech" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/resize-1-300x225.jpg" alt="Anastasia giving a speech" width="240" height="180" /></a><p class="wp-caption-text">Anastasia giving a speech</p></div>
<div id="attachment_1006" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/resize.jpg"><img class="size-medium wp-image-1006   " title="Anastasia resting between speeches" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/resize-300x225.jpg" alt="Anastasia resting between speeches" width="240" height="180" /></a><p class="wp-caption-text">Anastasia resting between speeches</p></div>
<p>For more pictures <a href="http://www.peteava.ro/valeriupalos/galerie/1612/anastasia-n-primele-zece-zile-de-via" target="_blank">go here</a>. Enjoy! (I know I do&#8230;) <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/G1wXLipKzHU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/1004/anastasia/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/1004/anastasia/</feedburner:origLink></item>
		<item>
		<title>“Loomiere 2.0 teaser” reloaded</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/-uylvr6Wr94/</link>
		<comments>http://valeriu.palos.ro/980/loomiere-2-0-teaser-reloaded/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 22:08:05 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[4500 connections]]></category>
		<category><![CDATA[6500+ connections]]></category>
		<category><![CDATA[bandwidth]]></category>
		<category><![CDATA[loomiere]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[SSD vs HDD]]></category>
		<category><![CDATA[streaming server]]></category>
		<category><![CDATA[teaser]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=980</guid>
		<description><![CDATA[«Update&#8230; Today this machine reached 6500+ active concurrent connections for a total traffic of 2841.19 Mbps (2778.1 output + 63.09 input) while the SSD&#8217;s were still below 20% load. Now that&#8217;s what I call serious FUN! Here are some pictures&#8230; &#8230;end!» Since my last post about describing the performance of Loomiere in production, quite a [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #3366ff;"><strong>«Update&#8230;</strong></span> Today this machine reached <strong><span style="color: #ff0000;">6500+ active concurrent connections</span></strong> for a total traffic of <span style="color: #ff0000;"><strong>2841.19 Mbps</strong></span> (<strong>2778.1</strong> output + <strong>63.09</strong> input) while the SSD&#8217;s were still below <span style="color: #ff0000;"><strong>20% load</strong></span>. Now that&#8217;s what I call <span style="color: #ff0000;"><strong>serious</strong></span> <strong><span style="color: #ff0000;">FUN</span></strong>! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  Here are some pictures&#8230;</p>
<div id="attachment_1013" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/7ABE.png"><img class="size-medium wp-image-1013  " title="6.5k+ active concurrent streams" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/7ABE-300x145.png" alt="6.5k+ active concurrent streams" width="240" height="116" /></a><p class="wp-caption-text">6.5k+ active concurrent streams</p></div>
<div id="attachment_1014" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/bandwidth-cacti-2771mbps.png"><img class="size-medium wp-image-1014  " title="2841.19 Mbps effective bandwidth" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/bandwidth-cacti-2771mbps-300x149.png" alt="2841.19 Mbps effective bandwidth" width="240" height="119" /></a><p class="wp-caption-text">2841.19 Mbps effective bandwidth</p></div>
<p><strong><span style="color: #3366ff;">&#8230;end!»</span></strong></p>
<p>Since <a href="http://valeriu.palos.ro/836/loomiere-2-0-teaser/">my last post</a> about describing the performance of Loomiere in production, quite a few things have happened. I tested a lot and found many things I would like to improve or add, I tried many hardware configurations to see what goes well with what. In short, I have a whole list of things I want to implement as soon as possible and the most important item on this list <strong>is to actually launch Loomiere to the public</strong> as soon as possible! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>However, here is a quick update on the performance of Loomiere, but this time with a nice twist: it&#8217;s running on a <strong>SSD-based</strong> machine! Here are some specs:</p>
<ul>
<li>Intel Core I7 3GHz</li>
<li>12 GB RAM</li>
<li>6 x 256GB SSD Kingston</li>
<li>3 x 1 Gigabit Ethernet cards</li>
<li>Ubuntu Lucid Lynx with 2.6.37-12-server kernel</li>
</ul>
<p>And here are the results after only a few hours from launch (graphs follow):</p>
<ul>
<li><strong><span style="color: #ff0000;">4500+ active oncurrent connections (streams)</span></strong></li>
<li><strong><span style="color: #ff0000;">300+ MB/s of throughput (aprox. 2500 Gbps)</span></strong></li>
</ul>
<div id="attachment_981" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/4903.png"><img class="size-medium wp-image-981 " title="Concurrent connections" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/4903-300x145.png" alt="Concurrent connections" width="240" height="116" /></a><p class="wp-caption-text">Concurrent connections</p></div>
<div id="attachment_982" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/8739.png"><img class="size-medium wp-image-982 " title="Network traffic: eth0" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/8739-300x145.png" alt="Network traffic: eth0" width="240" height="116" /></a><p class="wp-caption-text">Network traffic: eth0</p></div>
<div id="attachment_983" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/7657.png"><img class="size-medium wp-image-983 " title="Network traffic: eth1" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/7657-300x145.png" alt="Network traffic: eth1" width="240" height="116" /></a><p class="wp-caption-text">Network traffic: eth1</p></div>
<div id="attachment_984" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/BC0F.png"><img class="size-medium wp-image-984 " title="Network traffic: eth2" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/BC0F-300x145.png" alt="Network traffic: eth2" width="240" height="116" /></a><p class="wp-caption-text">Network traffic: eth2</p></div>
<p>However, at this point we hit a wall: the mainboard or the kernel. I have to dig into this problem further, but from what I could gather so far, it seems that the mainboard or the Linux kernel can&#8217;t handle more punishment. Following are the CPU and memory graphs; it is clear that while the memory has no worries, the CPU is hogging! The problem lies somewhere between the I/O operations and the actual sending of data to the sockets (interrupts maybe?). More testing will follow regarding this issue.</p>
<div id="attachment_991" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/80b0.png"><img class="size-medium wp-image-991 " title="CPU activity" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/80b0-300x145.png" alt="CPU activity" width="240" height="116" /></a><p class="wp-caption-text">CPU activity</p></div>
<div id="attachment_993" class="wp-caption alignnone" style="width: 250px"><a href="http://valeriu.palos.ro/wp-content/uploads/2011/01/AD48.png"><img class="size-medium wp-image-993 " title="Memory usage" src="http://valeriu.palos.ro/wp-content/uploads/2011/01/AD48-300x145.png" alt="Memory usage" width="240" height="116" /></a><p class="wp-caption-text">Memory usage</p></div>
<p>Hmm&#8230; this is a deep one.</p>
<p>So, that&#8217;s it for now, I&#8217;m going to sleep. Good night <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/-uylvr6Wr94" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/980/loomiere-2-0-teaser-reloaded/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/980/loomiere-2-0-teaser-reloaded/</feedburner:origLink></item>
		<item>
		<title>Loomiere 2.0 teaser</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/zXOCNJK421g/</link>
		<comments>http://valeriu.palos.ro/836/loomiere-2-0-teaser/#comments</comments>
		<pubDate>Sat, 06 Nov 2010 08:52:19 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[bijk]]></category>
		<category><![CDATA[lightweight]]></category>
		<category><![CDATA[loomiere]]></category>
		<category><![CDATA[loomiere 2.0]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[streaming]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=836</guid>
		<description><![CDATA[This article is a quick peek into how Loomiere 2.0 is (currently) serving close to 3k active streams from a single server in production which is restricted to 1.6 Gbps of bandwidth (we are currently negotiating with our ISP to raise this limitation). All these statistics represent a single machine, in real time, over a period of 12 hours, [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_929" class="wp-caption alignnone" style="width: 250px"><a href="https://www.bijk.com/s/A5D4.png"><img class="size-medium wp-image-929  " title="Concurrent streams" src="http://valeriu.palos.ro/wp-content/uploads/2010/11/A5D4-300x145.png" alt="" width="240" height="116" /></a><p class="wp-caption-text">Concurrent streams</p></div>
<div id="attachment_931" class="wp-caption alignnone" style="width: 250px"><a href="https://www.bijk.com/s/86c9.png"><img class="size-medium wp-image-931  " title="Bandwidth utilization" src="http://valeriu.palos.ro/wp-content/uploads/2010/11/86c9-300x145.png" alt="" width="240" height="116" /></a><p class="wp-caption-text">Bandwidth utilization</p></div>
<p>This article is a quick peek into how Loomiere 2.0 is (currently) serving close to <strong>3k active streams</strong> from a single server in production which is restricted to <strong>1.6 Gbps of bandwidth</strong> (we are currently negotiating with our ISP to raise this limitation). All these statistics represent <strong>a single machine</strong>, in <strong>real time</strong>, over a period of <strong>12 hours</strong>, equipped as follows:</p>
<ul>
<li>2 x Xeon Dual Core (with HyperThreading) at 3.2GHz</li>
<li>16 GB RAM</li>
<li>15 x 1TB HDD (7200rpm)</li>
<li>4 x 1GB NIC&#8217;s</li>
<li>This system runs on Ubuntu 10.04.1 LTS.</li>
</ul>
<p>OK so here are the graphs:</p>
<ul>
<li><a href="https://www.bijk.com/e/812e">Cpu load</a>.</li>
<li><a href="https://www.bijk.com/e/8423">Network traffic</a> (i.e. all interfaces cumulated).</li>
<li><a href="https://www.bijk.com/e/AF6E">Active connections</a> (i.e. concurrent streams).</li>
<li><a href="https://www.bijk.com/e/E379">Memory usage</a>.</li>
</ul>
<p>The graphs may not yet be as stable as I would wish at the moment since I am still testing this setup as well as Loomiere itself, but I think soon I will have a more stable configuration and also some time to comment on these graphs.</p>
<p>Enjoy! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/zXOCNJK421g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/836/loomiere-2-0-teaser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/836/loomiere-2-0-teaser/</feedburner:origLink></item>
		<item>
		<title>Case study: fixed number of iterations with LPeg</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/F2gdzH930ZA/</link>
		<comments>http://valeriu.palos.ro/755/case-study-fixed-number-of-iterations-with-lpeg/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 15:49:02 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Dynamic scripting]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[actionscript3]]></category>
		<category><![CDATA[amf]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[iterations]]></category>
		<category><![CDATA[lpeg]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[pcre]]></category>
		<category><![CDATA[repetitions]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=755</guid>
		<description><![CDATA[Words seem to fail to describe just how awesome LPeg is. Designed as a Lua implementation of the PEG concept, it is a true programming gem! Please, if you dont&#8217;t know what it is, take some time to familiarize yourself with it! It&#8217;s not the easiest thing to grasp, but you will not regret it! [...]]]></description>
			<content:encoded><![CDATA[<p>Words seem to fail to describe just how awesome <a href="http://www.inf.puc-rio.br/~roberto/lpeg/" target="_blank">LPeg</a> is. Designed as a Lua implementation of the <a href="http://en.wikipedia.org/wiki/Parsing_expression_grammar" target="_blank">PEG</a> concept, it is a true programming gem! Please, if you dont&#8217;t know what it is, take some time to familiarize yourself with it! It&#8217;s not the easiest thing to grasp, but you will <strong>not</strong> regret it! It is certainly one of the most worthwhile learning efforts you can make in generic parsing.</p>
<p>One great feature of LPeg is that it&#8217;s<em> binary-safe</em>, meaning that (unlike regular expressions) it can be <em>safely</em> used to parse <em>binary data</em>! This makes it an excellent tool for parsing binary protocols, especially network communication protocols, such as the <a href="http://en.wikipedia.org/wiki/Action_Message_Format" target="_blank">Action Message Format</a> (used by Adobe Flash for making remote calls and even in FLV movie files). I&#8217;ll leave it to you to explore the possibilities&#8230;</p>
<p>Beware that from here on, I assume that you know your way around Lua, LPeg and how they work.</p>
<h3>The problem</h3>
<p>That being said, this article is actually about an unusual roadblock I hit while using LPeg to build a Lua-based AMF parser, and the various solutions I found and/or came up with to overcome it (you didn&#8217;t think that I mentioned AMF before by accident, did you?).</p>
<p>The issue is <span style="color: #0000ff;">LPeg&#8217;s implementation of repetitive patterns: in particular, its inability to match (or capture) <strong>a fixed number of occurrences</strong> of a certain pattern</span>, although it can match a minimum or a maximum number of such occurrences, which is perfect for stream-oriented parsing (such as parsing programming languages) but insufficient for binary data.</p>
<p>Just to clarify, here&#8217;s a small list of LPeg patterns which correspond to the typical <a href="http://www.pcre.org/" target="_blank">PCRE</a> repetitive constructs (in each case we&#8217;re trying to match the string &#8216;cloth&#8217;):</p>
<table style="white-space: nowrap; border: none; width: 100%; margin-bottom: 15px;" cellspacing="1" cellpadding="2">
<tbody>
<tr>
<td style="color: #000; font-weight: bold;">Matching occurrences of &#8216;cloth&#8217;</td>
<td style="color: #000; font-weight: bold;">PCRE pattern</td>
<td style="color: #000; font-weight: bold;">LPeg pattern</td>
</tr>
<tr>
<td>0 or more (at least 0)</td>
<td><code>/(cloth)*/</code></td>
<td><code>lpeg.P'cloth'^0</code></td>
</tr>
<tr>
<td>1 or more (at least 1)</td>
<td><code>/(cloth)+/</code></td>
<td><code>lpeg.P'cloth'^1</code></td>
</tr>
<tr>
<td>X or more (at least X)</td>
<td><code>/(cloth){X,}/</code></td>
<td><code>lpeg.P'cloth'^X</code></td>
</tr>
<tr>
<td>1 or less (at most 1)</td>
<td><code>/(cloth)?/</code></td>
<td><code>lpeg.P'cloth'^-1</code></td>
</tr>
<tr>
<td>X or less (at most X)</td>
<td><code>/(cloth){,X}/</code></td>
<td><code>lpeg.P'cloth'^-X</code></td>
</tr>
<tr>
<td><strong>A:</strong> <span style="color: #ff0000;">precisely X (no more, no less)</span></td>
<td><code>/(cloth){X,X}/</code></td>
<td><code>-- unavailable --</code></td>
</tr>
<tr>
<td><strong>B:</strong> <span style="color: #ff0000;">anywhere between X and Y</td>
<td><code>/(cloth){X,Y}/</code></td>
<td><code>-- unavailable --</code></td>
</tr>
</tbody>
</table>
<p><span style="font-weight: normal;">For the last two cases (i.e. A and B), LPeg does not offer any simple constructs so we have to find a complex one. Let&#8217;s put aside the case B for now, and try to tackle A&#8230;</span><span id="more-755"></span></p>
<h4>Attempt 1 (brute force)</h4>
<p>The first solution was rather straight-forward: we explicitly compose (i.e. append) a pattern for every occurrence we need to match. I call this the <em>brute force</em> method (for obvious reasons). For example, to match the word &#8216;cloth&#8217; exactly three times we would use the pattern <code>lpeg.P'cloth' * lpeg.P'cloth' * lpeg.P'cloth'</code>.</p>
<p>Naturally, this is quite inflexible: what if we need to match 183 occurrences? To achieve that, we use a pattern generation function. Here&#8217;s a complete working example:</p>
<pre class="lang-lua">local lpeg = require'lpeg'

-- Version 1.
-- Generates a big pattern where every interation has
-- a corresponding instance of the initial pattern. The
-- resulting pattern captures all occurrences in a table.
function multiply_1(item, count)
    local set = lpeg.P(true)
    while count &gt; 0 do
        set = set * item
        count = count - 1
    end
    return lpeg.Ct(set)
end

-- Generate subject ('cloth,cloth,...').
local subject = string.rep('cloth,', 10)

-- A capture of the word 'cloth' optionally followed by a comma.
local item = lpeg.C(lpeg.P'cloth') * lpeg.P','^-1

-- Match exactly 10 items.
local result = lpeg.match(multiply_1(item, 10), subject)

-- Display capture table.
print(unpack(result))</pre>
<p>This function adds a composition operation to the big pattern for each occurrence. One method of improving this is to reduce the amount of compositions to the a bare minimum. We do this by duplicating (i.e. growing in powers of 2) the pattern until the required size is reached (also used by LPeg&#8217;s <a href="http://www.inf.puc-rio.br/~roberto/lpeg/re.html" target="_blank">regexp emulator</a>):</p>
<pre class="lang-lua">-- Version 1(b).
-- Generates a (potentially large) pattern using only
-- the minimum possible amount of compositions.
function multiply_1b(item, count)
    local set = lpeg.P(true)
    while count &gt;= 1 do
        if count % 2 &gt;= 1 then
            set = set * item
        end
        item = item * item
        count = count / 2
    end
    return lpeg.Ct(set)
end</pre>
<p>Looks good, however,  this method presents a serious problem:<strong> the resulting pattern can get very large</strong>, forcing the limits of the Lua stack. It works well enough for small jobs, but <strong>it is unwise to generate more than a few hundred repetitions using this method (and even that may be pushing it).</strong> This means that the brute force method is unusable for heavy binary parsing, <strong>where we would often need to match even thousands of pattern repetitions</strong>.</p>
<p>We need to do better&#8230;</p>
<h4>Attempt 2 (capture folding)</h4>
<p>The next approach is a bit more complex and makes use of the more advanced features of LPeg: the <a href="http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html#cap-f">lpeg.Cf()</a> and <a href="http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html#matchtime">lpeg.Cmt()</a> captures.</p>
<p>In essence, we match <em>at most</em> the desired number of occurrences (using the <code>^-N</code> operation described in the table at the top) and on every match (i.e. iteration) we execute a folding function (via <code>lpeg.Cf()</code>) that adds the item to the result table and also decreases a counter. At the end, we execute a checking function (via <code>lpeg.Cmt()</code>) which will test if the counter reached 0 (thus ensuring exact matching) and will fail if not.</p>
<pre class="lang-lua">-- Version 2.
-- Somewhat better approach, which starts with an empty table and gradually
-- folds every occurring capture of the initial pattern into this table, at
-- the same time decrementing the counter. Eventually, it also checks if the
-- counter reached 0 and fails otherwise.
function multiply_2(item, count)
    return lpeg.Cmt(

        -- Fold capture.
        lpeg.Cf(
            lpeg.Ct'' * item^-count,

            -- Append capture.
            function(set, item)
                count = count - 1
                table.insert(set, item)
                return set
            end),

        -- Final check.
        function(s, i, set)
            if count &gt; 0 then
                return false
            end
            return i, set
        end)
end</pre>
<p>Note that the capture starts with an empty table created from the empty string (i.e. <code>lpeg.Ct''</code>). This table is the first parameter given to the folding function (i.e. <code>set</code>); after all the iterations, it will contain all captured items and will then be given to the check-up function for evaluation. Eventually, if all goes well, it will be returned back as the final result of multiply().</p>
<p>Overall this method will behave much better than the first; the generated pattern is much more elegant. However, <strong>the generated pattern can still grow quite large in memory</strong>, even though it&#8217;s not that obvious (i.e. I was surprised to find that it also crashed when generating more than a few thousand iterations).</p>
<p>The issue is the <code>item^-count</code> bit. Checking the C code of the LPeg implementation, quickly reveals that whenever <code>count</code> is not <code>0</code> (i.e. which is a special case), the engine is forced to allocate <code>count</code> objects in memory, and while these objects are lighter than a complete item pattern (as in the first solution) <strong>they are still scaling-up the problem badly</strong>!</p>
<p>Eventually, this method only allows generating repetitive patterns of a few thousand iterations (that is as far as I was able to push it). But, what if we need to handle a rogue pattern of 35 000 occurrences? What then? No!&#8230; we still need better!</p>
<h4>Attempt 3 (classic loop)</h4>
<p>The only thing left to try is a (slightly) more rudimentary technique. Basically, it is the same as the second approach but with a significant twist: instead of using <code>lpeg.Cf()</code> to &#8220;accumulate&#8221; (i.e. fold) every occurrence of item to the result table, we just use a straight-forward loop that will advance through the input stream at every iteration.</p>
<p>That makes the pattern completely dynamic, i.e. totally non-proportional to the number of matched occurrences. As a result, this method allows for an unlimited number of iterations with no memory wastes.</p>
<pre class="lang-lua">-- Version 3.
-- Best method so far, capable of an unlimited number of iterations. It uses
-- an explicit loop to walk over all all iterations at match-time, so there
-- is no potentially huge pattern ever created. This method simply populates
-- the result table at each iteration, or fails the entire set if the count
-- is not reached precisely.
function multiply_3(item, count)
    return lpeg.Cmt(lpeg.P(true),
        function(s, i)
            local set, offset = {}, i
            for j = 1, count do
                set[j], offset = lpeg.match(item * lpeg.Cp(), s, offset)
                if not offset then
                    return false
                end
            end
            return offset, set
        end)
end</pre>
<p>When applied to a string, this will start matching at the current position (i.e. due to the <code>lpeg.P(true)</code> construct). The <code>lpeg.Cmt()</code> capture will trigger the iteration function immediately and there, in turn, will loop for &#8216;count&#8217; number of times, matching the item at every iteration and appending it to the results table. At the same time, at each iteration, the offset is advanced by capturing the position immediately following the matched item (i.e. the <code>lpeg.Cp()</code> construct). If anything goes wrong, then the entire match fails (i.e. via <code>return false</code>).</p>
<p>This method works flawlessly (as far as I could tell). The generated pattern is minimal (memory-wise), the source code is very small, and the function is overall very fast (only slightly slower than the first two approaches, when matching small-sized patterns). But most importantly, with this version, matching huge numbers of item repetitions is trivial (e.g. tens of thousands, millions&#8230;).</p>
<h4>Intervals</h4>
<p>From here, implementing the case <strong>B</strong> (i.e. <code>multiply(X, Y)</code>) is quite easy&#8230;</p>
<p>One approach would be to use <code>multiply_3(item, X)</code> to ensure the minimum number of occurrences (i.e. <strong><em>X</em></strong>) and then append the pattern <code>item^-(Y-X)</code> which will optionally match the remaining occurrences up to the maximum amount allowed. This method will work, and can also be used with the first two solutions, but will still (potentially) waste memory when <code>(Y-X)</code> is large.</p>
<p>A better (and still easy) approach would be to modify the loop inside <code>multiply_3()</code> to also accept an optional number of iterations (above the minimum). Here&#8217;s how&#8230;</p>
<pre class="lang-lua">-- Version 3(b).
-- The final variant with support for interval matching
-- based on the third capturing approach (classic loop).
function multiply_3b(item, min, max)
    return lpeg.Cmt(lpeg.P(true),
        function(s, i)
            local set, offset, attempt = {}, i, 0
            for j = 1, max or min do
                set[j], attempt = lpeg.match(item * lpeg.Cp(), s, offset)
                if not attempt then
                    if j &gt; min then
                        break
                    else
                        return false
                    end
                end
                offset = attempt
            end
            return offset, set
        end)
end</pre>
<h4>In conclusion&#8230;</h4>
<p>So, that&#8217;s that. Enjoy!<br />
I would be surprised if anyone would actually read this banter.<br />
If you did, hat&#8217;s off! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/F2gdzH930ZA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/755/case-study-fixed-number-of-iterations-with-lpeg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/755/case-study-fixed-number-of-iterations-with-lpeg/</feedburner:origLink></item>
		<item>
		<title>Lua2C, an updated version</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/DxeOyCW5noI/</link>
		<comments>http://valeriu.palos.ro/669/lua2c/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 05:26:58 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Command line tools]]></category>
		<category><![CDATA[Dynamic scripting]]></category>
		<category><![CDATA[bin-to-cee]]></category>
		<category><![CDATA[bin2c]]></category>
		<category><![CDATA[bin2c.lua]]></category>
		<category><![CDATA[lpeg]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[lua2c]]></category>
		<category><![CDATA[lua2c.lua]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=669</guid>
		<description><![CDATA[I know I have been &#8220;missing in action&#8221; lately but I am working furiously, and I seem to have too little time for my blog (very sad face). But, just for a breath of fresh air, I thought I&#8217;d share something with the world. Entering lua2c.lua Lately I became quite interested in Lua (a lot [...]]]></description>
			<content:encoded><![CDATA[<p>I know I have been &#8220;missing in action&#8221; lately but I am working <strong>furiously</strong>, and I seem to have too little time for my blog (very sad face). But, just for a breath of fresh air, I thought I&#8217;d share something with the world.</p>
<h2>Entering lua2c.lua</h2>
<p>Lately I became quite interested in Lua (a lot actually). It has phenomenal speed, exceptional interfacing with C and some features and libraries that just make my day (i.e. coroutines, lpeg, lua-ev and others), and since I needed to embed some Lua scripts (entirely) in a C project I&#8217;m currently working on, I ended up adapting <a href="http://lua-users.org/wiki/BinToCee" target="_blank">Mike Edgar&#8217;s &#8220;bin2c.lua&#8221;</a> script (which takes a Lua script and turns it into a C header file) to suit my needs.</p>
<h2>Basic functionality</h2>
<p>Specifically, this adaptation <strong>generates a function that takes a Lua state as the only argument and then runs the embedded Lua code in the given state after which it returns the status</strong> (as opposed to putting the code straight in the top-level scope of the generated file). This makes it easier to embed code in C and then invoke it, and also to apply the same code onto multiple Lua states (e.g. multiple threads).</p>
<p>Check further down for a short usage sample.<span id="more-669"></span></p>
<h2>Options</h2>
<ul>
<li><code>-c</code> compiles to Lua bytecode for (slightly) faster loading (does <strong>not</strong> work with LuaJIT)</li>
<li><code>-s</code> <a href="http://en.wikipedia.org/wiki/Minification_(programming)">minifies</a> the source code before embedding to minimize wastes</li>
<li><code>-e</code> applies a mild (XOR-based) obfuscation to prevent embedding of plain text</li>
<li><code>-u</code> produces a (void) function that never fails and panics on errors</li>
</ul>
<h2>Download</h2>
<p>So here it is, enjoy: <a href="/download/lua2c.lua">lua2c.lua</a> (<strong>MIT license</strong>, same as Lua)<span style="color: #ff0000;"><br />
</span></p>
<h2>Just to be clear</h2>
<p>For example, given a lua script file called &#8216;test.lua&#8217; (shown below) this is how we would go about embedding it into a simple C program called &#8216;test.c&#8217;.</p>
<pre class="lang-lua">-- test.lua: a small sign that we're up and running...
print("I am an embedded Lua script running inside a C binary! Hmm... cozy!")</pre>
<p>And here is the C file (rather overly commented)&#8230;</p>
<pre>/*
 * Include our embedded script.
 */
#include "test.h"

/*
 * Invoke the script.
 */
int main(int argc, char* argv[]) {

    // create a Lua state
    lua_State* L = lua_open();

    // load basic libraries into state
    luaL_openlibs(L);

    // invoke embedded script into state
    // by calling the generated function
    load_test_lua(L);

    // finish
    lua_close(L);
    return 0;
}</pre>
<p>And here&#8217;s how we can compile all this into an executable. Note that I tested this on Ubuntu Lucid and the following packages had to be installed: lua5.1, liblua5.1-0-dev, liblua5.1-lpeg2, liblua5.1-bitop0. Your configuration may differ, and if you don&#8217;t have the pre-built Lua packages, you will have to install them manyally (along with the dependencies)!</p>
<pre># generate (unprotected, minified and encrypted) C code
lua lua2c.lua -seu test.lua &gt; test.h

# compile C program against Lua library
gcc test.c -o test `pkg-config lua5.1 --libs --cflags`

# run program
./test
I am an embedded script running from inside a C binary! Hmm... cozy!</pre>
<p>Let me know if it works fr you, or if you think of some cool improvement! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
Cheers!</p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/DxeOyCW5noI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/669/lua2c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/669/lua2c/</feedburner:origLink></item>
		<item>
		<title>Loomiere/Stream: Revived!</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/Qc7qwWjU594/</link>
		<comments>http://valeriu.palos.ro/629/loomierestream-revived/#comments</comments>
		<pubDate>Sat, 01 May 2010 20:57:59 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Web-development]]></category>
		<category><![CDATA[flv]]></category>
		<category><![CDATA[loomiere]]></category>
		<category><![CDATA[loomiere/stream]]></category>
		<category><![CDATA[moov atom]]></category>
		<category><![CDATA[mp4]]></category>
		<category><![CDATA[mp4 streaming]]></category>
		<category><![CDATA[streaming]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=629</guid>
		<description><![CDATA[OK folks, things have settled down and we are good to go. After some considerations the legal concerns for Loomiere/Stream are now cleared and gone. The source is now released and will be available indefinitely. Again, this streamer (minimally customized) has already been serving all the video content at http://peteava.ro for 3 full months (for [...]]]></description>
			<content:encoded><![CDATA[<p>OK folks, things have settled down and we are good to go. After some considerations the legal concerns for Loomiere/Stream are now cleared and gone. The source is now released and will be available indefinitely. Again, this streamer (minimally customized) has already been serving all the video content at <a href="http://peteava.ro">http://peteava.ro</a> for 3 full months (for those seeking a demo).</p>
<p>Source code: <a href="http://valeriu.palos.ro/wp-content/uploads/2010/05/loomiere-0.2.1.tar.gz">loomiere-0.2.1-tar.gz</a></p>
<p><span style="color: #c30;"><strong>Warning:</strong><br />
Any software downloaded from this website is <strong>*never* </strong>to be associated in any form with pornographic or erotic content! I.E. &#8220;Loomiere/Stream&#8221; must <strong>*never* </strong>be used to stream pornographic or erotic videos! There are plenty alternatives if you can&#8217;t help it.</span></p>
<p><strong>Just a teaser:</strong><br />
Fighting the video-streaming problem has taught me very many things which, in turn, led me to realize that I might be able to use a substantially different streaming approach to achieve a massive amount of optimization in this field. So quite soon, I think I&#8217;ll have ready a brand new (and far more powerful) streamer that aims at making it possible for a single server to serve many thousands (I am still testing this) of streams simultaneously using commodity hardware. I am not yet settled on whether this project will be commercial, open-sourced or both but I hope to clear this aspect soon as well. Real-world streaming (i.e. before and after) statistics will be made available on release.</p>
<p>Stay tuned! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/Qc7qwWjU594" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/629/loomierestream-revived/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/629/loomierestream-revived/</feedburner:origLink></item>
		<item>
		<title>Seven essential WordPress plugins</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/qt0RpZh4JZs/</link>
		<comments>http://valeriu.palos.ro/622/seven-essential-wordpress-plugins/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 08:53:50 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Akismet]]></category>
		<category><![CDATA[Bad-behaviour]]></category>
		<category><![CDATA[CodeColorer]]></category>
		<category><![CDATA[FlagrantDisregard]]></category>
		<category><![CDATA[PHPIDS]]></category>
		<category><![CDATA[wordpress plugins]]></category>
		<category><![CDATA[WP Total Cache]]></category>
		<category><![CDATA[WPIDS]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=622</guid>
		<description><![CDATA[Here are the plugins I recommend for use on a WordPress blog. Most (not all) of these plugins are generic, meaning that they are not bound to a specific type of blog and may (or should) be used regardless of what you write. All of these plugins are top quality (IMHO) and provide an invaluable [...]]]></description>
			<content:encoded><![CDATA[<p>Here are the plugins I recommend for use on a WordPress blog.</p>
<p><strong>Most</strong> (<em>not all</em>) of these plugins are generic, meaning that they are not bound to a specific type of blog and may (or <em>should</em>) be used regardless of what you write.</p>
<p><strong>All</strong> of these plugins are top quality (IMHO) and provide an invaluable service for any blogger. I wholeheartedly recommend that you <strong>use and support them</strong>. I think that <strong>all of these should be apart of a standard</strong> WordPress installation.<br />
<span id="more-622"></span></p>
<h3>1. Akismet</h3>
<p>Website: ﻿<a href="http://akismet.com/download/" target="_blank">http://akismet.com/download/</a><br />
Everyone knows this one but it deserves mention. It prevents content spam. I&#8217;ve used it for about two years and so far it did [edit] <em>not</em> miss! I believe it now comes bundled with WordPress out-of-the-box.</p>
<h3>2. Bad-behaviour</h3>
<p>Website: <a href="http://www.bad-behavior.ioerror.us/download/" target="_blank">http://www.bad-behavior.ioerror.us/download/</a><br />
Prevents link and robot spam. Again, very good service repelling e-mail address harvesters, spam bots and all kinds of evil insects.</p>
<h3>3. WPIDS</h3>
<p>Website: <a href="http://php-ids.org/2007/09/12/wpids-phpids-your-wordpress-the-comfy-way/" target="_blank">http://php-ids.org/2007/09/12/wpids-phpids-your-wordpress-the-comfy-way/</a><br />
An excellent alliance between WordPress and the <a href="http://php-ids.org/" target="_blank">PHPIDS</a> project. It monitors your website for a very wide range of security attacks and does it very well.</p>
<h3>4. WP Total Cache</h3>
<p>Website: <a href="http://www.w3-edge.com/wordpress-plugins/w3-total-cache/" target="_blank">http://www.w3-edge.com/wordpress-plugins/w3-total-cache/</a><br />
This plugin enhances the performance of your blog and it seems to me, after trying out wp-cache and wp-super -cache that this is the best of the lot. It can even use a memcached server (if you have one).</p>
<h3>5. FlagrantDisregard FeedBurner</h3>
<p>Website: <a href="http://flagrantdisregard.com/feedburner/" target="_blank">http://flagrantdisregard.com/feedburner/</a><br />
This plugin provides the fundamental functionality of redirecting you blog feeds to FeedBurner, so you can track your subscribers properly.</p>
<h3>6. WordPress Stats</h3>
<p>Website: <a href="http://wordpress.org/extend/plugins/stats/" target="_blank">http://wordpress.org/extend/plugins/stats/</a><br />
Again, a fundamental feature: basic statistical tracking of your website right in the dashboard.</p>
<h3>7. CodeColorer</h3>
<p>Website: <a href="http://kpumuk.info/projects/wordpress-plugins/codecolorer/" target="_blank">http://kpumuk.info/projects/wordpress-plugins/codecolorer/</a><br />
I have yet to see a better syntax highlighting plugin than this one. Full-featured, configurable, knows any language in the book and <em>very</em> easy to use. Mix in <a href="http://valeriu.palos.ro/595/font-face-for-a-programmers-blog/" target="_blank">a good mono-spaced font</a> and your set!</p>
<p>Ok so maybe there are more, but these, I find myself installing constantly on all blogs that fall into my hands (except number 7 &#8211; CodeColorer &#8211; since it is only meant for posting source code).</p>
<p>Enjoy&#8230; and contribute if you like! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/qt0RpZh4JZs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/622/seven-essential-wordpress-plugins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/622/seven-essential-wordpress-plugins/</feedburner:origLink></item>
		<item>
		<title>@font-face for a programmer’s blog?</title>
		<link>http://feedproxy.google.com/~r/valeriupalos/~3/Jpb2g7PLOl0/</link>
		<comments>http://valeriu.palos.ro/595/font-face-for-a-programmers-blog/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 08:28:51 +0000</pubDate>
		<dc:creator>Valeriu Paloş</dc:creator>
				<category><![CDATA[Other Useful Links]]></category>
		<category><![CDATA[Web-development]]></category>
		<category><![CDATA[@font-face]]></category>
		<category><![CDATA[ascenderfonts]]></category>
		<category><![CDATA[commercial use]]></category>
		<category><![CDATA[consolas]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[fontsquirrel]]></category>
		<category><![CDATA[free fonts]]></category>

		<guid isPermaLink="false">http://valeriu.palos.ro/?p=595</guid>
		<description><![CDATA[I&#8217;m sure that many of you are well aware of the exceptional @font-face CSS3 rule, which made life much easier for many designers, I would think. A few days ago I started wondering if it would be appropriate to use such an instrument on my blog&#8230; the site you&#8217;re on! Yoopie! And, why not!? I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure that many of you are well aware of the exceptional <a href="http://www.css3.info/preview/web-fonts-with-font-face/" target="_blank">@font-face CSS3 rule</a>, which made life much easier for many designers, I would think. A few days ago I started wondering if it would be appropriate to use such an instrument on my blog&#8230; the site you&#8217;re on! Yoopie!</p>
<p><strong>And, why not!?</strong> I love the idea of making my source codes more readable by using some custom font (<a href="http://valeriu.palos.ro/755/case-study-fixed-number-of-iterations-with-lpeg/">Consolas</a> <a href="http://valeriu.palos.ro/537/uri-parsing-using-bash-built-in-features/">in</a> <a href="http://valeriu.palos.ro/669/lua2c/">action</a>). Anyway, I started looking around for some fonts and was not pleased with any of them (well to be fair I did like <a href="http://dejavu-fonts.org/wiki/index.php?title=Main_Page" target="_blank">DejaVu Sans Mono</a> but not as much as Consolas, unfortunately).<br />
<span id="more-595"></span><br />
Long story short, here is <a href="http://www.fontsquirrel.com/fontface" target="_blank"><strong>a very generous list of web-embeddable fonts</strong></a> which can be used freely in commercial scenarios, gathered by the awesome <a href="http://www.fontsquirrel.com" target="_blank">FontSquirrel.com</a> website (an instant friend!). They even provide a <a href="http://www.fontsquirrel.com/fontface/generator" target="_blank">@font-face generator</a> interface that transforms a given font file (TTF or OTF) into the needed formats (mainly EOT for IE) and also generates the embedding CSS3 rules. You should really give it a go!</p>
<p>And if you are wondering how I could speak of embedding Consolas on my blog, knowing full-well the licensing policy of Microsoft in these matters, here is your answer: one can buy a license to embed Consolas on the web (I paid $17.50) from <a href="http://www.ascenderfonts.com/font/consolas-regular.aspx" target="_blank">here</a>.</p>
<p>You can see it in action on any of my posts that contains some source code. Like <a href="http://valeriu.palos.ro/537/uri-parsing-using-bash-built-in-features/">this one</a>! <img src='http://valeriu.palos.ro/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I know, I know! I also would have never believed that I would actually promote a Microsoft product on my blog, but the Consolas font deserves all the credit! It is simply unsurpassed as far as programming goes.</p>
<p>So, does anyone know any other web-embeddable fonts worth mentioning?</p>
<img src="http://feeds.feedburner.com/~r/valeriupalos/~4/Jpb2g7PLOl0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://valeriu.palos.ro/595/font-face-for-a-programmers-blog/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://valeriu.palos.ro/595/font-face-for-a-programmers-blog/</feedburner:origLink></item>
	</channel>
</rss>

