<?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>Matt Galloway&#039;s iPhone Apps</title>
	<atom:link href="http://iphone.galloway.me.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://iphone.galloway.me.uk</link>
	<description>iPhone Applications</description>
	<lastBuildDate>Sat, 11 Feb 2012 17:34:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How does objc_retainAutoreleasedReturnValue work?</title>
		<link>http://iphone.galloway.me.uk/2012/02/how-does-objc_retainautoreleasedreturnvalue-work/</link>
		<comments>http://iphone.galloway.me.uk/2012/02/how-does-objc_retainautoreleasedreturnvalue-work/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 10:55:58 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=550</guid>
		<description><![CDATA[Ever since I started doing my &#8220;A look under ARC&#8217;s hood&#8221; series of blog posts I have been intrigued by objc_retainAutoreleasedReturnValue. It&#8217;s been covered by Mike Ash on his blog from a conceptual point of view but I haven&#8217;t found a decent explanation into exactly how it works. So I took a look and here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I started doing my &#8220;A look under ARC&#8217;s hood&#8221; <a href="http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/">series</a> <a href="http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-2/">of</a> <a href="http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-–-episode-3/">blog</a> <a href="http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-episode-4/">posts</a> I have been intrigued by <code>objc_retainAutoreleasedReturnValue</code>. It&#8217;s been covered by <a href="http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html">Mike Ash on his blog</a> from a conceptual point of view but I haven&#8217;t found a decent explanation into exactly how it works. So I took a look and here&#8217;s what I found.</p>
<p><span id="more-550"></span></p>
<h2>What does it do?</h2>
<p>The concept behind <code>objc_autoreleaseReturnValue</code> is that if a value is to be returned from a function autoreleased, but the very next thing that is to be done is a retain on that object then it&#8217;s absolutely pointless doing the autorelease and retain &#8211; we&#8217;re just wasting cycles. So if we can somehow determine that we&#8217;re about to retain then we can save a few CPU cycles. Over the course of a running application this could add up to quite a lot of time and effort saved.</p>
<p>In <a href="http://www.opensource.apple.com/source/objc4/objc4-493.9/runtime/objc-arr.mm">Apple&#8217;s code</a> they say this:</p>
<blockquote><p>
  objc_autoreleaseReturnValue() examines the caller&#8217;s instructions following<br />
  the return. If the caller&#8217;s instructions immediately call<br />
  objc_autoreleaseReturnValue, then the callee omits the -autorelease and saves<br />
  the result in thread-local storage. If the caller does not look like it<br />
  cooperates, then the callee calls -autorelease as usual.</p>
<p>  objc_autoreleaseReturnValue checks if the returned value is the same as the<br />
  one in thread-local storage. If it is, the value is used directly. If not,<br />
  the value is assumed to be truly autoreleased and is retained again.  In<br />
  either case, the caller now has a retained reference to the value.
</p></blockquote>
<p>[I think there is a typo there in that it should read <em>"If the caller's instructions immediately call objc_retainAutoreleasedReturnValue"</em>.]</p>
<p>So basically what it means is that if you consider this bit of code:</p>
<pre class="brush: objc; title: ; notranslate">
- (SomeClass*)createMeAnObject {
   SomeClass *obj = [[SomeClass alloc] init];
   obj.string = @&quot;Badger&quot;;
   obj.number = 10;
   return obj;
}

- (id)init {
    if ((self = [super init])) {
        self.myObject = [self createMeAnObject];
    }
    return self;
}
</pre>
<p>Just ignore the fact that you wouldn&#8217;t really do that, but if we rewrite that to include the retains, releases &#038; autoreleases that will be going on behind the scenes then it looks like this:</p>
<pre class="brush: objc; title: ; notranslate">
- (SomeClass*)createMeAnObject {
   SomeClass *obj = [[SomeClass alloc] init];
   obj.string = @&quot;Badger&quot;;
   obj.number = 10;
   return [obj autorelease];
}

- (id)init {
    if ((self = [super init])) {
        [myObject release];
        SomeClass *temp = [self createMeAnObject];
        myObject = [temp retain];
    }
    return self;
}
</pre>
<p>Now then if we inline the <code>createMeAnObject</code> code into <code>init</code>:</p>
<pre class="brush: objc; title: ; notranslate">
- (id)init {
    if ((self = [super init])) {
        [myObject release];
        SomeClass *temp = [[SomeClass alloc] init];
        obj.string = @&quot;Badger&quot;;
        obj.number = 10;
        [temp autorelease];
        myObject = [temp retain];
    }
    return self;
}
</pre>
<p>Here we notice that there is a <code>[temp autorelease]</code> followed immediately by a <code>[temp retain]</code>. It is this optimisation that the new Objective-C runtime can help us with.</p>
<h2>How does it work &#8211; objc_autoreleaseReturnValue?</h2>
<p>The code is <a href="http://www.opensource.apple.com/source/objc4/objc4-493.9/runtime/objc-arr.mm">out there for the x86 version</a> of this, but there&#8217;s no ARM code so I had to go digging into the disassembly for it.</p>
<p>Here&#8217;s the disassembly for <code>objc_autoreleaseReturnValue</code>:</p>
<pre class="brush: cpp; title: ; notranslate">
_objc_autoreleaseReturnValue:
00006ec4            b580        push    {r7, lr}
00006ec6            466f        mov     r7, sp
00006ec8        f01e0f01        tst.w   lr, #1  @ 0x1
00006ecc            d004        beq.n   0x6ed8
00006ece        f83e1c01        ldrh.w  r1, [lr, #-1]
00006ed2        f244623f        movw    r2, 0x463f
00006ed6            e005        b.n     0x6ee4
00006ed8        f8de1000        ldr.w   r1, [lr]
00006edc        f2470207        movw    r2, 0x7007
00006ee0        f2ce12a0        movt    r2, 0xe1a0
00006ee4            4291        cmp     r1, r2
00006ee6            d106        bne.n   0x6ef6
00006ee8        ee1d1f70        mrc     15, 0, r1, cr13, cr0, {3}
00006eec        f0210103        bic.w   r1, r1, #3      @ 0x3
00006ef0        f8c100c4        str.w   r0, [r1, #196]
00006ef4            bd80        pop     {r7, pc}
00006ef6        f00bfb93        bl      _objc_autorelease
00006efa            bd80        pop     {r7, pc}
</pre>
<p>Let&#8217;s break that down then&#8230;</p>
<blockquote>
<pre>
00006ec4            b580        push    {r7, lr}
00006ec6            466f        mov     r7, sp
</pre>
</blockquote>
<p>This is a standard prologue for a method in ARM.</p>
<blockquote>
<pre>
00006ec8        f01e0f01        tst.w   lr, #1  @ 0x1
00006ecc            d004        beq.n   0x6ed8
</pre>
</blockquote>
<p>Here we are doing the first bit of our sniffing of the following instructions. <code>lr</code> is the &#8220;link register&#8221; and contains the address of the method that we&#8217;re returning to. Since this method is always called as a <a href="http://en.wikipedia.org/wiki/Tail_call">tail call</a>, this will contain the address of the caller of our method that&#8217;s returning a value autoreleased.</p>
<p>The <code>tst</code> instruction is doing a bitwise AND of the value in <code>lr</code> and the integer value 1. Then the <code>beq</code> will branch if the zero flag is set, i.e. if <code>lr &#038; 1 == 0</code>. So this means that we are testing if the lowest bit is <em>not</em> set. You can either read up about ARM processors or take it from me that if the low bit is set on the link register then it means the caller is in thumb mode. So this means that if we&#8217;re going back to ARM code then we branch over a few instructions to <code>0x6ed8</code> whereas if we&#8217;re going back to Thumb code then we don&#8217;t branch.</p>
<blockquote>
<pre>
00006ece        f83e1c01        ldrh.w  r1, [lr, #-1]
00006ed2        f244623f        movw    r2, 0x463f
00006ed6            e005        b.n     0x6ee4
</pre>
</blockquote>
<p>This is the case that gets run if our condition before was not true. We are loading a half word (16-bits) from <code>lr - 1</code> into <code>r1</code> (we need the -1 because of the reason from before that the low bit is set if we&#8217;re in Thumb mode so actually the next instruction after return will be at <code>lr - 1</code>). We then put <code>0x463f</code> into <code>r2</code>. Then we jump to <code>0x6ee4</code>.</p>
<blockquote>
<pre>
00006ed8        f8de1000        ldr.w   r1, [lr]
00006edc        f2470207        movw    r2, 0x7007
00006ee0        f2ce12a0        movt    r2, 0xe1a0
</pre>
</blockquote>
<p>This is the case that gets run if our condition before was true. We are here loading a whole 32-bits from <code>lr</code> into <code>r1</code> and loading <code>0xe1a07007</code> into <code>r2</code>.</p>
<blockquote>
<pre>
00006ee4            4291        cmp     r1, r2
00006ee6            d106        bne.n   0x6ef6
</pre>
</blockquote>
<p>The next section compares the two registers that we&#8217;ve just been setting in one of two ways. If they are not equal then we branch over to <code>0x6ef6</code></p>
<p>So we&#8217;re matching against either <code>0x463f</code> (if it&#8217;s Thumb mode) or <code>0xe1a07007</code> (if it&#8217;s ARM mode). Why do we care that the instructions that we&#8217;re about to run when we return have those particular binary values? Well if we compile a method that does the <code>objc_autoreleaseReturnValue</code> and <code>objc_retainAutoreleasedReturnValue</code> dance then we see that the compiler adds in an instruction which acts as a marker. Let&#8217;s see what it looks like:</p>
<pre class="brush: cpp; title: ; notranslate">
Thumb mode:
f7ffef56	blx	_objc_msgSend
    463f	mov	r7, r7
f7ffef54	blx	_objc_retainAutoreleasedReturnValue

ARM mode:
ebffffa0	bl	_objc_msgSend
e1a07007	mov	r7, r7
ebffff9e	bl	_objc_retainAutoreleasedReturnValue
</pre>
<p>Well take a look at that. It&#8217;s added in a <code>mov r7, r7</code> in each case which is a noop (i.e. does nothing as it moves <code>r7</code> back into itself). If you examine the binary values for these instructions then you&#8217;ll see they match the values that we were told to compare against. The compiler has added this as a marker to tell the <code>objc_autoreleaseReturnValue</code> that the caller is about to call <code>objc_retainAutoreleasedReturnValue</code>.</p>
<blockquote>
<pre>
00006ee8        ee1d1f70        mrc     15, 0, r1, cr13, cr0, {3}
00006eec        f0210103        bic.w   r1, r1, #3      @ 0x3
00006ef0        f8c100c4        str.w   r0, [r1, #196]
00006ef4            bd80        pop     {r7, pc}
</pre>
</blockquote>
<p>This is the code that gets run if the instructions matched. It appears to be getting a value from a coprocessor (the <code>mrc</code> instruction) then acting on it and storing <code>r0</code> (which will be the value that&#8217;s to be returned) into the memory location computed. Then it returns. I&#8217;m not entirely sure what this coprocessor magic is doing but it will probably become apparent when we look at the code for <code>objc_retainAutoreleasedReturnValue</code>. But essentially it&#8217;s setting some flag that we&#8217;ll read later.</p>
<blockquote>
<pre>
00006ef6        f00bfb93        bl      _objc_autorelease
00006efa            bd80        pop     {r7, pc}
</pre>
</blockquote>
<p>Finally, this is where we get to if the instructions did not match. This performs a normal call to <code>objc_autorelease</code> incase the caller is not about to retain the object.</p>
<h2>How does it work &#8211; objc_retainAutoreleasedReturnValue?</h2>
<p>Let&#8217;s now take a look at <code>objc_retainAutoreleasedReturnValue</code>:</p>
<pre class="brush: cpp; title: ; notranslate">
_objc_retainAutoreleasedReturnValue:
00012bbc            b580        push    {r7, lr}
00012bbe        ee1d1f70        mrc     15, 0, r1, cr13, cr0, {3}
00012bc2            466f        mov     r7, sp
00012bc4        f0210103        bic.w   r1, r1, #3      @ 0x3
00012bc8        f8d110c4        ldr.w   r1, [r1, #196]
00012bcc            4281        cmp     r1, r0
00012bce            d107        bne.n   0x12be0
00012bd0        ee1d1f70        mrc     15, 0, r1, cr13, cr0, {3}
00012bd4            2200        movs    r2, #0
00012bd6        f0210103        bic.w   r1, r1, #3      @ 0x3
00012bda        f8c120c4        str.w   r2, [r1, #196]
00012bde            bd80        pop     {r7, pc}
00012be0        f7f3f976        bl      _objc_retain
00012be4            bd80        pop     {r7, pc}
</pre>
<p>And again, breaking that down we get:</p>
<blockquote>
<pre>
00012bbc            b580        push    {r7, lr}
</pre>
</blockquote>
<p>Standard prologue for a method.</p>
<blockquote>
<pre>
00012bbe        ee1d1f70        mrc     15, 0, r1, cr13, cr0, {3}
00012bc2            466f        mov     r7, sp
00012bc4        f0210103        bic.w   r1, r1, #3      @ 0x3
00012bc8        f8d110c4        ldr.w   r1, [r1, #196]
</pre>
</blockquote>
<p>Here we get some more context on what that <code>mrc</code> was all about before. We can see here that we&#8217;re running the same instruction as we did before and doing the same <code>bic</code> instruction and then loading value stored at the computed address into <code>r1</code>.</p>
<blockquote>
<pre>
00012bcc            4281        cmp     r1, r0
00012bce            d107        bne.n   0x12be0
</pre>
</blockquote>
<p>Now this is the interesting bit. We&#8217;re checking that the value we obtained from doing the dance with the coprocessor (<code>r1</code>) is the same as the object passed into this method (<code>r0</code>). If these two match then we know that the object we are trying to retain has just been returned from a method that had called <code>objc_autoreleaseReturnValue</code>. So we don&#8217;t need to do anything. It&#8217;s not been autoreleased so we&#8217;re not going to retain it.</p>
<blockquote>
<pre>
00012bd0        ee1d1f70        mrc     15, 0, r1, cr13, cr0, {3}
00012bd4            2200        movs    r2, #0
00012bd6        f0210103        bic.w   r1, r1, #3      @ 0x3
00012bda        f8c120c4        str.w   r2, [r1, #196]
00012bde            bd80        pop     {r7, pc}
</pre>
</blockquote>
<p>This is the code that then gets run if the comparison was true &#8211; i.e. this object had just gone through a <code>objc_autoreleaseReturnValue</code>. We clear out the value in the coprocessor magic dance and return.</p>
<blockquote>
<pre>
00012be0        f7f3f976        bl      _objc_retain
00012be4            bd80        pop     {r7, pc}
</pre>
</blockquote>
<p>If it didn&#8217;t match, then we know that this object has not gone through a <code>objc_autoreleaseReturnValue</code>, which is likely because the method we called was not compiled with ARC enabled. So we do a retain.</p>
<h2>Phew, so explain that again please?</h2>
<p>It&#8217;s probably easiest to consider the following pseudo code:</p>
<pre class="brush: cpp; title: ; notranslate">
id objc_autoreleaseReturnValue(id object) {
    if (thumb_mode &amp;&amp; next_instruction_after_return == 0x463f ||
        arm_mode   &amp;&amp; next_instruction_after_return == 0xe1a07007)
    {
        set_flag(object);
    } else {
        return objc_autorelease(object);
    }
}

id objc_retainAutoreleasedReturnValue(id object) {
    if (get_flag(object)) {
        clear_flag();
    } else {
        return objc_retain(object);
    }
}
</pre>
<p>That is basically what it all boils down to and with some tail call optimisations this can all be incredibly optimised compared to all the redundant autorelease followed by retain pairs that we must have had in code before ARC was invented.</p>
<h2>Conclusions</h2>
<p>This is yet again some awesome stuff from the Apple engineers here. Sniffing the next instructions to be executed is some very clever stuff to ensure that it&#8217;s always going to work. They must have to ensure that the marker (<code>mov r7, r7</code>) isn&#8217;t moved by the optimiser for instance and I&#8217;m sure lots of edge cases but it seems to work a treat!</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/02/how-does-objc_retainautoreleasedreturnvalue-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look under ARC&#8217;s hood &#8211; Episode 4</title>
		<link>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-episode-4/</link>
		<comments>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-episode-4/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 13:33:46 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=518</guid>
		<description><![CDATA[The next episode of my deep clambering into the underbelly of ARC starts from this Tweet by @steipete where he says &#8220;With ARC, I now find myself typing &#8220;new&#8221; for dumb model objects. Yay or Nay?&#8221;. It got me thinking. He&#8217;s totally right that with ARC we can now just use [SomeClass new] and let [...]]]></description>
			<content:encoded><![CDATA[<p>The next episode of my deep clambering into the underbelly of ARC starts from <a href="http://twitter.com/#!/steipete/status/165159717814013953">this Tweet by @steipete</a> where he says &#8220;With ARC, I now find myself typing &#8220;new&#8221; for dumb model objects. Yay or Nay?&#8221;. It got me thinking. He&#8217;s totally right that with ARC we can now just use <code>[SomeClass new]</code> and let ARC handle all the memory management for us. Previously we&#8217;d often create a convenience class method on <code>SomeClass</code> which would return an object autoreleased so that it made the calling code clean and easy to understand the memory management. Now with ARC we don&#8217;t need to do that and I wondered what would be the benefit of using <code>new</code> over <code>alloc + init</code> over using our old friends, the convenience class methods. This blog post tells that story.</p>
<p><span id="more-518"></span></p>
<h2>Some background on new</h2>
<p>First we&#8217;ll take a look at what <code>new</code> actually does. <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/clm/NSObject/new">According to the Apple documentation</a>, it does this:</p>
<blockquote><p>Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.
</p></blockquote>
<p>So we should expect a call like <code>[SomeClass new]</code> to be equivalent to <code>[[SomeClass alloc] init]</code>. The memory management here tells us that the returned object is owned by the caller, i.e. it&#8217;s returned with a +1 retain count. In the days of pre-ARC, we would therefore have to release this object when we were done with it. ARC adds these in for us as we know.</p>
<h2>What&#8217;s being tested</h2>
<p>What I wanted to know is which is faster out of these methods:</p>
<ol>
<li><code>[[SomeClass alloc] init]</code></li>
<li><code>[SomeClass new]</code></li>
<li><code>[SomeClass giveMeAnObject]</code></li>
<li><code>[SomeClass newObject]</code></li>
</ol>
<p>Where <code>giveMeAnObject</code> is a convenience method to return an object autoreleased and <code>newObject</code> is a convenience method which we would hope is the same as the standard <code>new</code>.</p>
<h2>How to test</h2>
<p>In order to benchmark each of these methods I decided to time how long it would take to call each of them a given number of times with correct memory management (well, I have no choice if ARC is enabled). I used this method for timing which gives me the number of nanoseconds that my code took to execute:</p>
<pre class="brush: objc; title: ; notranslate">
uint64_t start = mach_absolute_time();
// Do something which takes a while
uint64_t end = mach_absolute_time();

mach_timebase_info_data_t timebaseInfo;
mach_timebase_info(&amp;timebaseInfo);
uint64_t timeNanos = (end - start) * timebaseInfo.numer / timebaseInfo.denom;
NSLog(@&quot;time = %&quot;PRIu64, timeNanos);
</pre>
<p>In order to test this and to ensure there&#8217;d be no shortcuts made by the compiler / runtime by using an <code>NSString</code> or an <code>NSNumber</code> I created a simple dummy class called <code>ClassA</code> like so:</p>
<pre class="brush: objc; title: ; notranslate">
@interface ClassA : NSObject
+ (ClassA*)giveMeAnObject;
+ (ClassA*)newObject;
@end

@implementation ClassA
+ (ClassA*)giveMeAnObject {
    return [[ClassA alloc] init];
}
+ (ClassA*)newObject {
    return [[ClassA alloc] init];
}
@end
</pre>
<p>Then to benchmark each one I decided to loop for a number of iterations ranging from 1000 to 10000000 for each style of creating an instance of <code>ClassA</code>. Each of these should have the exact same effect, but we&#8217;d like to know how they differ in speed. Below is the code I used, commenting out all but one of the <code>ClassA *x =</code> each time I did the test.</p>
<pre class="brush: objc; title: ; notranslate">
for (unsigned long long i = 0; i &lt; iterations; ++i) {
    ClassA *a = [[ClassA alloc] init];
    ClassA *b = [ClassA new];
    ClassA *c = [ClassA giveMeAnObject];
    ClassA *d = [ClassA newObject];
}
</pre>
<p>For each of these tests I used my iPhone 4 (so ARMv7), running iOS 5.0.1 and compiled the code at <code>O3</code>.</p>
<h2>The results are in!</h2>
<p>Below are the results of running the tests. The value under each column is the time taken in milliseconds for the number of iterations given on the left.</p>
<style type="text/css">
table.arc4-results {
    border-width: 0px;
    border-spacing: 0px;
    border-style: solid;
    border-color: gray;
    border-collapse: collapse;
    background-color: white;
}
table.arc4-results th {
    border-width: 1px;
    padding: 5px;
    border-style: solid;
    border-color: gray;
    background-color: white;
    -moz-border-radius: ;
}
table.arc4-results td {
    border-width: 1px;
    padding: 5px;
    border-style: solid;
    border-color: gray;
    background-color: white;
    -moz-border-radius: ;
    text-align: center;
}
</style>
<table class="arc4-results">
<tr>
<td></td>
<td><b>A</b></td>
<td><b>B</b></td>
<td><b>C</b></td>
<td><b>D</b></td>
</tr>
<tr>
<td><b>1000</b></td>
<td>2.264</td>
<td>2.349</td>
<td>2.199</td>
<td>2.394</td>
</tr>
<tr>
<td><b>5000</b></td>
<td>10.102</td>
<td>10.149</td>
<td>9.993</td>
<td>11.017</td>
</tr>
<tr>
<td><b>10000</b></td>
<td>19.180</td>
<td>20.148</td>
<td>19.509</td>
<td>20.036</td>
</tr>
<tr>
<td><b>50000</b></td>
<td>92.357</td>
<td>98.177</td>
<td>104.362</td>
<td>97.099</td>
</tr>
<tr>
<td><b>100000</b></td>
<td>185.054</td>
<td>199.825</td>
<td>204.560</td>
<td>194.353</td>
</tr>
<tr>
<td><b>500000</b></td>
<td>924.090</td>
<td>1000.588</td>
<td>1335.106</td>
<td>985.735</td>
</tr>
<tr>
<td><b>1000000</b></td>
<td>1863.110</td>
<td>1973.086</td>
<td>2885.719</td>
<td>1977.487</td>
</tr>
<tr>
<td><b>5000000</b></td>
<td>9407.941</td>
<td>10245.857</td>
<td>23314.495</td>
<td>9757.074</td>
</tr>
<tr>
<td><b>10000000</b></td>
<td>18557.632</td>
<td>20841.905</td>
<td>56602.491</td>
<td>20315.784</td>
</tr>
</table>
<p>And graphically, that looks like this:</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/02/ARC-Episode4-graph.png" rel="lightbox[518]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/02/ARC-Episode4-graph-1024x560.png" alt="Results" title="Results" width="550" class="aligncenter size-large wp-image-539" /></a></p>
<p>So what does that tell us then? Well it basically tells us that <code>alloc + init</code> is fastest, with <code>new</code> and our custom convenience <code>new</code> close behind. It also shows us that for large iterations, our convenience method that returns the value autoreleased is quite a bit slower. At the maximum number of iterations, it was more than twice as slow as the other methods.</p>
<h2>Let&#8217;s analyse what happened then</h2>
<p>In order to understand what&#8217;s going on here, let&#8217;s take a look at the code generated. Below are the various interesting bits of code.</p>
<p><code>ClassA</code>&#8216;s <code>giveMeAnObject</code>:</p>
<pre class="brush: cpp; title: ; notranslate">
	.align	2
	.code	16
	.thumb_func	&quot;+[ClassA giveMeAnObject]&quot;
&quot;+[ClassA giveMeAnObject]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_0:
        add     r1, pc
LPC0_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC0_2+4))
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC0_2+4))
LPC0_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop.w   {r7, lr}
        b.w     _objc_autorelease
</pre>
<p>[18 instructions]</p>
<p><code>ClassA</code>&#8216;s <code>newObject</code>:</p>
<pre class="brush: cpp; title: ; notranslate">
	.align	2
	.code	16
	.thumb_func	&quot;+[ClassA newObject]&quot;
&quot;+[ClassA newObject]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC4_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC4_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC4_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC4_1+4))
LPC4_0:
        add     r1, pc
LPC4_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC4_2+4))
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC4_2+4))
LPC4_2:
        add     r1, pc
        ldr     r1, [r1]
        pop.w   {r7, lr}
        b.w     _objc_msgSend
</pre>
<p>[17 instructions]</p>
<p>Iteration loop for method A:</p>
<pre class="brush: cpp; title: ; notranslate">
LBB2_1:
	ldr	r1, [r5]
	ldr	r0, [r6]
	blx	_objc_msgSend
	ldr.w	r1, [r8]
	blx	_objc_msgSend
	blx	_objc_release
	adds.w	r11, r11, #1
	eor.w	r0, r11, r10
	adc	r4, r4, #0
	orrs	r0, r4
	bne	LBB2_1
</pre>
<p>[11 instructions]</p>
<p>Iteration loop for method B:</p>
<pre class="brush: cpp; title: ; notranslate">
LBB2_1:
	ldr	r1, [r5]
	ldr	r0, [r6]
	blx	_objc_msgSend
	blx	_objc_release
	adds.w	r10, r10, #1
	eor.w	r0, r10, r8
	adc	r4, r4, #0
	orrs	r0, r4
	bne	LBB2_1
</pre>
<p>[9 instructions]</p>
<p>Iteration loop for method C:</p>
<pre class="brush: cpp; title: ; notranslate">
LBB2_1:
	ldr	r1, [r5]
	ldr	r0, [r6]
	blx	_objc_msgSend
	@ InlineAsm Start
	mov	r7, r7		@ marker for objc_retainAutoreleaseReturnValue
	@ InlineAsm End
	blx	_objc_retainAutoreleasedReturnValue
	blx	_objc_release
	adds.w	r10, r10, #1
	eor.w	r0, r10, r8
	adc	r4, r4, #0
	orrs	r0, r4
	bne	LBB2_1
</pre>
<p>[11 instructions]</p>
<p>Iteration loop for method D:</p>
<pre class="brush: cpp; title: ; notranslate">
LBB2_1:
	ldr	r1, [r5]
	ldr	r0, [r6]
	blx	_objc_msgSend
	blx	_objc_release
	adds.w	r10, r10, #1
	eor.w	r0, r10, r8
	adc	r4, r4, #0
	orrs	r0, r4
	bne	LBB2_1
</pre>
<p>[9 instructions]</p>
<p>So having looked at all the relevant code it might be surprising that these are that different. They&#8217;re all going to have a similar number of instructions. Infact method A has in the inner loop more instructions, but it was the fastest. The interesting question is why is method C so much slower than the others for large number of iterations? If we take a look at the generated code for method C we&#8217;ll notice that there&#8217;s a call to <code>objc_retainAutoreleasedReturnValue</code>. This method is a kind of shortcut to retain a value that will have been returned autoreleased. It should be working with our code since all of this is compiled using ARC and running on an iOS 5 device. It was interesting to me then that this method took twice as long at large numbers of iterations. I can understand that it&#8217;s likely to be slower since there&#8217;s more message dispatch going on, but I did not expect it to be that much slower and also interesting that the difference increase with increasing number of iterations.</p>
<h2>Conclusions</h2>
<p><del datetime="2012-02-05T13:33:49+00:00">I&#8217;m actually at a loss as to how to explain why method C is so much slower</del>. It&#8217;s great to see that A, B and D are roughly the same speed, which is of course what we would expect. <del datetime="2012-02-05T13:33:49+00:00">This whole thing does mean that we are much better off using <code>new</code>, <code>alloc + init</code> or a convenience method that returns an object with a +1 retain count rather than using convenience methods that return the object autoreleased.</del> See below for a reasoning for why method C was slower and how method C can become just as fast as the other methods.</p>
<h2>Ah ha! That&#8217;s why!</h2>
<p>Having done a bit more digging I have found why method C was so much slower. Whilst I was writing this up I thought it was a bit odd that the tail call in <code>giveMeAnObject</code> was to <code>objc_autorelease</code> rather than <code>objc_autoreleaseReturnValue</code>. The magic of <code>objc_retainAutoreleasedReturnValue</code> which I refer to previously only works if the value has been returned with <code>objc_autoreleaseReturnValue</code>. The internals of that are for a later blog post but just take it from me that it works like that. So I decided to just change the return type of <code>giveMeAnObject</code> from <code>ClassA*</code> to <code>id</code>. I thought that this should make absolutely no difference. I was wrong. Take a look and see:</p>
<pre class="brush: objc; title: ; notranslate">
+ (id)giveMeAnObject {
    return [[ClassA alloc] init];
}
</pre>
<p>This outputs this assembly:</p>
<pre class="brush: objc; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;+[ClassA giveMeAnObject3]&quot;
&quot;+[ClassA giveMeAnObject3]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC2_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC2_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
LPC2_0:
        add     r1, pc
LPC2_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_2+4))
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_2+4))
LPC2_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop.w   {r7, lr}
        b.w     _objc_autoreleaseReturnValue
</pre>
<p>The single difference here is the call to <code>objc_autoreleaseReturnValue</code> rather than <code>objc_autorelease</code>. <del datetime="2012-02-07T17:29:39+00:00">I still don&#8217;t particularly understand why the compiler is doing something different here, so I&#8217;ve still to work that one through but</del> The results for the benchmark using this method are as follows (added to the previous results where I&#8217;ve called this new method, E):</p>
<table class="arc4-results">
<tr>
<td></td>
<td><b>A</b></td>
<td><b>B</b></td>
<td><b>C</b></td>
<td><b>D</b></td>
<td><b>E</b></td>
</tr>
<tr>
<td><b>1000</b></td>
<td>2.264</td>
<td>2.349</td>
<td>2.199</td>
<td>2.394</td>
<td>2.401</td>
</tr>
<tr>
<td><b>5000</b></td>
<td>10.102</td>
<td>10.149</td>
<td>9.993</td>
<td>11.017</td>
<td>11.381</td>
</tr>
<tr>
<td><b>10000</b></td>
<td>19.180</td>
<td>20.148</td>
<td>19.509</td>
<td>20.036</td>
<td>22.120</td>
</tr>
<tr>
<td><b>50000</b></td>
<td>92.357</td>
<td>98.177</td>
<td>104.362</td>
<td>97.099</td>
<td>106.966</td>
</tr>
<tr>
<td><b>100000</b></td>
<td>185.054</td>
<td>199.825</td>
<td>204.560</td>
<td>194.353</td>
<td>223.045</td>
</tr>
<tr>
<td><b>500000</b></td>
<td>924.090</td>
<td>1000.588</td>
<td>1335.106</td>
<td>985.735</td>
<td>1113.261</td>
</tr>
<tr>
<td><b>1000000</b></td>
<td>1863.110</td>
<td>1973.086</td>
<td>2885.719</td>
<td>1977.487</td>
<td>2262.960</td>
</tr>
<tr>
<td><b>5000000</b></td>
<td>9407.941</td>
<td>10245.857</td>
<td>23314.495</td>
<td>9757.074</td>
<td>11419.025</td>
</tr>
<tr>
<td><b>10000000</b></td>
<td>18557.632</td>
<td>20841.905</td>
<td>56602.491</td>
<td>20315.784</td>
<td>22510.462</td>
</tr>
</table>
<p>So that at least explains why method C was so much slower. <del datetime="2012-02-07T17:29:39+00:00">But I&#8217;ve no idea why the compiler doesn&#8217;t emit the same thing when the return type of <code>giveMeAnObject</code> is <code>ClassA*</code> or <code>id</code>.</del></p>
<h2>Update: Turns out, it&#8217;s a bug</h2>
<p>It <a href="http://stackoverflow.com/a/9180782/1068248">turns out that it&#8217;s a bug that the compiler</a> (well, the optimiser part of the compiler) did something different for the case of returning <code>id</code> versus <code>ClassA*</code> and the cases of splitting out the <code>alloc + init</code> in the method versus returning on the same line. All of these should compile exactly the same, but they don&#8217;t in the current version of clang.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-episode-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look under ARC’s hood – Episode 3</title>
		<link>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/</link>
		<comments>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 10:46:38 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=509</guid>
		<description><![CDATA[This instalment of &#8220;A look under ARC&#8217;s hood&#8221; is all about the new @autoreleasepool directive. LLVM tells us that the semantics of autorelease pools has changed with LLVM 3.0 and in particular, I thought it might be interesting to see what ARC is doing when it comes to these. So consider the following method: This [...]]]></description>
			<content:encoded><![CDATA[<p>This instalment of &#8220;A look under ARC&#8217;s hood&#8221; is all about the new <code>@autoreleasepool</code> directive. <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool">LLVM tells us</a> that the semantics of autorelease pools has changed with LLVM 3.0 and in particular, I thought it might be interesting to see what ARC is doing when it comes to these.</p>
<p><span id="more-509"></span></p>
<p>So consider the following method:</p>
<pre class="brush: objc; title: ; notranslate">
void foo() {
    @autoreleasepool {
        NSNumber *number = [NSNumber numberWithInt:0];
        NSLog(@&quot;number = %p&quot;, number);
    }
}
</pre>
<p>This is entirely contrived, of course, but it should let us see what&#8217;s going on. In non-ARC land we would assume here that <code>number</code> would be allocated inside <code>numberWithInt:</code> and returned autoreleased. So when the autorelease pool is next drained, it will be released. So let&#8217;s see if that&#8217;s what happened (as usual, this is ARMv7 instructions):</p>
<pre class="brush: cpp; highlight: [8,21,29]; title: ; notranslate">
        .globl  _foo
        .align  2
        .code   16
        .thumb_func     _foo
_foo:
        push    {r4, r7, lr}
        add     r7, sp, #4
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        mov     r1, r0
        movw    r0, :lower16:(L__unnamed_cfstring_-(LPC0_2+4))
        movt    r0, :upper16:(L__unnamed_cfstring_-(LPC0_2+4))
LPC0_2:
        add     r0, pc
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        pop     {r4, r7, pc}
</pre>
<p>Well, yes. That&#8217;s exactly what&#8217;s happening. We can see the call to push an autorelease pool then a call to <code>numberWithInt:</code> then a call to pop an autorelease pool. Exactly what we&#8217;d expect. Now let&#8217;s look at the exact same code compiled under ARC:</p>
<pre class="brush: cpp; highlight: [25,34]; title: ; notranslate">
        .globl  _foo
        .align  2
        .code   16
        .thumb_func     _foo
_foo:
        push    {r4, r5, r7, lr}
        add     r7, sp, #8
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        mov     r5, r0
        movw    r0, :lower16:(L__unnamed_cfstring_-(LPC0_2+4))
        movt    r0, :upper16:(L__unnamed_cfstring_-(LPC0_2+4))
        mov     r1, r5
LPC0_2:
        add     r0, pc
        blx     _NSLog
        mov     r0, r5
        blx     _objc_release
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        pop     {r4, r5, r7, pc}
</pre>
<p>Notice the calls to <code>objc_retainAutoreleasedReturnValue</code> and <code>objc_release</code>. What&#8217;s happening there is that ARC has determined for us that it doesn&#8217;t really need to worry about the autorelease pool that&#8217;s in place, because it can simply tell the autorelease to not happen (with the call to <code>objc_retainAutoreleasedReturnValue</code>) and then release the object later itself. This is desirable as it means the autorelease logic doesn&#8217;t have to happen.</p>
<p>Note that the autorelease pool is still required to be pushed and popped because ARC can&#8217;t know what&#8217;s going on in the calls to <code>numberWithInt:</code> and <code>NSLog</code> to know if objects will be put into the pool there. If it did know that they didn&#8217;t autorelease anything then it could actually get rid of the push and pop. Perhaps that kind of logic will come in future versions although I&#8217;m not quite sure how the semantics of that would work though.</p>
<p>Now let&#8217;s consider another example which is where we want to use <code>number</code> outside of the scope of the autorelease pool block. This should show us why ARC is a wonder to work with. Consider the following code:</p>
<pre class="brush: objc; title: ; notranslate">
void bar() {
    NSNumber *number;
    @autoreleasepool {
        number = [NSNumber numberWithInt:0];
        NSLog(@&quot;number = %p&quot;, number);
    }
    NSLog(@&quot;number = %p&quot;, number);
}
</pre>
<p>You might be (correctly) thinking that this is going to cause problems even though it looks perfectly innocuous. It&#8217;s a problem because <code>number</code> will be allocated inside the autorelease pool block, will be deallocated when the autorelease pool pops but is then used after it&#8217;s been deallocated. Uh oh! Let&#8217;s see if we&#8217;re right by compiling it without ARC enabled:</p>
<pre class="brush: cpp; title: ; notranslate">
        .globl  _bar
        .align  2
        .code   16
        .thumb_func     _bar
_bar:
        push    {r4, r5, r6, r7, lr}
        add     r7, sp, #12
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r6, :lower16:(L__unnamed_cfstring_-(LPC1_2+4))
        movt    r6, :upper16:(L__unnamed_cfstring_-(LPC1_2+4))
LPC1_2:
        add     r6, pc
        mov     r5, r0
        mov     r1, r5
        mov     r0, r6
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        mov     r0, r6
        mov     r1, r5
        blx     _NSLog
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Obviously no calls to retain, release or autorelease as we&#8217;d expect since we haven&#8217;t made any explicitly and we&#8217;re not using ARC. We can see here that it&#8217;s been compiled exactly as we&#8217;d expect from our reasoning before. So let&#8217;s see what it looks like when ARC gives us a helping hand:</p>
<pre class="brush: cpp; highlight: [25,40]; title: ; notranslate">
        .globl  _bar
        .align  2
        .code   16
        .thumb_func     _bar
_bar:
        push    {r4, r5, r6, r7, lr}
        add     r7, sp, #12
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        movw    r6, :lower16:(L__unnamed_cfstring_-(LPC1_2+4))
        movt    r6, :upper16:(L__unnamed_cfstring_-(LPC1_2+4))
LPC1_2:
        add     r6, pc
        mov     r5, r0
        mov     r1, r5
        mov     r0, r6
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        mov     r0, r6
        mov     r1, r5
        blx     _NSLog
        mov     r0, r5
        blx     _objc_release
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Round of applause for ARC please! Notice that it&#8217;s realised we&#8217;re using <code>number</code> outside of the scope of the autorelease pool block so it&#8217;s retained the return value from <code>numberWithInt:</code> just as it did before, but this time it&#8217;s placed the release at the end of the <code>bar</code> function rather than before the autorelease pool is popped. That will have saved us a crash in some code that we might have thought was correct but actually had a subtle memory management bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UIImageOrientation / EXIF orientation sample images</title>
		<link>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 18:18:22 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=491</guid>
		<description><![CDATA[Whilst I was doing some work with images recently I was in desperate need for some sample images which were tagged with the EXIF orientation flag for each of the 8 orientations which are supported by UIImage in its UIImageOrientation metadata. I couldn&#8217;t find any already out there, so I made my own. And now [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst I was doing some work with images recently I was in desperate need for some sample images which were tagged with the EXIF orientation flag for each of the 8 orientations which are supported by UIImage in its <code>UIImageOrientation</code> metadata. I couldn&#8217;t find any already out there, so I made my own. And now I&#8217;m posting them here for anyone who might also find this useful. I now have these saved on my iPhone and use them in apps as test images.</p>
<p><a href='http://iphone.galloway.me.uk/wp-content/uploads/2012/01/EXIF_Orientation_Samples.zip'>Download All</a></p>
<p><span id="more-491"></span></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-225x300.jpg" alt="" title="up" width="225" height="300" class="aligncenter size-medium wp-image-499" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-mirrored-225x300.jpg" alt="" title="up-mirrored" width="225" height="300" class="aligncenter size-medium wp-image-498" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-225x300.jpg" alt="" title="down" width="225" height="300" class="aligncenter size-medium wp-image-493" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-mirrored-225x300.jpg" alt="" title="down-mirrored" width="225" height="300" class="aligncenter size-medium wp-image-492" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-300x225.jpg" alt="" title="left" width="300" height="225" class="aligncenter size-medium wp-image-495" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-mirrored-300x225.jpg" alt="" title="left-mirrored" width="300" height="225" class="aligncenter size-medium wp-image-494" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-300x225.jpg" alt="" title="right" width="300" height="225" class="aligncenter size-medium wp-image-497" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-mirrored-300x225.jpg" alt="" title="right-mirrored" width="300" height="225" class="aligncenter size-medium wp-image-496" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to visualise affine transforms when drawing with Quartz 2D.</title>
		<link>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 18:09:00 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=484</guid>
		<description><![CDATA[I had been struggling with some code that I had to rotate an image whilst drawing it so I decided to sit down and work out a nice way to visualise it as I hadn&#8217;t seen anything out there really that explained it very well. This is what I came up with&#8230; In order to [...]]]></description>
			<content:encoded><![CDATA[<p>I had been <a href="http://stackoverflow.com/questions/9062363/cgcontext-drawing-rotated-around-arbitrary-point">struggling with some code</a> that I had to rotate an image whilst drawing it so I decided to sit down and work out a nice way to visualise it as I hadn&#8217;t seen anything out there really that explained it very well. This is what I came up with&#8230;</p>
<p><span id="more-484"></span></p>
<p>In order to visualise it I decided to create a card with an &#8216;F&#8217; on the front and then trace through the &#8216;F&#8217; onto the back to that effectively it is mirrored. The purpose of the mirror is to enable me to visualise the case when dealing with images that are mirrored (i.e. <code>UIImageOrientationUpMirrored</code>, <code>UIImageOrientationDownMirrored</code>, etc). Here is an example of my card:</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Sample-Card.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Sample-Card.png" alt="" title="Sample-Card" width="400" height="354" class="aligncenter size-full wp-image-487" /></a></p>
<p>Now we can get to work using that to work out how we might want to translate &#038; rotate a context whilst drawing into. Consider first of all an image that is in the <code>UIImageOrientationDown</code> orientation. So hold your card with the &#8216;F&#8217; on it with the front facing you and rotate it through 180 degrees. That is the orientation we want to determine the correct transformation matrix for in this case. Now since Quartz draws with the origin in the bottom right, you need to imagine a coordinate system where the y-axis is going up from the bottom to the top of your card and the x-axis is going across from the left to the right. I like to imagine little arrows going up &#038; left from the bottom left corner of my card.</p>
<p>So to get this to draw into our canvas correctly it&#8217;s pretty clear that we need to rotate by 180 degrees. A rotation will rotate about the origin of our coordinate system so if we purely rotate by 180 degrees then the image will be drawn the right way up but will lie down and left from the origin. To see why this is, hold your card with your fingers at the bottom left. Now imagine the canvas going up and right from here, the size of the card. Now push the card round from the top right corner by 180 degrees, pivoting about the bottom left. You can see that nothing would be drawn into the canvas! So what we need to do is translate up by the height and right by the width. That then brings the image into the right place in the canvas.</p>
<p>I like to draw out this like so on paper:</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Down-Example.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Down-Example.png" alt="" title="Down-Example" width="640" height="452" class="aligncenter size-full wp-image-485" /></a></p>
<p>I think this helps explain it because if at all times you keep your little coordinate arrows on your card lined up with the ones in the diagram, you can visualise what&#8217;s going on.</p>
<p>Now let&#8217;s consider a much more complicated example, the case of <code>UIImageOrientationLeftMirrored</code>. Start with your card on its front and on its side with the long side of the &#8216;F&#8217; at the bottom. Then flip it over. This is now in the desired orientation.</p>
<p>It&#8217;s hard to work out from here what needs to be done but if you follow through the diagram below and at all times keeping your little coordinate arrows on your card aligned with the arrows on the diagram then you should be able to convince yourself that it&#8217;s right.</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Left-Mirrored-Example.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Left-Mirrored-Example.png" alt="" title="Left-Mirrored-Example" width="640" height="452" class="aligncenter size-full wp-image-486" /></a></p>
<p>So to follow this you would hold your card with the &#8216;F&#8217; in the position described above with its bottom left aligned with the bottom left of the canvas &#8217;1&#8242; on the diagram. Then to get to position 2 you would move the card horizontally across the width of the canvas where your little arrows should line up with the arrows in the diagram on canvas &#8217;2&#8242;. To get to position 3 you would rotate 90 degrees counter-clockwise (i.e. positive rotation &#8211; which is rotating from the positive x-axis towards the positive y-axis). Then to get to position 4 you need to move the card vertically, which is a translation in positive x if you look at it carefully. Then finally to get to position 5 you need to flip the card along the y-axis which brings the image into the canvas fully and you&#8217;ll notice the &#8216;F&#8217; is the right way up!</p>
<p>Note that if we want to then draw the image we would need to define the rectangle to draw into with a width equal to the canvas height and a height equal to the canvas width because our x axis is now vertical and our y axis is horizontal. Alternatively think of it that the height of the image is the width of the canvas and the width of the image is the height of the canvas.</p>
<p>I used this method to work out how to draw a <code>UIImage</code> rotated correctly to its upright position. Here is that code:</p>
<pre class="brush: objc; title: ; notranslate">
UIImage *image = &lt;THE_IMAGE&gt;;
CGSize imageSize = image.size;

CGRect imageRect = CGRectIntegral(CGRectMake(0.0f, 0.0f, imageSize.width, imageSize.height));
CGRect imageTransposedRect = CGRectMake(0.0f, 0.0f, imageSize.height, imageSize.width);

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect drawRect = imageRect;

switch (image.imageOrientation) {
    case UIImageOrientationUp:
        break;
    case UIImageOrientationUpMirrored:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        break;
    case UIImageOrientationDown:
        transform = CGAffineTransformTranslate(transform, rect.size.width, rect.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, 0.0f, rect.size.height);
        transform = CGAffineTransformScale(transform, 1.0f, -1.0f);
        break;
    case UIImageOrientationLeft:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        transform = CGAffineTransformTranslate(transform, rect.size.height, 0.0f);
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationRight:
        transform = CGAffineTransformTranslate(transform, 0.0f, rect.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        drawRect = imageTransposedRect;
        break;
}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            drawRect.size.width,
                                            drawRect.size.height,
                                            8,
                                            0,
                                            colorSpace,
                                            kCGImageAlphaPremultipliedLast);

CGContextConcatCTM(bitmap, transform);
CGContextDrawImage(bitmap, drawRect, image);

CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGContextRelease(bitmap);
CGImageRelease(newImageRef);
CGColorSpaceRelease(colorSpace);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look under ARC&#8217;s hood &#8211; Episode 2</title>
		<link>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-2/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-2/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 15:30:31 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=456</guid>
		<description><![CDATA[Following on from my first post about looking at how ARC works under the hood I thought I would share another little snippet that I found interesting. This time I was wondering what happened when you pulled an object out of an array and returned it from a method. Pre-ARC, you would retain the object [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from my first post about looking at <a href="http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/">how ARC works under the hood</a> I thought I would share another little snippet that I found interesting. This time I was wondering what happened when you pulled an object out of an array and returned it from a method. Pre-ARC, you would retain the object then return it autoreleased. With ARC we can get rid of those memory management calls but it just feels wrong. So I decided to check ARC was doing the right thing.</p>
<p><span id="more-456"></span></p>
<p>Consider this class:</p>
<pre class="brush: objc; title: ; notranslate">
#import &lt;Foundation/Foundation.h&gt;

@interface ClassA : NSObject

@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation ClassA

@synthesize array;

- (id)popObject {
    id lastObject = [array lastObject];
    if (lastObject) {
        [array removeLastObject];
    }
    return lastObject;
}

@end
</pre>
<p>In non-ARC land, the call to <code>removeLastObject</code> would release the object that was in the array and then if that was the last reference to it, it would <code>dealloc</code> the object meaning that we return a dead object. So we would retain <code>lastObject</code> and then return it with an autorelease.</p>
<p>But this scared me not doing this, even though I knew full well that ARC should be doing its job. I think I thought this because naively I thought that ARC would parse the method line by line. If it did then I thought it might not necessarily add in a retain because when we get a reference to the last object, ARC doesn&#8217;t know it needs to add a retain because well, why would it necessarily have to?</p>
<p>That&#8217;s where I was wrong. Obviously what ARC does is it will add in a retain once we get a reference to that object and then when that variable goes out of scope it adds a release or in our case, because we are returning it and the method name does not start with <code>new</code> or <code>copy</code>, it autoreleases it.</p>
<p>Let&#8217;s see what that code compiled to:</p>
<pre class="brush: cpp; highlight: [21,34]; title: ; notranslate">
        .thumb_func     &quot;-[ClassA popObject]&quot;
&quot;-[ClassA popObject]&quot;:
        push    {r4, r5, r6, r7, lr}
        movw    r6, :lower16:(_OBJC_IVAR_$_ClassA.array-(LPC0_0+4))
        mov     r4, r0
        movt    r6, :upper16:(_OBJC_IVAR_$_ClassA.array-(LPC0_0+4))
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4))
LPC0_0:
        add     r6, pc
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_1+4))
LPC0_1:
        add     r1, pc
        add     r7, sp, #12
        ldr     r0, [r6]
        ldr     r1, [r1]
        ldr     r0, [r4, r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        mov     r5, r0
        cbz     r5, LBB0_2
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC0_2+4))
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC0_2+4))
        ldr     r0, [r6]
LPC0_2:
        add     r1, pc
        ldr     r1, [r1]
        ldr     r0, [r4, r0]
        blx     _objc_msgSend
LBB0_2:
        mov     r0, r5
        blx     _objc_autoreleaseReturnValue
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Well, there we go. ARC has done a fine job for us. What it&#8217;s actually done is added a call to <code>objc_retainAutoreleaseReturnValue</code> which means it has noticed it needs to retain a value that was returned autoreleased which must be an ARC optimisation that will just pull the object once from the autorelease pool rather than actually performing a retain. Then at the end of the method it calls <code>objc_autoreleaseReturnValue</code> which must do the work of autoreleasing the value we&#8217;re returning.</p>
<p>This is just another example of how to look at what ARC is doing under the hood. The more I use ARC, the more I realise how useful it is. It makes code less prone to memory management errors and allows for optimisations such as the one shown here of retaining an autoreleased return value.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look under ARC&#8217;s hood &#8211; Episode 1</title>
		<link>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 11:12:58 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=443</guid>
		<description><![CDATA[After a conversation on Twitter with @jacobrelkin I decided to write a little post about how ARC works under the hood and how you can go about seeing what it&#8217;s doing. In this post I&#8217;ll explain about how ARC adds in retain, release and autorelease calls accordingly. We shall start by defining a class like [...]]]></description>
			<content:encoded><![CDATA[<p>After a <a href="http://twitter.com/#!/mattjgalloway/status/154478264537194496">conversation on Twitter</a> with <a href="http://twitter.com/jacobrelkin">@jacobrelkin</a> I decided to write a little post about how ARC works under the hood and how you can go about seeing what it&#8217;s doing. In this post I&#8217;ll explain about how ARC adds in <code>retain</code>, <code>release</code> and <code>autorelease</code> calls accordingly. </p>
<p><span id="more-443"></span></p>
<p>We shall start by defining a class like so:</p>
<pre class="brush: objc; title: ; notranslate">
#import &lt;Foundation/Foundation.h&gt;

@interface ClassA : NSObject 

@property (nonatomic, retain) NSNumber *foo;

@end

@implementation ClassA

@synthesize foo;

- (void)changeFooDirect:(NSNumber*)inFoo {
    foo = inFoo;
}

- (void)changeFooSetter:(NSNumber*)inFoo {
    self.foo = inFoo;
}

- (NSNumber*)newNumber {
    return [[NSNumber alloc] initWithInt:10];
}

- (NSNumber*)getNumber {
    return [[NSNumber alloc] initWithInt:10];
}

@end
</pre>
<p>This outlines a few important aspects of ARC including direct access to ivars versus using a setter and how ARC will add autorelease calls when returning an object from a method based on the name of the method.</p>
<p>Let&#8217;s first look at the direct access to ivars versus using a setter. If we compile that code and look at the assembly then we&#8217;ll get an insight into what&#8217;s going on. I decided to use ARMv7 because it&#8217;s easier to understand what&#8217;s going on than x86 (in my opinion anyway!). We can turn ARC on and off with the <code>-fobjc-arc</code> and <code>-fno-objc-arc</code> compiler options. In these examples I&#8217;ve used optimisation level 3 which will mean the compiler will also remove redundant code which we&#8217;re not really interested in and would clog up the understanding (an exercise for the reader is to try without any optimisations and see what it looks like).</p>
<p>So to compile without ARC I used the following command:</p>
<pre class="brush: bash; title: ; notranslate">$ /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk -arch armv7 -fno-objc-arc -O3 -S -o - test-arc.m</pre>
<p>So, let&#8217;s look at <code>changeFooDirect:</code> and <code>changeFooSetter:</code>:</p>
<pre class="brush: cpp; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooDirect:]&quot;
&quot;-[ClassA changeFooDirect:]&quot;:
        movw    r1, :lower16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
        movt    r1, :upper16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
LPC0_0:
        add     r1, pc
        ldr     r1, [r1]
        str     r2, [r0, r1]
        bx      lr

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooSetter:]&quot;
&quot;-[ClassA changeFooSetter:]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
LPC1_0:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}
</pre>
<p>And following straight on, let&#8217;s look at what it looks like with ARC enabled. To do this I used the following command:</p>
<pre class="brush: bash; title: ; notranslate">$ /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk -arch armv7 -fobjc-arc -O3 -S -o - test-arc.m</pre>
<p>Again, we&#8217;re interested at the moment in <code>changeFooDirect:</code> and <code>changeFooSetter:</code>:</p>
<pre class="brush: cpp; highlight: [14]; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooDirect:]&quot;
&quot;-[ClassA changeFooDirect:]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
        mov     r7, sp
        movt    r1, :upper16:(_OBJC_IVAR_$_ClassA.foo-(LPC0_0+4))
LPC0_0:
        add     r1, pc
        ldr     r1, [r1]
        add     r0, r1
        mov     r1, r2
        blx     _objc_storeStrong
        pop     {r7, pc}

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA changeFooSetter:]&quot;
&quot;-[ClassA changeFooSetter:]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
LPC1_0:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}
</pre>
<p>We can instantly see the difference here. The <code>changeFooSetter:</code> is exactly the same whereas <code>changeFooDirect:</code> has changed with a single call to <code>objc_storeStrong</code>. That&#8217;s the interesting bit. If we looks at the <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime.objc_storeStrong">LLVM documentation for this</a> then we see that it&#8217;s doing a standard swap of the variable by releasing the old value and retain the new value. Whereas in the non-ARC version the ivar is just swapped without any retain or release. That&#8217;s just what we&#8217;d expect! Thanks ARC!</p>
<p>Now for the more interesting bit, the <code>newNumber</code> versus <code>getNumber</code>. Those methods in non-ARC land are both returning <code>NSNumber</code> objects which have a retain count of 1, i.e. the caller owns them. That sounds right for <code>newNumber</code> but not for <code>getNumber</code> according to Cocoa&#8217;s naming conventions. We&#8217;d expect to see an <code>autorelease</code> call when returning from <code>getNumber</code>. So let&#8217;s see what the code looks like without ARC:</p>
<pre class="brush: cpp; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA newNumber]&quot;
&quot;-[ClassA newNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
LPC2_0:
        add     r1, pc
LPC2_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
LPC2_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA getNumber]&quot;
&quot;-[ClassA getNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
LPC3_0:
        add     r1, pc
LPC3_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
LPC3_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}
</pre>
<p>And now with ARC:</p>
<pre class="brush: cpp; highlight: [51]; title: ; notranslate">
        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA newNumber]&quot;
&quot;-[ClassA newNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC2_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC2_1+4))
LPC2_0:
        add     r1, pc
LPC2_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC2_2+4))
LPC2_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        pop     {r7, pc}

        .align  2
        .code   16
        .thumb_func     &quot;-[ClassA getNumber]&quot;
&quot;-[ClassA getNumber]&quot;:
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        mov     r7, sp
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC3_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC3_1+4))
LPC3_0:
        add     r1, pc
LPC3_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
        movs    r2, #10
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC3_2+4))
LPC3_2:
        add     r1, pc
        ldr     r1, [r1]
        blx     _objc_msgSend
        blx     _objc_autorelease
        pop     {r7, pc}
