<?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>web developer &#38; system programmer</description>
	<lastBuildDate>Tue, 15 May 2012 10:46:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>practice makes perfect</title>
		<link>http://coder.cl/2011/08/practice-makes-perfect/</link>
		<comments>http://coder.cl/2011/08/practice-makes-perfect/#comments</comments>
		<pubDate>Sun, 28 Aug 2011 13:37:02 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1775</guid>
		<description><![CDATA[As developer, someone that is passionate for his work, will tend to learn new stuff without being directed to do that. Also, a passionate developer probably will develop a software solution using its own initiative, publishing a Free/Open Source Software solution or simply a Proprietary Product that will be available for download on its site [...]]]></description>
			<content:encoded><![CDATA[<p>As developer, someone that is passionate for his work, will tend to learn new stuff without being directed to do that. Also, a passionate developer probably will develop a software solution using its own initiative, publishing a Free/Open Source Software solution or simply a Proprietary Product that will be available for download on its site or something similar to Android Market. <i>&ldquo;Practice Makes Perfect&rdquo;</i>, it can lead a developer from being a junior developer to a senior developer in a short period. The more interest they have, the more they get induced to build better code and better solutions, increasing his knowledge and leaving behind common programming mistakes. Usually an interested developer visits programming forums and mailing lists, has a Blog discussing programming topics &mdash; which indicates that he can write and communicate better than average developers &mdash; and has better initiatives using its creativity to solve problems.</p>
<p><span id="more-1775"></span></p>
<p>Programming does not have patterns, so you cannot learn programming using how-to guides or similar stuff. The highest level of abstraction that programming has is an <i>algorithm</i>, which can have very varying representations in real source code, so you cannot memorize algorithms in hard coded languages, and require that you must understand <i>concepts</i> instead of learning certain language approach. Usually a polyglot programmer will try to implement the same algorithm in different languages or similar languages to see how different are they and how he can handle the same problem using different approaches in the real world. A non polyglot programmer will tend to use algorithm variations to produce the same solution with different perspectives.</p>
<blockquote><p>
Practice is the act of rehearsing a behaviour over and over, or engaging in an activity again and again, for the purpose of improving or mastering it, as in the phrase &ldquo;practice makes perfect&rdquo;. Sports teams practice to prepare for actual games. Playing a musical instrument well takes a lot of practice. It is a method of learning and of acquiring experience. [<a href='http://en.wikipedia.org/wiki/Practice_(learning_method)' title=''>Practice (learning method), Wikipedia</a>]
</p></blockquote>
<p>The most common approach to practice is learning a new language. As I wrote in past posts, you can take a book to learn a language, but you must consider three types of books: <i>tutorials</i>, <i>user manuals</i> and <i>reference manuals</i>. The first one that you must handle on each new language that you learn is a tutorial, it will guide you in the very basic features that the language will have, the you should take the user manual and finally you must use the reference manual to see which components are common to its standard library. Also, those tutorials which are fulfilled with exercises are very useful to learn, since they require that you start practising while you are learning. Also you can find programming problems &mdash; which require from you a higher level of thinking &mdash; by finding those problems exposed in programming contests. Try to acquire some books related to algorithms, learning from them and then practising your programming capabilities by solving those problems exposed in programming contests.</p>
<p>I know that the number of programmers that care about practising his programming capabilities are lower than the average developer, so you as employer must take care of those programmers that really care about programming as a professional activity. You will not find them easily. Where those programmers &mdash; usually called passionate programmers &mdash; care about programming as an interesting activity, instead of those employees that only take care about the position that they are occupying in the organization. Keep practising, you will be a better programmer.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/08/practice-makes-perfect/">Permalink</a> |
<a href="http://coder.cl/2011/08/practice-makes-perfect/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/08/practice-makes-perfect/&title=practice makes perfect">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2011/08/practice-makes-perfect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>understanding oauth</title>
		<link>http://coder.cl/2011/04/understanding-oauth/</link>
		<comments>http://coder.cl/2011/04/understanding-oauth/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 01:47:53 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1432</guid>
		<description><![CDATA[&#8220;An open protocol to allow secure API authorization in a simple and standard method from desktop and web applications&#8221;. The basis of OAuth protocol, is to allow applications to share its user resources or data among third party applications without sharing the user credentials. It is done using authentication tokens, allowing certain applications to expose [...]]]></description>
			<content:encoded><![CDATA[<p><i>&ldquo;An open protocol to allow secure API authorization in a simple and standard method from desktop and web applications&rdquo;</i>. The basis of OAuth protocol, is to allow applications to share its user resources or data among third party applications without sharing the user credentials. It is done using authentication tokens, allowing certain applications to expose its API to other desktop applications and web based applications. The protocol was created with the purpose to solve the problem of enabling delegated access to protected resources.</i><span id="more-1432"></span></p>
<p>The basic behavior of this Authentication Protocol, is made through HTTPS requests. Initial, the <i>consumer</i> requests a temporary token &mdash; for example gravatar.com accessing your facebook.com account data is a consumer &mdash;, called <i>request token</i>. With the request token, the oauth_callback parameter is sent, which holds the callback URL that will be triggered once the <i>user</i> or <i>resource owner</i> authorizes the <i>consumer</i> to access its </i>resources</i>.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/oauth-user.png?v=1.2' title='OAuth User'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/oauth-user.png?v=1.2' border='0' alt='OAuth User' /></a></p>
<div>OAuth User</div>
</div>
<p>The Consumer never has real user credentials, it only has temporary Authentication Tokens to be used to access the User or Resource Owner resources.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/oauth-consumer.png?v=1.2' title='OAuth Consumer'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/oauth-consumer.png?v=1.2' border='0' alt='OAuth Consumer' /></a></p>
<div>OAuth Consumer</div>
</div>
<p>A classic example, is Twitter allowed third party applications. To take a look on which applications are accessing to your Twitter account, you must go to the <i>Settings</i> section, and click on the <i>Connections</i> panel. You will see some applications like Twitpic, Gravatar and Twitcam.com.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/oauth-behavior.png?v=1.2' title='OAuth Behavior'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/oauth-behavior.png?v=1.2' border='0' alt='OAuth Behavior' /></a></p>
<div>OAuth Behavior</div>
</div>
<p>The first step, is to build and send the Request Token from the Consumer to the Service Provider. This is made using a simple HTTPS request with the OAuth authentication mechanism enabled on the application.</p>
<pre>
 POST /initiate HTTP/1.1
 Host: some.service.com
 Authorization: OAuth realm="SomeService",
    oauth_consumer_key="dpf43f3p2l4k3l03",
    oauth_signature_method="HMAC-SHA1",
    oauth_timestamp="137131200",
    oauth_nonce="wIjqoS",
    oauth_callback="http%3A%2F%2Fsome.service.com%2Fready",
    oauth_signature="74KNZJeDHnMBp0EMJ9ZHt%2FXKycU%3D"
</pre>
<p>The initial parameters on the Request Token are the consumer key, an unique identifier of the consumer, that should be stored on the Service Provider application to identify its authorized consumers, for example Twitpic.com on Twitter has its own consumer key. The signature method should be <tt>HMAC-SHA1</tt> [ref. <a href='http://tools.ietf.org/html/rfc2104'>RFC2104</a>] or <tt>RSA-SHA1</tt> [ref. <a href='http://tools.ietf.org/html/rfc3447#section-8.2'>RFC3447, Sec 8.2</a>], where the first one uses a simple cryptographic hash algorithm and second one requires the PKI RSA standard. You can use <tt>PLAINTEXT</tt>, but it is not recommended and not commonly recognized. The timestamp holds the request timestamp &mdash; on those timestamp friendly days where here in Chile we were playing with timezones the wrong timestamp was generating some problems on certain applications. The <tt>oauth_nonce</tt> is:</p>
<blockquote><p>A nonce is a random string, uniquely generated by the client to allow the server to verify that a request has never been made before and helps prevent replay attacks when requests are made over a non-secure channel.</p></blockquote>
<p>The <tt>oauth_callback</tt> argument is the callback URL that will be triggered once the user authorizes the consumer to access its resources, and the <tt>oauth_signature</tt> is the algorithm described by the <tt>signature_method</tt> argument applied to the URL base string, which is made concatenating the request method (or HTTP verb) and the full URL encoded with all OAuth arguments, except the <tt>oauth_signature</tt> argument.</p>
<p>Once the Request Token HTTPS request is made, the server returns the oauth_token, oauth_token_secret and oauth_callback_confirmed parameters to the consumer with the form of <i>application/x-www-form-urlencoded</i> content type. Where the consumer must handle those arguments and build the local object containing the OAuth Request Token, then with that token, the consumer request the Authorization URL on the Service Provider endpoint, and it should be triggered by the User &mdash; from a standard browser or his desktop application &mdash; and then the User or Resource Owner <i>authorizes</i> the consumer application to use its resources.</p>
<p>Once the Consumer is authorized by the User or Resource Owner, is triggered the Consumer provided callback URL on the <tt>oauth_callback</tt> argument, but it is triggered from the Service Provider application with the <tt>oauth_token</tt> and the <tt>oauth_verifier</tt> to construct the subsequent Resource Requests.</p>
<p>This protocol is very simple and useful, and it is having a wide adoption between a lot of web sites. It is not an official standard yet, but is becoming a new authentication standard for many web based applications.</p>
<p><br/></p>
<h3>references</h3>
<ul>
<li><a href='http://tools.ietf.org/html/rfc5849'>The OAuth 1.0 Protocol</a></li>
</ul>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/04/understanding-oauth/">Permalink</a> |
<a href="http://coder.cl/2011/04/understanding-oauth/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/04/understanding-oauth/&title=understanding oauth">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2011/04/understanding-oauth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>dmw</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 class='brush: 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 class="figure">
<img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/04/loan-classes.png' title='Loan Classes' alt='Loan Classes' width='510' style='border: 2px solid #000;'>
</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 class='brush: 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 class='brush: 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 class="figure">
<img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/04/di-pattern-pac-pattern.png' title='Loan Classes' alt='Loan Classes' width='510' style='border: 2px solid #000;'>
</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>
<hr />
<p><small>© dmw 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> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2010/04/using-dependency-injection-on-gui/&title=using dependency injection on gui">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</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>dmw</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 [pmath size=8]O(log ~n) ~left~ O(n)[/pmath]. 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 [pmath size=8]y * 2 ^ x[/pmath]. 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 [pmath size=8]~s = ~n^2[/pmath], with [pmath size=8]~s[/pmath] as the structure size and [pmath size=8]~n[/pmath] 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 [pmath size=8]O(2n)[/pmath] to [pmath size=8]O(n)[/pmath] &mdash; note that [pmath size=8]O(2n)[/pmath] 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>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>
<hr />
<p><small>© dmw 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">2 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2009/12/source-code-optimization-in-c/&title=source code optimization in c">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2009/12/source-code-optimization-in-c/feed/</wfw:commentRss>
		<slash:comments>2</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>dmw</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 class="brush: 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 class="brush: 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 class="brush: 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>
<hr />
<p><small>© dmw 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> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2009/12/java-mutexes/&title=java mutexes">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2009/12/java-mutexes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for ( 2e96b290f6c5ccd2bc6830557c0aba6e coder.cl/category/programming/algorithms/feed/ ) in 0.39667 seconds, on May 16th, 2012 at 2:21 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 23rd, 2012 at 2:21 am UTC -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Quick Cache Is Fully Functional :-) ... A Quick Cache file was just served for ( 2e96b290f6c5ccd2bc6830557c0aba6e coder.cl/category/programming/algorithms/feed/ ) in 0.00055 seconds, on May 18th, 2012 at 11:55 am UTC. -->
