<?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>coder . cl &#187; algorithms</title>
	<atom:link href="http://coder.cl/category/programming/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.cl</link>
	<description>system programmer &#38; web developer</description>
	<lastBuildDate>Thu, 29 Jul 2010 21:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>using dependency injection on gui</title>
		<link>http://coder.cl/2010/04/using-dependency-injection-on-gui/</link>
		<comments>http://coder.cl/2010/04/using-dependency-injection-on-gui/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 17:04:32 +0000</pubDate>
		<dc:creator>Daniel Molina Wegener</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=670</guid>
		<description><![CDATA[Many people know the Model View Controller architectural pattern. Another interesting pattern is Presentation Abstraction Control architectural pattern. We can implement it using Dependency Injection or similar Inversion of Control patterns. So, when we are modeling solutions, we many times leave the control or business logic on the Controller. This will guide us to a [...]]]></description>
			<content:encoded><![CDATA[<p>Many people know the <i>Model View Controller</i> architectural pattern. Another interesting pattern is <i>Presentation Abstraction Control</i> architectural pattern. We can implement it using <i>Dependency Injection</i> or similar <i>Inversion of Control</i> patterns. So, when we are modeling solutions, we many times leave the control or business logic on the <i>Controller</i>. This will guide us to a very coupled platform. Remember that any well designed architecture claims for cohesion instead of coupling its components. On this article I will try to analyze the use of IoC on the PAC pattern, so we can have more maintainable software components, mainly on <i>n-tier architectures</i>.</p>
<p><span id="more-670"></span></p>
<p><br/></p>
<h3>control classes and interfaces</h3>
<p>We can hold our business logic on separate classes. As example we will use a <i>bank loan</i> application, which is supposed to bring us the proper load for certain kind of customers of a bank. So, we need to define an interface pf the loan component.</p>
<pre name='code' class='java'>
interface Loan {
    void setUserData(User bankUser);
    void setCustomerData(Customer cus, Account acc);
    LoanResults getLoanResults();
}
</pre>
<p>This simple interface should be implemented by a variety of Loan types, from consumer ones to mortgage ones, since this variety of loans have different evaluation and estimation logic.</p>
<div style='border: 1px solid #000; text-align: center; overflow: scroll;'>
<img src='http://coder.cl/uploads/2010/04/loan-classes.png' title='Loan Classes' alt='Loan Classes' width='510'>
</div>
<p>Now we need to define a way to instantiate the proper Loan implementation. The proper Loan implementation request is on the hands of the control layer, since the Loan interface is the <i>abstraction</i> layer itself. So, if we have to need the same methods for every Loan that we request, we just use a <i>Factory Pattern</i> returning the interface Loan, instead of his real implementation.</p>
<p><br/></p>
<h3>the abstraction layer</h3>
<p>The abstraction layer is implemented on the Loan interface and its returning implementation based on the factory pattern. So we need to implement the factory outside of the package that holds that set of interfaces and implementations. The factory also hold the logic that <i>decides</i> which Loan implementation will be returned, based on the user data, customer data and account data. So we need a call similar to this one:</p>
<pre name='code' class='java'>
Loan loanRequest = LoanFactory.getInstance(data);
LoanResults requestResults = loanRequest.getLoanResults();
</pre>
<p>So, the <i>factory</i> and the <i>loan interface</i> holds the abstraction layer, separating the decoupling the business logic from its real implementation and generating more cohesion. What does the loan request form in the application about those components? The answer is quiet simple: nothing. We will use another pattern to hold input data and pass it through the abstraction layer from the presentation layer. Here is when we are doing <i>dependency injection</i>.</p>
<p><br/></p>
<h3>the presentation layer</h3>
<p>The presentation layer, which in this case holds a simple <i>LoanRequestForm</i> will never know which concrete class will be used to deliver the <i>LoanResults</i>, so we have to define an instance of another pattern, an <i>Adapter</i>. The adapter should transform the input data from the LoanRequestForm to some kind of Data Transfer Object, to be passed to the factory as CustomerData.</p>
<pre name='code' class='java'>
class LoanRequestForm {
    protected Customer cus;
    protected User usr;
    protected Account acc;
    protected LoanRequestFormAdapter adapter = new LoanRequestFormAdapter();
    protected Loan loanRequest;

    LoanResults getLoanResults() {
        try {
            loanRequest = LoanFactory.getInstance(adapter.transform(this));
            LoanResults requestResults = loanRequest.getLoanResults();
            return requestResults;
        } catch (InvalidDataException ex) {
            log.error(ex);
        }
    }
}
</pre>
<p>Here we are injecting a dependency for the LoanRequestForm and also we acquiring abstraction through the LoanFactory, transforming its input using the LoanRequestFormAdapter to transform the form data into a DTO (Data Transfer Object). The business logic is completely separated from the its User Interface and the abstraction layer. So we are fitting the model with PAC pattern and the IoC DI pattern at once.</p>
<div style='border: 1px solid #000; text-align: center; overflow: scroll;'>
<img src='http://coder.cl/uploads/2010/04/di-pattern-pac-pattern.png' title='Loan Classes' alt='Loan Classes' width='510'>
</div>
<p>The transformation between the form and the factory is a must. The factory can not depend on the form and the form can not depend on the factory. The adapter plays an important role here, since it&#8217;s separating layers and transforming the application into an <i>n-tier architected</i> application, so we have more control over changes and possible enhancements on the model. Also it will bring us possible automated processes, since the factory do not needs the form to operate over the loan. So you build another application that just handles the user data and generate loan bids automatically to the customers based on the same logic, then we are reusing the properly the code.</p>
<p><br/></p>
<h3>conclusion</h3>
<p>We can glue the PAC pattern with IoC DI pattern, so we have more reliable and flexible GUI implementations, then we can work in our platforms without worries about how to generate reusable code if we follow well designed and well applied patterns. Also we can use frameworks, but it depends on the application size and how do need to work on it. For an application with just two active forms, we do not need large scale architectures.</p>



share this article at: 


	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fcoder.cl%2F2010%2F04%2Fusing-dependency-injection-on-gui%2F&amp;title=using%20dependency%20injection%20on%20gui&amp;source=coder+.+cl+system+programmer+%26amp%3B+web+developer&amp;summary=Many%20people%20know%20the%20Model%20View%20Controller%20architectural%20pattern.%20Another%20interesting%20pattern%20is%20Presentation%20Abstraction%20Control%20architectural%20pattern.%20We%20can%20implement%20it%20using%20Dependency%20Injection%20or%20similar%20Inversion%20of%20Control%20patterns.%20So%2C%20when" title="LinkedIn"><img src="http://coder.cl/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fcoder.cl%2F2010%2F04%2Fusing-dependency-injection-on-gui%2F&amp;title=using%20dependency%20injection%20on%20gui&amp;notes=Many%20people%20know%20the%20Model%20View%20Controller%20architectural%20pattern.%20Another%20interesting%20pattern%20is%20Presentation%20Abstraction%20Control%20architectural%20pattern.%20We%20can%20implement%20it%20using%20Dependency%20Injection%20or%20similar%20Inversion%20of%20Control%20patterns.%20So%2C%20when" title="del.icio.us"><img src="http://coder.cl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fcoder.cl%2F2010%2F04%2Fusing-dependency-injection-on-gui%2F&amp;title=using%20dependency%20injection%20on%20gui&amp;bodytext=Many%20people%20know%20the%20Model%20View%20Controller%20architectural%20pattern.%20Another%20interesting%20pattern%20is%20Presentation%20Abstraction%20Control%20architectural%20pattern.%20We%20can%20implement%20it%20using%20Dependency%20Injection%20or%20similar%20Inversion%20of%20Control%20patterns.%20So%2C%20when" title="Digg"><img src="http://coder.cl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fcoder.cl%2F2010%2F04%2Fusing-dependency-injection-on-gui%2F&amp;title=using%20dependency%20injection%20on%20gui" title="Reddit"><img src="http://coder.cl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fcoder.cl%2F2010%2F04%2Fusing-dependency-injection-on-gui%2F&amp;t=using%20dependency%20injection%20on%20gui" title="Facebook"><img src="http://coder.cl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=using%20dependency%20injection%20on%20gui%20-%20http%3A%2F%2Fcoder.cl%2F2010%2F04%2Fusing-dependency-injection-on-gui%2F" title="Twitter"><img src="http://coder.cl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fcoder.cl%2F2010%2F04%2Fusing-dependency-injection-on-gui%2F&amp;title=using%20dependency%20injection%20on%20gui&amp;annotation=Many%20people%20know%20the%20Model%20View%20Controller%20architectural%20pattern.%20Another%20interesting%20pattern%20is%20Presentation%20Abstraction%20Control%20architectural%20pattern.%20We%20can%20implement%20it%20using%20Dependency%20Injection%20or%20similar%20Inversion%20of%20Control%20patterns.%20So%2C%20when" title="Google Bookmarks"><img src="http://coder.cl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>


<br/><br/><br/><hr height="1px" width="50%" />
<div style='text-align: center !important;'><b>Copyright © 2010 Daniel Molina Wegener</b><br/><b>Atribución-No Comercial-Sin Derivadas 2.0 Chile</b><br/><a target='_new' rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/cl/"><img alt="Creative Commons License" style="border-width:0" src="/cc88x31.png" /></a></div>
<br/><hr height="1px" width="100%" />
<p><small>© Daniel Molina Wegener for <a href="http://coder.cl">coder . cl</a>, 2010. | <a href="http://coder.cl/2010/04/using-dependency-injection-on-gui/">Permalink</a> | <a href="http://coder.cl/2010/04/using-dependency-injection-on-gui/#comments">No comment</a><br/>Post tags: <br/></small></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2010/04/using-dependency-injection-on-gui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>source code optimization in c</title>
		<link>http://coder.cl/2009/12/source-code-optimization-in-c/</link>
		<comments>http://coder.cl/2009/12/source-code-optimization-in-c/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 21:45:02 +0000</pubDate>
		<dc:creator>Daniel Molina Wegener</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=589</guid>
		<description><![CDATA[&#34;&#8230;premature optimization is the root of all evil&#34;. &#8212; Donald Knuth I agree with the fact that we must do our source code level optimizations when we have finshed the construction stage or it is almost complete. I was searching articles and papers about optimizing C source code to be applied on my programs and [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
<i>&quot;&#8230;premature optimization is the root of all evil&quot;</i>.<br />
<i style='align: right;'>&mdash; Donald Knuth</i>
</p></blockquote>
<p>I agree with the fact that we must do our source code level optimizations when we have finshed the construction stage or it is almost complete. I was searching articles and papers about optimizing C source code to be applied on my programs and libraries. I&#8217;ve collected some of those optimizations. But you must not confuse algorithm optimization, source code optimization and compiler optimization, since the first one refers to <i>algorithm design</i> and the second one just refers to the <i>algorithm implementation</i>, and both are sharing just few common approaches to formal reductions.</i><span id="more-589"></span></p>
<p>Usually the source code optimization just applies well known formal reductions. We will not treat those formalities in this article. Instead we will take a look on some examples that I&#8217;ve collected, allowing a common reasoning about how to optimize the source code. Most ideas for source code optimizations comes from &eta;-conversions using &lambda; equivalences, allowing for example reductions from <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_992.5_109bdcfacfe30bd05b191cfd2e7c3612.png" style="vertical-align:-7.5px; display: inline-block ;" alt="O(log ~n) ~left~ O(n)" title="O(log ~n) ~left~ O(n)"/>. Also, some optimizations without an apparent &eta;-conversion, since most is done at low level, as those which are taken by the compiler and translated to less assembler instructions, such as function inlining and parameter reduction. Most &eta;-conversions which implies &eta;-reductions, and are called <i>strength reductions</i>. But we have also the opposed side, where we are &mdash; instead of doing reductions &mdash; populating our programs with more lines of code, including repeated code, such as the loop unrolling optimization.</p>
<p><br/></p>
<h3>inline code</h3>
<p>C supports both <i>macros</i> and <i>inlining functions</i>. Both features are reaching the same effect, when the code is compiled, all <i>inlined</i> instructions are placed when we call the macro or the inline function. For example, a typical approach, is a <tt>max(a, b)</tt> function or a <tt>MAX(a, b)</tt> macro.</p>
<pre name="code" class="c">
/* the slowest expression, compiling and running */
int
max(int a, int b)
{
	if (a &gt; b)
		return a;
	else
		return b;
}

/* normal expression with inlining */
inline int
max(int a, int b)
{
	return ((a &gt; b) ? a : b);
}

/* the faster expression using macros */
#define MAX(a, b)			((a &gt; b) ? a : b)
</pre>
<p>The <i>inline</i> and the <i>macro</i> definitions both are similar. The macro places every occurrence of <tt>MAX(var1, var2)</tt> expressions with the <tt>((var1 > var2) ? var1 : var2)</tt> expression. The <i>inline</i> does the same, and places every occurrence of <tt>max(var1, var2)</tt> with the <i>result</i> of the expression <tt>((var1 > var2) ? var1 : var2)</tt>. So, the macro usage is oriented to replace entire blocks of code, instead of inline functions which are oriented to replace calls and skip the assembler <i>call</i> instruction and its derivates. We may look examples of the <i>call</i> opcode in typical system calls on operating systems, such as FreeBSD system calls.</p>
<p><br/></p>
<h3>parameter reduction</h3>
<p>Reducing parameters implies less assembler instructions. C calling convention typically push parameters into the stack, which implies that for each parameter a <i>push</i> opcode call is made. For example you can group function arguments &mdash; if they are use subsequently by a group of functions &mdash; under <tt>struct</tt> forms. Think a little, each <i>push</i> instruction in his family takes 2 clocks, varying to 18 clocks on x86 architecture &mdash; depending on vendor and model. For 8 arguments, it will take from 16 to 36 clocks, against 2 to 18 clocks with one argument. This approach on argument reduction may be applied to those functions which are not inlined.</p>
<pre name='code' class='c'>
/* slower version */
int
my_function(int arg1, int arg2, int arg3, int arg4)
{
	/* do something... */
}

/* faster version */
struct myargs
{
	int arg1; int arg2; int arg3; int arg4;
};

int
my_function(struct myargs *args)
{
	/* do something... */
}
</pre>
<p><br/></p>
<h3>practical reductions</h3>
<p>We do <i>reduction</i> when we remove unnecessary steps in our functions. We can do most reductions just by thinking a little on the code, and also there are some well known reductions which can be used as optimizations.</p>
<h4>removing else</h4>
<pre name='code' class='c'>
/* with else, smaller code, but slower one */
inline int
test(int a)
{
	return a &gt; 0 ? 1 : 0;
}

/* without else, large code but faster one */
inline int
test(int a)
{
	if (a &gt; 0)
		return 1;
	/* implied else */
	return 0;
}
</pre>
<p>On this optimization, we are removing <tt>jmp</tt> and <tt>jxx</tt> family of instructions, where most of them takes near to 7 clocks on x86 architecture and also the required instruction to setup the proper context for the following instruction in the program sequence. This is like the <i>spartan programming</i> style, where most code is minimized through reductions and similar programming tasks.</p>
<h4>bitwise operations</h4>
<p>Bitwise operations are cheaper than other operations. For example, curiously, a shift operation and plus operations have less clocks than multiplication and division operations. The happens to logical evaluations. In other post, I&#8217;ve wrote about <a href='http://coder.cl/2008/12/29/minimization-patterns/'>logical minimizations</a>. A complement to what I&#8217;ve said, is the fact that <tt>mul</tt> instruction takes from 10 to 40 clocks and <i>div</i> instruction takes from 15 to 40 clocks on x86 architecture, against <tt>add</tt>, <tt>sub</tt>, <tt>shr</tt>, <tt>shl</tt> and similar instruction that are taking from 2 to 7 clocks. Remember that the number clocks used by some instruction is vendor and model dependent. For example if we do:</p>
<pre name='code' class='c'>
/* we take a call to the math.h function pow() */
n = y * pow(2.0, x);

/* we can replace it by */
n = y &lt;&lt; x;
</pre>
<p>In other words, the <tt>x &lt;&lt; y</tt> operation is equivalent to <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_994.5_975cf26e16a8e396b3e831896513a495.png" style="vertical-align:-5.5px; display: inline-block ;" alt="y * 2 ^ x" title="y * 2 ^ x"/>. You can find other math equivalences too. From this basic approach, we can reach other types of optimizations through the proper maths. You can take a look on the <a href='http://www.jjj.de/bitwizardry/bitwizardrypage.html'>bit wizardry</a> page from <i>Jörg Arndt</i> if you want to seek for more bitwise operations and reductions.</p>
<h4>reversing counter loops</h4>
<p>Usually we do one step loops for fixed set of counters. The following example shows a reduction which can be used every time we can do a reverse loop.</p>
<pre name='code' class='c'>
/* for loop coded usually */
for (c = 0; c &lt; MAX_C_VALUE; c++) {
	/* do something... */
}

/* for reversed loop */
for (c = MAX_C_VALUE; c--; /* we do not do nothing here */ ) {
	/* do something... */
}
</pre>
<h4>table lookups</h4>
<p>A table lookup is the technique where we create an array and kind related structures to lookup for data, usually previously calculated data or simply, we are looking for concrete data that we want to use.</p>
<pre name='code' class='c'>
/* we can have a switch statement */
ourtype_t *varn = NULL;
switch (var1) {
case 0:
	varn = value1; break;
case 1:
	varn = value2; break;
case 2:
	varn = value3; break;
default:
	varn = value1;
}

/* or have an if/else statement */
if (var1 == 0)
	varn = value1;
else if (var1 == 1)
	varn = value2;
else if (var2 == 2)
	varn = value3;
else
	varn = value1;

/* so we can simply replace those values using arrays */
ourtype_t mapsvalues[] = { value1, value2, value3 };
varn = mapsvalues[var1];
</pre>
<p>Also we usually can setup reductions by creating <i>table lookups</i> and <i>state machines</i>, so we can create the proper map between certain variable data and certain <i>function</i>. State machines are a powerful abstraction which allows us to code different states inside data structures.</p>
<p>We can have predefined values in our table lookup tasks. Then we are using <a href='http://en.wikipedia.org/wiki/Lazy_evaluation'>lazy evaluation</a>. Every time we have a constant value which is requested and not calculated each time we work with it, we are doing lazy evaluation.</p>
<h4>register keyword</h4>
<pre name='code' class='c'>
/* using the register keyword should help creating faster code */
register int counter;
</pre>
<p><br/></p>
<h3>reduce data access computation</h3>
<p>If we have deep constructed data structures (<tt>struct</tt> in C), every time we access most deep nodes, we are using <i>pointer arithmetics</i>, which implies basic math operations to access certain parts of our structures. Here we can do tow tasks: <i>alias creation and usage</i> and <i>padding adjustment</i>. Alias usage, means that we must use an internal pointer to access directly a structure member, so we omit pointer calculation each time we access it. Padding adjustment, is just about to create the proper data structure member order.</p>
<pre name='code' class='c'>
/* structure without the proper padding */
struct my_struct {
	char *a;
	void *b;
	int c;
	double n;
	char *x;
};

/* the same structure with the proper padding */
struct my_struct {
	double n;
	int c;
	char *a;
	char *x;
	void *b;
};
</pre>
<p>Also the data access computation is reduced on assembler level code, not the C code. Here we have an implied &eta;-reduction and invisible one. For deeply and nested data structures, we have the same issue. We should have as rule that the structure size must have an ideal size of <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_994.5_2d815e98c648cc714c03be5dad8ec434.png" style="vertical-align:-5.5px; display: inline-block ;" alt="~s = ~n^2" title="~s = ~n^2"/>, with <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_995_6ded80df32818a359c9e6e317b4a741b.png" style="vertical-align:-5px; display: inline-block ;" alt="~s" title="~s"/> as the structure size and <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_995_7f9468a4201f228dc27a50d25c44c696.png" style="vertical-align:-5px; display: inline-block ;" alt="~n" title="~n"/> as the padding adjustemnt to power of two.</p>
<pre name='code' class='c'>
/* some structs with nested members */
struct a {
	int m1;
};

struct b {
	int m2;
	struct a *m3;
}

struct c {
	int m4;
	struct b *m5;
}

/* the non cheaper version to access its members */
struct c *x = some_function_returning_c();
x-&gt;m5-&gt;m3-&gt;m1 = some_other_function();
if (x-&gt;m5-&gt;m3-&gt;m1 != 0) {
	another_function(x-&gt;m5-&gt;m3-&gt;m1);
}

/* the cheaper version to access its members
   using aliases */
struct c *x = some_function_returning_c();
struct b *p;
b = x-&gt;m5-&gt;m3;
b-&gt;m1 = some_other_function();
if (b-&gt;m1 != 0) {
	another_function(b-&gt;m1);
}
</pre>
<p><br/></p>
<h3>loop unrolling</h3>
<p>Each step on a loop repeats some instructions. For example if we have a fixed size array, where we must treat each element with certain function, we can use unrolled loops.</p>
<pre name='code' class='c'>
/* normal iteration over fixed size array */
for (i = 0; i &lt; 100; i++) {
	call_some_function(my_array[i]);
}

/* applied loop unrolling and reverse loop */
for (i = 100; i--;) {
	call_some_function(my_array[i]);
	call_some_function(my_array[--i]);
	call_some_function(my_array[--i]);
	call_some_function(my_array[--i]);
	call_some_function(my_array[--i]);
}
</pre>
<p>Note that here we have a fixed size array. In other case is hard to know the array size. Also we can use our compiler optimization to unroll each loop, if our compiler has the proper option. For example <i>GCC</i> supports automatic loop unrolling by using the <i>-funroll-loops</i> flag.</p>
<p><br/></p>
<h3>loop jamming</h3>
<p>Reusing blocks of code inside loops matters.</p>
<pre name='code' class='c'>
/* here we have two loops for something that can
   be done one loop */
for (i = 0; i &lt; 100; i++) {
	some_function_a(my_array[i]);
}
for (i = 0; i &lt; 100; i += 10) {
	some_function_b(my_array[i]);
}

/* here we have two loops for something that can
   be done one loop */
for (i = 0; i &lt; 100; i++) {
	some_function_a(my_array[i]);
	if ((i % 10) == 0) {
		some_function_b(my_array[i]);
	}
}
</pre>
<p>On the example above we have the proper loop doing the same tasks with less operations, so we have reduced the steps of that loop from <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_992.5_7b1dc71d185484895c3d041d16d3e12c.png" style="vertical-align:-7.5px; display: inline-block ;" alt="O(2n)" title="O(2n)"/> to <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_992.5_125ef5dbfb16f87edc57260c2f532793.png" style="vertical-align:-7.5px; display: inline-block ;" alt="O(n)" title="O(n)"/> &mdash; note that <img src="http://coder.cl/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_992.5_7b1dc71d185484895c3d041d16d3e12c.png" style="vertical-align:-7.5px; display: inline-block ;" alt="O(2n)" title="O(2n)"/> is just symbolic and not strict.</p>
<p><br/></p>
<h3>bit padding matters</h3>
<p>It depends on the architecture. Processing data types shorter or larger than the register size do not have a cheaper cost than using them. For example if we use <tt>char</tt> or <tt>short</tt>, we are not using a complete register, which is more hard to handle than register length variables, such as <tt>int</tt> and <tt>long</tt>.</p>
<p><br/></p>
<h3>references</h3>
<ul>
<li>Michael E. Lee, <a href='http://leto.net/docs/C-optimization.php'>&quot;Optimization of Computer Programs in C&quot;</a>, Ontek Corporation.</li>
<li>Adrian Barnetts, <a href='http://www.abarnett.demon.co.uk/tutorial.html'>&quot;C optimisation tutorial&quot;</a>.</li>
<li>Paul Hsieh, <a href='http://www.azillionmonkeys.com/qed/optimize.html'>&quot;Programming Optimization&quot;</a>.</li>
<li>Koushik Ghosh, <a href='http://www.codeproject.com/KB/cpp/C___Code_Optimization.aspx'>Writing Efficient C and C Code Optimization</a>, The Code Project.</li>
<li><a href='http://www.eventhelix.com/realtimemantra/basics/optimizingcandcppcode.htm'>Optimizing C and C++ Code</a>. EventHelix.</li>
<li>Cris H. Pappas and William H. Murray, <i>&quot;386 Microprocessor Handbook</a>&quot;</i>, McGraw Hill.</li>
</ul>



share this article at: 


	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fsource-code-optimization-in-c%2F&amp;title=source%20code%20optimization%20in%20c&amp;source=coder+.+cl+system+programmer+%26amp%3B+web+developer&amp;summary=%0D%0A%26quot%3B...premature%20optimization%20is%20the%20root%20of%20all%20evil%26quot%3B.%0D%0A%26mdash%3B%20Donald%20Knuth%0D%0A%0D%0A%0D%0AI%20agree%20with%20the%20fact%20that%20we%20must%20do%20our%20source%20code%20level%20optimizations%20when%20we%20have%20finshed%20the%20construction%20stage%20or%20it%20is%20almost%20complete.%20I%20was%20searchin" title="LinkedIn"><img src="http://coder.cl/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fsource-code-optimization-in-c%2F&amp;title=source%20code%20optimization%20in%20c&amp;notes=%0D%0A%26quot%3B...premature%20optimization%20is%20the%20root%20of%20all%20evil%26quot%3B.%0D%0A%26mdash%3B%20Donald%20Knuth%0D%0A%0D%0A%0D%0AI%20agree%20with%20the%20fact%20that%20we%20must%20do%20our%20source%20code%20level%20optimizations%20when%20we%20have%20finshed%20the%20construction%20stage%20or%20it%20is%20almost%20complete.%20I%20was%20searchin" title="del.icio.us"><img src="http://coder.cl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fsource-code-optimization-in-c%2F&amp;title=source%20code%20optimization%20in%20c&amp;bodytext=%0D%0A%26quot%3B...premature%20optimization%20is%20the%20root%20of%20all%20evil%26quot%3B.%0D%0A%26mdash%3B%20Donald%20Knuth%0D%0A%0D%0A%0D%0AI%20agree%20with%20the%20fact%20that%20we%20must%20do%20our%20source%20code%20level%20optimizations%20when%20we%20have%20finshed%20the%20construction%20stage%20or%20it%20is%20almost%20complete.%20I%20was%20searchin" title="Digg"><img src="http://coder.cl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fsource-code-optimization-in-c%2F&amp;title=source%20code%20optimization%20in%20c" title="Reddit"><img src="http://coder.cl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fsource-code-optimization-in-c%2F&amp;t=source%20code%20optimization%20in%20c" title="Facebook"><img src="http://coder.cl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=source%20code%20optimization%20in%20c%20-%20http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fsource-code-optimization-in-c%2F" title="Twitter"><img src="http://coder.cl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fsource-code-optimization-in-c%2F&amp;title=source%20code%20optimization%20in%20c&amp;annotation=%0D%0A%26quot%3B...premature%20optimization%20is%20the%20root%20of%20all%20evil%26quot%3B.%0D%0A%26mdash%3B%20Donald%20Knuth%0D%0A%0D%0A%0D%0AI%20agree%20with%20the%20fact%20that%20we%20must%20do%20our%20source%20code%20level%20optimizations%20when%20we%20have%20finshed%20the%20construction%20stage%20or%20it%20is%20almost%20complete.%20I%20was%20searchin" title="Google Bookmarks"><img src="http://coder.cl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>


<br/><br/><br/><hr height="1px" width="50%" />
<div style='text-align: center !important;'><b>Copyright © 2009 Daniel Molina Wegener</b><br/><b>Atribución-No Comercial-Sin Derivadas 2.0 Chile</b><br/><a target='_new' rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/cl/"><img alt="Creative Commons License" style="border-width:0" src="/cc88x31.png" /></a></div>
<br/><hr height="1px" width="100%" />
<p><small>© Daniel Molina Wegener for <a href="http://coder.cl">coder . cl</a>, 2009. | <a href="http://coder.cl/2009/12/source-code-optimization-in-c/">Permalink</a> | <a href="http://coder.cl/2009/12/source-code-optimization-in-c/#comments">One comment</a><br/>Post tags: <br/></small></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2009/12/source-code-optimization-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>java mutexes</title>
		<link>http://coder.cl/2009/12/java-mutexes/</link>
		<comments>http://coder.cl/2009/12/java-mutexes/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 11:25:27 +0000</pubDate>
		<dc:creator>Daniel Molina Wegener</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=548</guid>
		<description><![CDATA[Java synchronization is usually made through the synchronized keyword. Synchronized allow users to create a mutex around certain variable, class or method, where a mutex allows concurrent access to it. By concurrent access we should understand access from multiple threads. If an operation is atomic, there one and only one process or thread executing it. [...]]]></description>
			<content:encoded><![CDATA[<p>Java synchronization is usually made through the <i>synchronized</i> keyword. Synchronized allow users to create a mutex around certain variable, class or method, where a mutex allows concurrent access to it. By concurrent access we should understand <i>access from multiple threads</i>. If an operation is <i>atomic</i>, there one and only one process or thread executing it. Then, mutex based operations are atomic.</p>
<p><span id="more-548"></span></p>
<p>In Java, thread synchronization made using the <i>synchronized</i> keyword can create a heavy &mdash; with a high number of threads &mdash; overhead in two main cases, the first one is when it is used on classes and the second one when it is used in methods. Then we must think a way to create small locks, mainly if we are using design patterns like <i>singletons</i>.</p>
<pre name="code" class="java">
/* BAD CASE 1, throws NullPointerException */
class BadMutexBaseClass {
	private static BadMutexBaseClass singletonInstance;
	public static BadMutexBaseClass getInstance(void) {
		synchronized (singletonInstance) {
			if (singletonInstance == null) {
				singletonInstance = new BadMutexBaseClass();
			}
			return singletonInstance;
		}
	}
}
</pre>
<pre name="code" class="java">
/* BAD CASE 2, heavy overhead */
class BadMutexBaseClass {
	private static BadMutexBaseClass singletonInstance;
	public synchronized static BadMutexBaseClass getInstance(void) {
		if (singletonInstance == null) {
			singletonInstance = new BadMutexBaseClass();
		}
		return singletonInstance;
	}
}
</pre>
<p>Both cases are wrong, we must use mutex object, without touching the singleton member and allowing the program to access the singleton atomically. We can use a basic <i>Object</i> instance to create the proper mutex.</p>
<pre name="code" class="java">
/* MUTEX BASED CASE */
class MutexBaseClass {
	private static Object mutex = new Object();
	private static MutexBaseClass singletonInstance;
	public static MutexBaseClass getInstance(void) {
		synchronized (mutex) { /* critical section */
			if (singletonInstance == null) {
				singletonInstance = new MutexBaseClass();
			}
			return singletonInstance;
		}
	}
}
</pre>
<p>By creating a <i>dummy</i> object, we can use it as mutex in our program. With a more low level perspective, this technique is similar to the one used with <a href="http://www.opengroup.org/onlinepubs/000095399/functions/pthread_mutex_lock.html">pthread_mutex_lock(P)</a> techniques while we are programming with threads in <b>C</b>. An interesting point is the fact that <i>non-blocking algorithms</i> are faster than locking ones. The mutex technique is a blocking technique. Non-blocking techniques, such as lock-free and obstruction-free, can allow the user to implement faster &mdash; but more complex &mdash; code.</p>
<p><b>GCC</b> has <a href="http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html">built-in atomic</a> operations to allow the creation of such code, like <i>CAS</i> operations, allowing the use of <i>lock-free</i> techniques. For example in the code bellow &mdash; which is using the __sync_bool_compare_and_swap atomic operation &mdash; we can see that the  <i>CAS</i> or <i>Compare and Swap</i> operation is made around the number of transactions with <i>old value</i> and <i>old value plus one</i>.</p>
<pre name="code" class="c">
do {
	old = vproc_shmem->vp_shmem_transaction_cnt;

	if (unlikely(old &lt; 0)) {
		if (vproc_shmem->vp_shmem_flags &amp; VPROC_SHMEM_EXITING) {
			_exit(0);
		} else {
			__crashreporter_info__ = &quot;Unbalanced: vproc_transaction_begin()&quot;;
		}
		abort();
	}
} while( !__sync_bool_compare_and_swap(&amp;vproc_shmem->vp_shmem_transaction_cnt, old, old + 1) );
</pre>
<p>I hope that Java will include kind that <i>atomic operations</i> to allow the use of non-blocking algorithms and more faster code and since I agree with the fact that we are entering the multi-core era&#8230;</p>



share this article at: 


	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fjava-mutexes%2F&amp;title=java%20mutexes&amp;source=coder+.+cl+system+programmer+%26amp%3B+web+developer&amp;summary=Java%20synchronization%20is%20usually%20made%20through%20the%20synchronized%20keyword.%20Synchronized%20allow%20users%20to%20create%20a%20mutex%20around%20certain%20variable%2C%20class%20or%20method%2C%20where%20a%20mutex%20allows%20concurrent%20access%20to%20it.%20By%20concurrent%20access%20we%20should%20understand%20access" title="LinkedIn"><img src="http://coder.cl/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fjava-mutexes%2F&amp;title=java%20mutexes&amp;notes=Java%20synchronization%20is%20usually%20made%20through%20the%20synchronized%20keyword.%20Synchronized%20allow%20users%20to%20create%20a%20mutex%20around%20certain%20variable%2C%20class%20or%20method%2C%20where%20a%20mutex%20allows%20concurrent%20access%20to%20it.%20By%20concurrent%20access%20we%20should%20understand%20access" title="del.icio.us"><img src="http://coder.cl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fjava-mutexes%2F&amp;title=java%20mutexes&amp;bodytext=Java%20synchronization%20is%20usually%20made%20through%20the%20synchronized%20keyword.%20Synchronized%20allow%20users%20to%20create%20a%20mutex%20around%20certain%20variable%2C%20class%20or%20method%2C%20where%20a%20mutex%20allows%20concurrent%20access%20to%20it.%20By%20concurrent%20access%20we%20should%20understand%20access" title="Digg"><img src="http://coder.cl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fjava-mutexes%2F&amp;title=java%20mutexes" title="Reddit"><img src="http://coder.cl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fjava-mutexes%2F&amp;t=java%20mutexes" title="Facebook"><img src="http://coder.cl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=java%20mutexes%20-%20http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fjava-mutexes%2F" title="Twitter"><img src="http://coder.cl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fcoder.cl%2F2009%2F12%2Fjava-mutexes%2F&amp;title=java%20mutexes&amp;annotation=Java%20synchronization%20is%20usually%20made%20through%20the%20synchronized%20keyword.%20Synchronized%20allow%20users%20to%20create%20a%20mutex%20around%20certain%20variable%2C%20class%20or%20method%2C%20where%20a%20mutex%20allows%20concurrent%20access%20to%20it.%20By%20concurrent%20access%20we%20should%20understand%20access" title="Google Bookmarks"><img src="http://coder.cl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>


<br/><br/><br/><hr height="1px" width="50%" />
<div style='text-align: center !important;'><b>Copyright © 2009 Daniel Molina Wegener</b><br/><b>Atribución-No Comercial-Sin Derivadas 2.0 Chile</b><br/><a target='_new' rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/cl/"><img alt="Creative Commons License" style="border-width:0" src="/cc88x31.png" /></a></div>
<br/><hr height="1px" width="100%" />
<p><small>© Daniel Molina Wegener for <a href="http://coder.cl">coder . cl</a>, 2009. | <a href="http://coder.cl/2009/12/java-mutexes/">Permalink</a> | <a href="http://coder.cl/2009/12/java-mutexes/#comments">No comment</a><br/>Post tags: <br/></small></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2009/12/java-mutexes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