</pre>
<p>And look at the single difference &#8211; a <code>blx</code> (this is a method call) to <code>objc_autorelease</code> in <code>getNumber:</code>. That&#8217;s just what we&#8217;d expect from ARC because it&#8217;s noticed that the method name doesn&#8217;t start with <code>new</code> or <code>copy</code> and it knows that at the point of return the <code>NSNumber</code> has a retain count of 1 so it adds in an <code>autorelease</code> call. Excellent!</p>
<p>This has just shown a little insight into how ARC works in two circumstances and I hope it inspires the reader to go away and look for themselves into how ARC works rather than just taking it for granted. It&#8217;s important as a programmer to understand how your tools work.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/a-look-under-arcs-hood-episode-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is it with people and free apps?</title>
		<link>http://iphone.galloway.me.uk/2011/11/what-is-it-with-people-and-free-apps/</link>
		<comments>http://iphone.galloway.me.uk/2011/11/what-is-it-with-people-and-free-apps/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 09:24:20 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[iPhone Apps]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=435</guid>
		<description><![CDATA[So we have just released a new version of BeerMap which is an iPhone app that I created with a couple of friends. It&#8217;s had a long life and has changed significantly in this latest update because, to be honest, it was quite confusing to use. We didn&#8217;t really have a plan for it before [...]]]></description>
			<content:encoded><![CDATA[<p>So we have just released a new version of <a href="http://www.beermap.co">BeerMap</a> which is an <a href="http://itunes.apple.com/gb/app/beermap/id313367328?mt=8">iPhone app</a> that I created with a couple of friends. It&#8217;s had a long life and has changed significantly in this latest update because, to be honest, it was quite confusing to use. We didn&#8217;t really have a plan for it before but now we do and we&#8217;re executing it step by step.</p>
<p>Since 2.2 went live we&#8217;ve seen a lot more uptake of the app with more people reviewing beers and even some of the social media integration being used in the app which is exactly what we wanted. We wanted to push the app in a more social direction rather than just reviewing beers and pubs. If you&#8217;re simply reviewing beers and pubs then you need critical mass of users before any of that data becomes usable. So opting for the more Twitter-like style of a review now being a &#8220;Taste&#8221; and having a timeline of realtime &#8220;Tastes&#8221; coming in, the idea becomes like a way to tell the world what you think about the beer you&#8217;re drinking right now. We kept the idea of pubs (or places as we call them, because it might be a pub, a bar, a beer festival, etc) but you cannot actually review a pub. This serves the purpose of making the app simpler to understand because there&#8217;s just one first class citizen and that&#8217;s the &#8220;Taste&#8221;.</p>
<p>But, I&#8217;m sad now. Why is it that people decide for themselves what an app should do? Why is it that people writing reviews for apps cannot be constructive? Why is it that people think that free apps should be 100% perfect straight away? Here&#8217;s a review we got a day after 2.2 came out:</p>
<blockquote><p>[1 star] &#8211; Why hasn&#8217;t beerintheevening got an app? This is not a satisfactory substitute &#8211; it neither adequately provides user reviews of pubs nor does it review beers. FAIL.</p></blockquote>
<p>This was coupled with a support email request which goes further to complain about the app and how they failed to read the instructions for adding a new pub (it&#8217;s really not that complicated).</p>
<p>We&#8217;ll see what the future holds for BeerMap. I sincerely hope that it&#8217;s good things because I think that there&#8217;s a definite use for an app like this, especially in the UK where beer can vary between parts of the country so you want a way to find a pub that&#8217;s great.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/11/what-is-it-with-people-and-free-apps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GiffGaff: Great for iPad!</title>
		<link>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/</link>
		<comments>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 14:32:51 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=432</guid>
		<description><![CDATA[I&#8217;ve heard about GiffGaff from a lot of people but it was only when I realised how good it would be for my iPad that I decided to give it a go. So I got a SIM, topped up £10 and now I can spend just 20p each day and get 20MB of data! It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve heard about <a href="http://giffgaff.com/orders/affiliate/mattjgalloway_ipad">GiffGaff</a> from a lot of people but it was only when I realised how good it would be for my iPad that I decided to give it a go. So I got a SIM, topped up £10 and now I can spend just 20p each day and get 20MB of data! It&#8217;s great for my iPad because I can use it whilst out and about and just pay for each day that I want mobile data, which is quite rare for me really.</p>
<p>So if you&#8217;ve got an iPad and want cheap data for it then go <a href="http://giffgaff.com/orders/affiliate/mattjgalloway_ipad">grab a GiffGaff SIM</a> and away you go!</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[UPDATED - Not a bug!] Another interesting compiler bug (in Apple&#8217;s LLVM this time)</title>
		<link>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/</link>
		<comments>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 13:57:09 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=419</guid>
		<description><![CDATA[I came across another interesting compiler bug today. I&#8217;m not going to go into too much detail about it, but the problem code is this: To break that down a bit, the function is doing this: Initialise a couple of variables and set them to 0. Then perform some inline assembly using the variables. This [...]]]></description>
			<content:encoded><![CDATA[<p>I came across another interesting compiler bug today. I&#8217;m not going to go into too much detail about it, but the problem code is this:</p>
<pre class="brush: cpp; title: ; notranslate">
void a() {
    int a = 0;
    int b = 0;
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=r&quot;(a), &quot;+m&quot;(b)
           );
}
</pre>
<p>To break that down a bit, the function is doing this:</p>
<ol>
<li>
<pre class="brush: cpp; title: ; notranslate">
    int a = 0;
    int b = 0;
</pre>
<p>Initialise a couple of variables and set them to 0.
	</li>
<li>
<pre class="brush: cpp; title: ; notranslate">
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=r&quot;(a), &quot;+m&quot;(b)
           );
</pre>
<p>Then perform some inline assembly using the variables. This just puts 0 into the register that will hold our &#8216;a&#8217; variable, then loads the value of &#8216;b&#8217; into &#8216;a&#8217;, then sets &#8216;b&#8217; to 0.
	</li>
</ol>
<p>This is of course a contrived example, but it illustrates the bug. The output assembly from LLVM-GCC or clang is (for ARM architecture):</p>
<pre class="brush: plain; title: ; notranslate">
        .globl  _a
        .align  2
        .code   16
        .thumb_func     _a
_a:
        sub     sp, #8
        movs    r0, #0
        movt    r0, #0
        str     r0, [sp, #4]
        str     r0, [sp]
        mov     r0, sp
        @ InlineAsm Start

        mov r0, 0
        ldr r0, [r0]
        mov [r0], 0

        @ InlineAsm End
        str     r0, [sp, #4]
        add     sp, #8
        bx      lr
</pre>
<p>The interesting bit is the inline assembly. You&#8217;ll notice that it&#8217;s doing something very stupid. It&#8217;s choosing the same register for both operands (it&#8217;s choosing r0). This is completely wrong, and will lead to a runtime crash in this case due to the dereference of 0.</p>
<p>I did a bit of hunting and it appears to be a problem in generating the LLVM bytecode as the problem manifests itself before the LLVM bytecode is compiled down into instructions, like so:</p>
<pre class="brush: plain; title: ; notranslate">
define void @a() nounwind ssp {
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4
  store i32 0, i32* %b, align 4
  %1 = call i32 asm &quot;&#92;&#48;A&#92;&#48;9mov $0, 0&#92;&#48;A&#92;&#48;9ldr $0, $1&#92;&#48;A&#92;&#48;9mov $1, 0&#92;&#48;A&quot;, &quot;=r,=*m,*m&quot;(i32* %b, i32* %b) nounwind, !srcloc !0
  store i32 %1, i32* %a, align 4
  ret void
}
</pre>
<p>You can see here that the &#8216;%a&#8217; (i.e. variable &#8216;a&#8217;) is never referenced, only &#8216;%b&#8217; (i.e. variable &#8216;a&#8217;). This is not what we&#8217;d expect at all given we&#8217;re referencing both variables in the code.</p>
<p>I found this quite interesting <img src='http://iphone.galloway.me.uk/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p><strong>[Update]</strong></p>
<p>I&#8217;ve actually found out that this isn&#8217;t a bug! That&#8217;s good news, right? It&#8217;s quite a subtle thing, but the heart of the problem can be explained after understanding the <a href="http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Modifiers.html#Modifiers">modifiers to operands</a> on inline assembly. The problem is that we&#8217;re not specifying that &#8216;a&#8217; is <em>clobbered early</em>. In the assembly, we&#8217;re writing to it before reading we&#8217;ve finished using all input operands (only &#8216;b&#8217; in this case) so we&#8217;re meant to mark it like that. It&#8217;s just luck that GCC does the right thing &#8211; Apple&#8217;s LLVM is doing the right thing and using less registers!</p>
<p>So the correct code is this:</p>
<pre class="brush: cpp; title: ; notranslate">
void a() {
    int a = 0;
    int b = 0;
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=&amp;r&quot;(a), &quot;+m&quot;(b)
           );
}
</pre>
<p>Which results in the following assembly:</p>
<pre class="brush: plain; title: ; notranslate">
        .globl  _a
        .align  2
        .code   16
        .thumb_func     _a
_a:
        sub     sp, #8
        movs    r0, #0
        movt    r0, #0
        str     r0, [sp, #4]
        str     r0, [sp]
        mov     r0, sp
        @ InlineAsm Start

        mov r1, 0
        ldr r1, [r0]
        mov [r0], 0

        @ InlineAsm End
        str     r1, [sp, #4]
        add     sp, #8
        bx      lr
</pre>
<p>And the following LLVM:</p>
<pre class="brush: plain; title: ; notranslate">
define void @a() nounwind ssp {
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4
  store i32 0, i32* %b, align 4
  %1 = call i32 asm &quot;&#92;&#48;A&#92;&#48;9mov $0, 0&#92;&#48;A&#92;&#48;9ldr $0, $1&#92;&#48;A&#92;&#48;9mov $1, 0&#92;&#48;A&quot;, &quot;=&amp;r,=*m,*m&quot;(i32* %b, i32* %b) nounwind, !srcloc !0
  store i32 %1, i32* %a, align 4
  ret void
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

