<?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>Nullified Construction</title>
	<atom:link href="http://blog.aki-null.net/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.aki-null.net</link>
	<description></description>
	<lastBuildDate>Tue, 19 Jan 2010 08:03:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>RegexKit on Snow Leopard</title>
		<link>http://blog.aki-null.net/?p=66</link>
		<comments>http://blog.aki-null.net/?p=66#comments</comments>
		<pubDate>Tue, 19 Jan 2010 06:30:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://blog.aki-null.net/?p=66</guid>
		<description><![CDATA[I have been using RegexKit for the Twitter client called YoruFukurou for quite some time now. The framework was essential for the application, because the primary functionality of the app heavily involved the regular expression. I have tried other libraries, but only RegexKit satisfied my requirements.
RegexKit seemed to perform just fine on Snow Leopard, but [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using RegexKit for the Twitter client called <a href="http://sites.google.com/site/yorufukurou/">YoruFukurou</a> for quite some time now. The framework was essential for the application, because the primary functionality of the app heavily involved the regular expression. I have tried other libraries, but only RegexKit satisfied my requirements.</p>
<p>RegexKit seemed to perform just fine on Snow Leopard, but I found a significant bug. The RegexKit was advertised to support Garbage Collection introduced in Leopard, but the application using the library seemed to crash as soon as RegexKit API was called on Snow Leopard. After some research, I found out that only one line of code was required to be changed to fix the issue. The bug was being tracked on their <a href="http://sourceforge.net/tracker/?func=detail&#038;aid=2876858&#038;group_id=204582&#038;atid=990188">official bug tracker</a>, but the code maintainer seemed to be inactive. I have tried to build the framework after applying the fix, but the build failed miserably on Snow Leopard.</p>
<p>Unfortunately, I had to install Leopard on my external HDD to build the framework, because I had no computers running Leopard. You can download the fixed RegexKit framework from <a href='http://blog.aki-null.net/wp-content/uploads/2010/01/RegexKit.zip'>here</a>, so nobody needs to go through the pain of setting up Leopard system to fix the bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.aki-null.net/?feed=rss2&amp;p=66</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grand Central Dispatch (Part 2)</title>
		<link>http://blog.aki-null.net/?p=46</link>
		<comments>http://blog.aki-null.net/?p=46#comments</comments>
		<pubDate>Wed, 14 Oct 2009 04:40:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[GCD]]></category>

		<guid isPermaLink="false">http://blog.aki-null.net/?p=46</guid>
		<description><![CDATA[The Grand Central Dispatch (GCD) can be used to optimise programs for multi-core processors. However, the usual issue with threading still exists in GCD (GCD is not a magic). I would like to cover how to use semaphore to make a program thread-safe.
The most common issues you may face when you introduce threading into your [...]]]></description>
			<content:encoded><![CDATA[<p>The Grand Central Dispatch (GCD) can be used to optimise programs for multi-core processors. However, the usual issue with threading still exists in GCD (GCD is not a magic). I would like to cover how to use semaphore to make a program thread-safe.</p>
<p>The most common issues you may face when you introduce threading into your program are accesses to shared resources. An example of the issue is presented in the code below.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #400080;">NSMutableSet</span> <span style="color: #002200;">*</span>items <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> array<span style="color: #002200;">&#93;</span>;
&nbsp;
   dispatch_queue_t queue <span style="color: #002200;">=</span>
      dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
   dispatch_apply<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">50</span>, queue,
         <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> index<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">500</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
               <span style="color: #002200;">&#91;</span>items addObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hi&quot;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span>
         <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
   NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%i&quot;</span>, <span style="color: #002200;">&#91;</span>items count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
   <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
   <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The program above simply tries to add an item to the shared resource (NSMutableSet) from multiple threads. If you try to run this program, it will probably crash. It is because NSMutableSet is NOT thread-safe (actually, all collection classes are not thread-safe). To prevent this issue, we could use the traditional <em>@synchronized</em> block to ensure thread-safety (example below).</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #400080;">NSMutableSet</span> <span style="color: #002200;">*</span>items <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> array<span style="color: #002200;">&#93;</span>;
&nbsp;
   dispatch_queue_t queue <span style="color: #002200;">=</span>
      dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
   dispatch_apply<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">50</span>, queue,
         <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> index<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">500</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
               <span style="color: #a61390;">@synchronized</span> <span style="color: #002200;">&#40;</span>items<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                  <span style="color: #002200;">&#91;</span>items addObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hi&quot;</span><span style="color: #002200;">&#93;</span>;
               <span style="color: #002200;">&#125;</span>
            <span style="color: #002200;">&#125;</span>
         <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
   NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%i&quot;</span>, <span style="color: #002200;">&#91;</span>items count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
   <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
   <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Well, it runs. But you must remember that locks are very expensive. In the case of the program above, it will lock 500 times per block, which is extremely inefficient. Apple has provided optimised version of semaphore for GCD (dispatch semaphore). The traditional semaphores always require calling down to the kernel to test the semaphore, but the dispatch semaphore tests semaphore in user space, and only traps into the kernel only when the test fails and needs to block the thread. The following code demonstrates the usage of the dispatch semaphore.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #400080;">NSMutableSet</span> <span style="color: #002200;">*</span>items <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> array<span style="color: #002200;">&#93;</span>;
&nbsp;
   dispatch_semaphore_t itemLock <span style="color: #002200;">=</span> dispatch_semaphore_create<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
   dispatch_queue_t queue <span style="color: #002200;">=</span>
      dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
   dispatch_apply<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">50</span>, queue,
         <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> index<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">500</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
               dispatch_semaphore_wait<span style="color: #002200;">&#40;</span>itemLock, DISPATCH_TIME_FOREVER<span style="color: #002200;">&#41;</span>;
               <span style="color: #002200;">&#91;</span>items addObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hi&quot;</span><span style="color: #002200;">&#93;</span>;
               dispatch_semaphore_signal<span style="color: #002200;">&#40;</span>itemLock<span style="color: #002200;">&#41;</span>;
            <span style="color: #002200;">&#125;</span>
         <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
   dispatch_release<span style="color: #002200;">&#40;</span>itemLock<span style="color: #002200;">&#41;</span>;
&nbsp;
   NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%i&quot;</span>, <span style="color: #002200;">&#91;</span>items count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
   <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
   <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>&#8220;<em>dispatch_semaphore_create</em>&#8221; function is used to create a dispatch semaphore. The parameter specifies the starting value for the semaphore. To wait for the resource to be available, you have to use &#8220;<em>dispatch_semaphore_wait</em>&#8220;. Use &#8220;<em>dispatch_semaphore_signal</em>&#8221; function to signal the semaphore. Finally, &#8220;<em>dispatch_release</em>&#8221; function is called to release the memory allocated for the semaphore. The semaphore is not managed by the reference counter, so you must release it manually.</p>
<p>This is the basic usage of dispatch semaphore. It also has many other usages, such as managing program flow of threaded application.</p>
Note: There is a print link embedded within this post, please visit this post to print it.
]]></content:encoded>
			<wfw:commentRss>http://blog.aki-null.net/?feed=rss2&amp;p=46</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory Management Battle with GCD</title>
		<link>http://blog.aki-null.net/?p=38</link>
		<comments>http://blog.aki-null.net/?p=38#comments</comments>
		<pubDate>Thu, 08 Oct 2009 02:38:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[GCD]]></category>

		<guid isPermaLink="false">http://blog.aki-null.net/?p=38</guid>
		<description><![CDATA[I have been using GCD quite extensively on my desktop application, and have noticed severe memory management issue. The battle started when I have realised a strange memory usage increase when the application was ran overnight without any user interaction. The memory usage of the application was about 70MB before I went to bed. When [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using GCD quite extensively on my desktop application, and have noticed severe memory management issue. The battle started when I have realised a strange memory usage increase when the application was ran overnight without any user interaction. The memory usage of the application was about 70MB before I went to bed. When I got up, the memory usage was on 400MB. This was unexpected, because I was always making sure that there are no memory leaks in my application using Instruments.</p>
<p>I was trying to come up with a valid explanation of this issue to fix the bug. I have profiled the objects in the memory using the command line tool &#8220;heap&#8221; that was apparently introduced on Snow Leopard. However, I could not find significant issue after running it for an hour.</p>
<p>I could not figure out what was wrong for the entire day, so I restarted the program before I went to bed last night, and profiled the objects in the memory. When I got up this morning, the memory usage was on 400MB as expected. I ran the profiler again, and it told me that there were insane amount of objects that were not being freed. This was unusual, because Instruments told me that there were no leaks. I have fiddled with the application for a while, and ran the profiler again. I was extremely surprised when I saw the new report generated by the heap tool. All objects that were not being freed were all gone! At this point, I&#8217;ve realised that this was not happening to the build that did not have GCD optimisation.</p>
<p>After a while, I found out that it was indeed caused by GCD optimisation code I wrote. I ran some tests, and realised that objects that were allocated in Blocks dispatched to main queue are not being released UNTIL an event loop fires. This means the auto-released objects allocated inside a Block that was dispatched to the main queue are not released until a user interaction occurs.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">dispatch_async<span style="color: #002200;">&#40;</span>dispatch_get_global_queue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">^</span><span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// do something expensive</span>
	dispatch_async<span style="color: #002200;">&#40;</span>dispatch_get_main_queue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">^</span><span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// All auto-released objects allocated</span>
		<span style="color: #11740a; font-style: italic;">// in here are not released until the</span>
		<span style="color: #11740a; font-style: italic;">// next event loop fires.</span>
	<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The above code should explain what was going on. I ran several tests to validate that my hypothesis was correct. To fix the problem, I have changed the above code to the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">dispatch_async<span style="color: #002200;">&#40;</span>dispatch_get_global_queue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">^</span><span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// do something expensive</span>
	dispatch_async<span style="color: #002200;">&#40;</span>dispatch_get_main_queue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">^</span><span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
		<span style="color: #11740a; font-style: italic;">// all auto-released objects are released when</span>
		<span style="color: #11740a; font-style: italic;">// this block finishes!</span>
		<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>After placing the NSAutoreleasePool inside a block dispatched to the main queue, everything was back to normal.</p>
Note: There is a print link embedded within this post, please visit this post to print it.
]]></content:encoded>
			<wfw:commentRss>http://blog.aki-null.net/?feed=rss2&amp;p=38</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fast and Thread-safe Singleton for Objective-C</title>
		<link>http://blog.aki-null.net/?p=26</link>
		<comments>http://blog.aki-null.net/?p=26#comments</comments>
		<pubDate>Tue, 06 Oct 2009 10:12:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://blog.aki-null.net/?p=26</guid>
		<description><![CDATA[I have seen various implementations of Singleton pattern in Objective-C in the past. The most common template I have seen is the following.

static MyClass *instance = nil;
&#160;
+ &#40;MyClass *&#41;sharedInstance &#123;
	@synchronized&#40;self&#41; &#123;
		if &#40;instance == nil&#41; &#123;
			instance = &#91;&#91;self alloc&#93; init&#93;;
		&#125;
	&#125;
	return instance;
&#125;

As you can see, it locks the method using self. The issue with this code is [...]]]></description>
			<content:encoded><![CDATA[<p>I have seen various implementations of Singleton pattern in Objective-C in the past. The most common template I have seen is the following.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> MyClass <span style="color: #002200;">*</span>instance <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MyClass <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedInstance <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>instance <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			instance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> instance;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As you can see, it locks the method using self. The issue with this code is that it can get very slow if this method is called hundreds of times, because the code to instantiate the shared instance must be thread-safe. I was researching for a better way to make this faster, then I found <a href="http://eschatologist.net/blog/?p=178">this article</a>. The comment section of the article details the use of &#8220;Method Swizzling&#8221; to replace the method to access the shared instance with more optimised code (simply returning the instance without a check). Method swizzling allows us to modify the mapping from a selector to an implementation.</p>
<p>I have tried to use the code provided, but it failed to work. I have done some digging around, and I found an article on <a title="CocoaDev" href="http://www.cocoadev.com/index.pl?MethodSwizzling">CocoaDev</a> detailing the swizzling techniques. I have created a category of NSObject using one of the implementation, and modified my Singleton code to the following.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> MyClass <span style="color: #002200;">*</span>instance <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MyClass <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedInstance <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>instance<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			instance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
			OSMemoryBarrier<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
			<span style="color: #002200;">&#91;</span>self swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>sharedInstance<span style="color: #002200;">&#41;</span>
				 withClassMethod<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>sharedInstance2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> instance;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MyClass <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedInstance2 <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> instance;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The above code worked very well. The performance of initialisation would be much slower than the original implementation, but it would be extremely fast after the first initialisation.</p>
<p>Edit: This should be faster than double-checked locking, but I have not tested the performance.</p>
<p>Edit2: I did need the memory barrier after all. The code has been update.</p>
Note: There is a print link embedded within this post, please visit this post to print it.
]]></content:encoded>
			<wfw:commentRss>http://blog.aki-null.net/?feed=rss2&amp;p=26</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Grand Central Dispatch (Part 1)</title>
		<link>http://blog.aki-null.net/?p=3</link>
		<comments>http://blog.aki-null.net/?p=3#comments</comments>
		<pubDate>Wed, 30 Sep 2009 05:55:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[GCD]]></category>

		<guid isPermaLink="false">http://blog.aki-null.net/?p=3</guid>
		<description><![CDATA[I&#8217;ve been working on optimising my Twitter client using Grand Central Dispatch (GCD) recently. Grand Central Dispatch is an Apple technology to optimise application with multicore processor. It was released with Mac OS 10.6 (Snow Leopard).
GCD makes it easier for programmers to perform tasks on different threads to optimise its algorithm performance. There are other [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on optimising my Twitter client using Grand Central Dispatch (GCD) recently. <a title="Apple - Mac OS X - New technologies in Snow Leopard" href="http://www.apple.com/macosx/technology/" target="_blank">Grand Central Dispatch</a> is an Apple technology to optimise application with multicore processor. It was released with Mac OS 10.6 (Snow Leopard).</p>
<p>GCD makes it easier for programmers to perform tasks on different threads to optimise its algorithm performance. There are other interesting usages, which I will probably cover later. The GCD uses the new language feature of Objective-C 2.1 (also available on C and C++), called Blocks. Blocks lets us create closure-like objects to make it easy to execute a block of code parallel to the main thread. Blocks can also be used in C or C++. I&#8217;m not going to cover how to write blocks in this post, so it may be good to have a <a title="Blocks (C language extension)" href="http://en.wikipedia.org/wiki/Blocks_(C_language_extension)">read</a> about it if you don&#8217;t already know how it works.</p>
<p>The first example I&#8217;m going to cover is simple batch processing. It is quite common to batch process data in a loop, but it is usually done on the main thread, which is not desirable for multicore processors.</p>
<p>The following code is a small loop that can be seen in many applications.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// iterate through all tab items and execute an expensive operation</span>
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTabViewItem</span> <span style="color: #002200;">*</span>currentItem <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>tabView tabViewItems<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>currentItem identifier<span style="color: #002200;">&#93;</span> doExpensiveOperation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As you can see, this code will only utilise one core, which is not efficient for multicore processors. This is where we can introduce one of the GCD feature. The following code basically replaces the loop with GCD batch processing function.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// get all tab items</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tabItems <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tabView tabViewItems<span style="color: #002200;">&#93;</span>;
dispatch_apply<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>tabItems count<span style="color: #002200;">&#93;</span>,
		dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>,
		<span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> index<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #400080;">NSTabViewItem</span> <span style="color: #002200;">*</span>currentItem;
			currentItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tabItems objectAtIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>currentItem identifier<span style="color: #002200;">&#93;</span> doExpensiveOperation<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The <em>&#8220;dispatch_apply&#8221; </em>function is used to batch process a specified block. The first argument specifies the number of loops to execute. In the example&#8217;s case, it is the number of tab items.</p>
<p>The second parameter specified the queue to use. Queue in GCD is an object that maintains set of blocks to execute. The actual execution of blocks are done automatically by GCD, so programmer does not need to worry about the execution after scheduling. GCD automatically detects the optimal number of threads to start, so the code does not need to be optimised for different systems.</p>
<p>The third parameter specifies the actual block to execute. In the code above, I&#8217;m getting the tab item I need to process from <em>&#8220;tabItems&#8221;</em> array. The <em>&#8220;index&#8221;</em> parameter provides the current thread index, starting from 0.</p>
<p>The <em>&#8220;dispatch_apply&#8221;</em> blocks, so it will wait until the batch process is complete. Batch processing feature of GCD will automatically optimise the code to run optimally on multicore machine. However, you have to be always be careful when using threads. The block you passed in as a parameter can obviously run on different threads at the same time, which can cause a big issue depending on your code. If <em>&#8220;doExpensiveOperation&#8221;</em> accesses a shared resource, it may cause a serious threading issue. I&#8217;m going to cover how semaphore works in GCD to solve this typical problem of threading in the later post.</p>
Note: There is a print link embedded within this post, please visit this post to print it.
]]></content:encoded>
			<wfw:commentRss>http://blog.aki-null.net/?feed=rss2&amp;p=3</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
