<?xml version="1.0" encoding="utf-8" standalone="no"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Cocoatoa</title>
  <link href="http://www.cocoatoa.com/"/>
  <link rel="self" href="http://www.cocoatoa.com/feed/index.xml"/>
  <icon>http://www.cocoatoa.com/static/images/favicon.png</icon>
  <updated>2010-05-29T00:00:00+00:00</updated>
  <id>urn:uuid:9F4A67A2-C573-43DA-8901-4124FBF4863C</id>
  <author>
    <name>Tim Wood</name>
  </author>
  
<entry>
  <title>Kitchen Tiles</title>
  <link href="http://www.cocoatoa.com/posts/2010/05/29/kitchen_tiles/"/>
  <published>2010-05-29T00:00:00+00:00</published>
  <updated>2010-05-29T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2010/05/29/kitchen_tiles/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>We&#8217;re in the midst of planning a kitchen remodel, which will include a tile backsplash. The stone stairs out the kitchen to the back patio, which we had rebuilt a couple years ago have shards of our pottery embedded in them. Now that we have a studio at home, it seems like we should include some tiles in our kitchen too!</p>

	</div>
  </content>
</entry>

<entry>
  <title>Private API is the Wrong Question</title>
  <link href="http://www.cocoatoa.com/posts/2010/04/07/private_api_is_the_wrong_question/"/>
  <published>2010-04-07T00:00:00+00:00</published>
  <updated>2010-04-07T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2010/04/07/private_api_is_the_wrong_question/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>With the release of iPad, Apple&#8217;s conformance to public API in their App Store offerings has come under scrutiny. iWork is lauded for using only public API, while more recently, <a href="http://www.marco.org/500743718">iBooks has been called out</a> for lacking such purity. As with many topics, arguing about whether Apple should use private API not only doesn&#8217;t have a simple answer, but it is the wrong question.</p>

<p>What&#8217;s comforting about the black or white phrasing is that it seemingly has an equally plain answer. While over simplification is a sure fire way to stir up bloggers, it avoids important issues. Looking at iWork for iPad, you might start to wonder about a more basic question: what counts as private API?</p>

<p>Building iWork using only public API is a huge achievement. I cannot even guess at how much work my friends at Apple put into accomplishing this, on top of making some incredible applications. Thank you!</p>

<p>Evolving public API has both costs to Apple (development, QA, maintenance, delays in important revenue generating events like, say, iPad) and it has benefits (allowing third-party developers to fill niche needs, making the platform more compelling to customers). iPhone OS 3.2 makes important strides, but shortchanges some critical factors: platform consistency and wasted effort. Consider these framework-worthy features, common across iWork:</p>

<ul>
<li>Rich text editing</li>
<li>Document browser</li>
<li>Document export/import</li>
</ul>

<p>Rich text editing is made <em>feasible</em> in iPhone OS 3.2 by the addition of CoreText, but just barely. Writing a real text editor atop CoreText is a large amount of work. It is difficult, and painstaking to exactly mimic all the little behaviors that aren&#8217;t addressed by CoreText at all (bi-di cursor movement, selection display and editing, style editing, fast multi-paregraph editing, embedded attachments, &#8230;). The document picker also has a wide range of common behaviors like open/close animations, custom scroll page snapping, preview rendering, and common actions. The import/export sheet has controls for selecting file types, and querying, listing and operating on the shared documents.</p>

<p>So, here we have a blob of common code that helps define the platform and I can&#8217;t get to it. This sounds like private API to me, even if it isn&#8217;t actually in the pile labelled &#8220;UIKit&#8221;. Instead of finishing this and making it available, the code is seemingly included directly in each of the iWork applications. The claim of iWork using 100% public API comes off a bit like a kid finishing cleaning their room by sweeping the last pile of junk under the bed. Yeah, it looks better for when company arrives, but it&#8217;ll generate more work overall.</p>

<p>By not finishing this API, Apple has left those of us needing similar features to either choose to build slightly different subsets of the features (and all get them differently wrong), or just drop the features. I can only guess as to what went into these decisions, so I won&#8217;t presume to say this choice was wrong. But, I do know that it is sad to hear the &#8220;100% public&#8221; claim, given how much we had to re-implement (and best case: only to throw it away for iPhone OS 4.0).</p>

<h1>Stop whining, you!</h1>

<p>Particularly on iPhone and iPad, it seems like there is a potential approach to improvement.</p>

<p>Consider that there is only one app running at a time (modulo Apple&#8217;s background apps and whatever announcements happen tomorrow with iPhone OS 4.0), there is no bundle loading and the developer submitting the app controls all the code above the frameworks. In all, this means it is pretty easy to version &#8220;mini SDKs&#8221; without symbol conflicts.</p>

<p>Let&#8217;s say that iWork for iPad 1.0 has a real CoreText-based editor widget, and while maybe it doesn&#8217;t have perfect API, it is at least the best on the platform! Apple could ship a static library, headers and some resources for these semi-baked APIs. The mini SDK would use only real public APIs, and maybe they would have some cross dependencies on each other (not great, but still OK). When iWork 1.1 arrives, maybe there is a new set of mini SDKs shipped and that&#8217;s fine: all the existing third-party apps would continue to work just fine until they chose to update.</p>

<p>Out of this Apple would get a more consistent platform for its users, external feedback on API before it becomes frozen, and higher quality third party apps by allowing developers to spend time on non-framework development &#8230; and I could spend less time writing code to throw away later.  Just sayin&#8217; &#8230;</p>

<p>Please reference <a href="rdar://problem/7839960">Radar #7839960</a> if you agree!</p>

	</div>
  </content>
</entry>

<entry>
  <title>Dtrace Probe for Global Collector Promotion</title>
  <link href="http://www.cocoatoa.com/posts/2009/12/09/dtrace_probe_for_global_collector_promotion/"/>
  <published>2009-12-09T00:00:00+00:00</published>
  <updated>2009-12-09T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/12/09/dtrace_probe_for_global_collector_promotion/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>I&#8217;m working through my list of Radars to file, can you tell?</p>

<p>In 10.6 we have the snazzy new thread-local collector, which promises to increase the performance of threaded &amp; collected apps. Part of this is machinery in libauto/CF to promote blocks out of the thread-local collector to the global collector when a write barrier detects a reference from global memory to something that was previously thread-local.</p>

<p>Faster apps sound great, but so far it is hard for me to tell how much thread-local collection buys or even how well I&#8217;m using it. This would be easier to check if there was a dtrace probe to detect the local&#x2192;global promotion and an Instruments tool to show hot spots of objects escaping from thread-local collection.</p>

<p>In the <a href="http://www.opensource.apple.com/source/autozone/autozone-141.1/">source to autozone</a>, we can see an inline in <code>AutoSubzone</code>:</p>

<div class="pygment"><pre><span class="kr">inline</span> <span class="kt">void</span> <span class="nf">make_global</span><span class="p">(</span><span class="n">usword_t</span> <span class="n">q</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">assert</span><span class="p">(</span><span class="o">!</span><span class="n">is_garbage</span><span class="p">(</span><span class="n">q</span><span class="p">));</span>
    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">data</span> <span class="o">=</span> <span class="n">_side_data</span><span class="p">[</span><span class="n">q</span><span class="p">];</span>
    <span class="n">data</span> <span class="o">&amp;=</span> <span class="o">~</span><span class="p">(</span><span class="n">refcount_mask</span> <span class="o">|</span> <span class="n">age_mask</span><span class="p">);</span>
    <span class="n">data</span> <span class="o">|=</span> <span class="n">global_bit</span> <span class="o">|</span> <span class="p">(</span><span class="n">youngest_age</span> <span class="o">&lt;&lt;</span> <span class="n">age_mask_log2</span><span class="p">);</span>
    <span class="n">_side_data</span><span class="p">[</span><span class="n">q</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">;</span>
    <span class="n">AUTO_PROBE</span><span class="p">(</span><span class="n">auto_probe_make_global</span><span class="p">(</span><span class="n">quantum_address</span><span class="p">(</span><span class="n">q</span><span class="p">),</span> <span class="n">youngest_age</span><span class="p">));</span>
<span class="p">}</span>
</pre></div>

<p>Sadly, <code>AUTO_PROBE</code> here is for the testing infrastructure within autozone and isn&#8217;t exposed as a dtrace probe.</p>

<p>Feel free to reference Radar #7459539 if you&#8217;d also like to see this in Instruments!</p>

	</div>
  </content>
</entry>

<entry>
  <title>Modernizing the Responder Chain</title>
  <link href="http://www.cocoatoa.com/posts/2009/12/08/modernizing_the_responder_chain/"/>
  <published>2009-12-08T00:00:00+00:00</published>
  <updated>2009-12-08T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/12/08/modernizing_the_responder_chain/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>Cocoa&#8217;s responder chain has served us over the decades, but it is time to modernize and make it more flexible.</p>

<p>New Cocoa developers have a hard time learning how the responder chain works, where they can or should place their actions and validation methods, and how to debug things when they go wrong. But even experienced developers can run into difficulty with even modestly interesting interfaces. Say you have a window with a toolbar, a sidebar view and a content view. Further, say you have your code nicely factored into controllers for the sidebar and content. Now, if the first responder is in a search field in the toolbar, your sidebar and content views and controllers aren&#8217;t in the responder chain.  If they have actions they can perform any time (ex: &#8220;New Collection&#8221;), you may end up having to put version of <em>each</em> of those actions, and menu validation, on your window controller to forward to the appropriate pane&#8217;s controller.</p>

<p>Here I&#8217;ll propose some possible solutions to these problems, though I&#8217;m sure the AppKit folks could improve on them.</p>

<p>The current state of affairs is that <code>NSApplication</code> has <code>-targetForAction:to:from:</code> which does a pre-defined scan of the responder chain, attempting to find a target that responds to the action&#8217;s selector. If there is a target found, callers might then perform some validation via one of many schemes that have been piled on over the years. Some targets might call out to delegates with things like <code>-textView:doCommandBySelector:</code>. Also, some delegates (but only those blessed by AppKit) are given a chance to participate directly in the responder search, even though they are not <code>NSResponders</code>! For extra fun, <code>NSError</code> presentation has a different responder chain than normal target/action, though I&#8217;m going to ignore that here.</p>

<p>This has left us with a bunch of concepts intermingled in ways that aren&#8217;t terribly flexible. Instead, let&#8217;s break down the target/action responder chain into more orthogonal pieces:</p>

<ul>
<li>Walking the responder chain in an extensible fashion</li>
<li>Querying targets for whether they want responsibility for an action</li>
<li>Validating the action</li>
<li>Performing the action</li>
</ul>

<p><br/></p>

<h1>Iterating the Responder Chain</h1>

<p>Imagine the following API:</p>

<div class="pygment"><pre> <span class="k">typedef</span> <span class="kt">BOOL</span> <span class="p">(</span><span class="o">^</span><span class="n">OAResponderChainApplier</span><span class="p">)(</span><span class="kt">id</span> <span class="n">target</span><span class="p">);</span>

 <span class="c">// ... on NSApplication</span>
 <span class="o">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nl">applyToFullResponderChain:</span><span class="p">(</span><span class="n">OAResponderChainApplier</span><span class="p">)</span><span class="n">applier</span><span class="p">;</span>

 <span class="c">// ... on NSObject</span>
 <span class="o">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nl">applyToResponderChain:</span><span class="p">(</span><span class="n">OAResponderChainApplier</span><span class="p">)</span><span class="n">applier</span><span class="p">;</span>
</pre></div>

<p>The <code>NSObject</code> method would default to simply calling the applier block and returning its result, with <code>YES</code> signaling to continue the traversal. Subclasses could then extend this to consider extra objects. For example, <code>NSApplication</code> would first call super and then if that returns <code>YES</code>, it would call it on its delegate if it had one. <code>NSResponder</code> would consider its <code>nextResponder</code>, <code>NSWindow</code> its <code>delegate</code>/<code>windowController</code>, and so on. This allows each object to decide who else it will include in the responder chain (and in what order).</p>

<p>The <code>NSApplication</code> method would perform the entry points for the full search of the responder chain (<code>keyWindow</code>&#8217;s first responder, <code>mainWindow</code>&#8217;s, &#8230;)</p>

<p>Given these few changes, you can very simply log every object that will be considered:</p>

<div class="pygment"><pre><span class="p">[</span><span class="n">NSApp</span> <span class="nl">applyToFullResponderChain:</span><span class="o">^</span><span class="kt">BOOL</span><span class="p">(</span><span class="kt">id</span> <span class="n">target</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;%@ %p&quot;</span><span class="p">,</span> <span class="n">NSStringFromClass</span><span class="p">([</span><span class="n">target</span> <span class="n">class</span><span class="p">]),</span> <span class="n">target</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">YES</span><span class="p">;</span> <span class="c">// continue;</span>
<span class="p">}];</span>
</pre></div>

<p><br/></p>

<h1>Determining Responsibility</h1>

<p>Now, once we have a nice way to iterate the responder chain, lets look at the issue of picking the responsible target.  <code>-targetForAction:to:from:</code>, when passed a <code>nil</code> target, will search until it finds something in the responder chain that implements the action.  That is, responsibility is defined as <code>-respondsToSelector:</code> &#8212; not entirely unreasonable, but not very flexible.</p>

<p>Once the first implementor is found, the search is called off, and validation begins. There is no provision for distinguishing between implementing a selector and taking responsibility for it, given the situation at hand (selection, availability of some resource, etc). Stated another way, the <code>-validate*</code> methods only have a <code>YES</code> and <code>NO</code> response &#8212; there is no <code>WhatAreYouTalkingAboutThatIsNotMyProblem</code> response.</p>

<p>Let&#8217;s define another new method on <code>NSObject</code>:</p>

<div class="pygment"><pre>  <span class="o">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nl">responsibleTargetForAction:</span><span class="p">(</span><span class="kt">SEL</span><span class="p">)</span><span class="n">action</span> <span class="nl">sender:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="n">sender</span><span class="p">;</span>
</pre></div>

<p>This returns <code>nil</code> if it doesn&#8217;t want to declare the responsible target, or it returns non-<code>nil</code> if it does (probably <code>self</code>).  <code>-targetForAction:to:from:</code>, for the <code>nil</code> candidate target case, then has an applier block that looks like:</p>

<div class="pygment"><pre><span class="n">__block</span> <span class="kt">id</span> <span class="n">target</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>

<span class="n">OAResponderChainApplier</span> <span class="n">applier</span> <span class="o">=</span> <span class="o">^</span><span class="p">(</span><span class="kt">id</span> <span class="n">object</span><span class="p">){</span>
    <span class="kt">id</span> <span class="n">responsible</span> <span class="o">=</span> <span class="p">[</span><span class="n">object</span> <span class="nl">responsibleTargetForAction:</span><span class="n">theAction</span> <span class="nl">sender:</span><span class="n">sender</span><span class="p">];</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">responsible</span><span class="p">)</span> <span class="p">{</span>
        <span class="c">// Someone claimed to be responsible for the action.  The sender will</span>
        <span class="c">// re-validate with any appropriate means and might still get refused,</span>
        <span class="c">// but we should stop iterating.</span>
        <span class="n">target</span> <span class="o">=</span> <span class="n">responsible</span><span class="p">;</span>
        <span class="k">return</span> <span class="n">NO</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">YES</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>

<p>In the sample project, I define this is a semi-ugly way: check <code>-respondsToSelector:</code> and then based on the <code>sender</code> call the appropriate <code>-validate*</code> method. This means that a <code>NO</code> response from <code>-validateMenuItem:</code> takes on the &#8220;not my problem&#8221; behavior by default. After we return the object, the validation will be reapplied by AppKit (which is ugly but shouldn&#8217;t really hurt anything).</p>

<p>This still doesn&#8217;t quite split up determining responsibility from validation, but I&#8217;m sure Apple could do better with deeper hooks into AppKit. The interesting bit here is that this gives us all three choices we are looking for:</p>

<ul>
<li>I&#8217;m responsible and the action can proceed</li>
<li>I&#8217;m responsible and the action is blocked</li>
<li>That&#8217;s not my problem. Go find someone else to bother.</li>
</ul>

<p><br/></p>

<h1>Sample Code</h1>

<p>Here is a project that implements some of these ideas: <a class="xcode" href="/posts/2009/12/08/modernizing_the_responder_chain/OATargetSelection.zip">OATargetSelection</a></p>

<p>Obviously this implementation is a hack, overriding AppKit methods and re-implementing the responder chain.  This also doesn&#8217;t handle the <code>NSViewController</code> absence in the responder chain, though between this and ObjC 2.0 associated storage, it might be possible to pile another hack atop this to let views know their view controllers and thus include them in the search as if they were a delegate. Really, I&#8217;d rather Apple solved both these problems.</p>

<p><br/></p>

<h1>Further Crazy Ideas</h1>

<p>Finally, another big problem with the current target/action scheme is the implementation distance between the action&#8217;s code and its validation.  Both bits of code typically start start out the same, computing some information about the selection or other state. A trivial case might look like:</p>

<div class="pygment"><pre><span class="o">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nl">validateMenuItem:</span><span class="p">(</span><span class="n">NSMenuItem</span> <span class="o">*</span><span class="p">)</span><span class="n">menuItem</span><span class="p">;</span>
<span class="p">{</span>
    <span class="kt">SEL</span> <span class="n">action</span> <span class="o">=</span> <span class="p">[</span><span class="n">menuItem</span> <span class="n">action</span><span class="p">];</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">action</span> <span class="o">==</span> <span class="k">@selector</span><span class="p">(</span><span class="nl">delete:</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">NSArray</span> <span class="o">*</span><span class="n">selection</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span> <span class="n">selectedItems</span><span class="p">];</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">selection</span> <span class="n">count</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="p">...</span>
<span class="p">}</span>

<span class="o">-</span> <span class="p">(</span><span class="kt">IBAction</span><span class="p">)</span><span class="nl">delete:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="n">sender</span><span class="p">;</span>
<span class="p">{</span>
    <span class="n">NSArray</span> <span class="o">*</span><span class="n">selection</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span> <span class="n">selectedItems</span><span class="p">];</span>

    <span class="p">...</span>
<span class="p">}</span>
</pre></div>

<p><br/>
But imagine that we made the selector just a key used to find an &#8220;action&#8221;, and that an action could validate itself.  Instead of the <code>-responsibleTargetForAction:sender:</code> I proposed above, maybe we&#8217;d instead have this:</p>

<div class="pygment"><pre><span class="k">typedef</span> <span class="kt">BOOL</span> <span class="p">(</span><span class="o">^</span><span class="kt">IBAction</span><span class="p">)(</span><span class="kt">id</span> <span class="n">sender</span><span class="p">,</span> <span class="kt">BOOL</span> <span class="n">justValidate</span><span class="p">);</span>

<span class="o">-</span> <span class="p">(</span><span class="kt">IBAction</span><span class="p">)</span><span class="nl">actionForSelector:</span><span class="p">(</span><span class="kt">SEL</span><span class="p">)</span><span class="n">sel</span> <span class="nl">sender:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="n">sender</span><span class="p">;</span>
<span class="p">{</span>
    <span class="c">// iterate the responder chain for someone claiming responsibility for the action or nil if none is found.</span>
<span class="p">}</span>
<span class="p">...</span>

<span class="o">-</span> <span class="p">(</span><span class="kt">IBAction</span><span class="p">)</span><span class="nl">delete:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="n">sender</span><span class="p">;</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="o">^</span><span class="kt">BOOL</span><span class="p">(</span><span class="kt">id</span> <span class="n">sender</span><span class="p">,</span> <span class="kt">BOOL</span> <span class="n">justValidate</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">NSArray</span> <span class="o">*</span><span class="n">selection</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span> <span class="n">selectedItems</span><span class="p">];</span>
        <span class="n">NSUInteger</span> <span class="n">selectionCount</span> <span class="o">=</span> <span class="p">[</span><span class="n">selection</span> <span class="n">count</span><span class="p">];</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">selectionCount</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">justValidate</span><span class="p">)</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">selectionCount</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">);</span>

        <span class="c">// perform the action</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>

<p>Now the formulation above has many, many flaws, but I think there is some promise in the general idea. Allowing the validation and the implementation to defined more closely together would be really nice:</p>

<ul>
<li>Less chance of the two prefix snippets getting out of sync, resulting in incorrect validation</li>
<li>Less chance of dangling validation checks handing out when the an action gets renamed or removed</li>
<li>Possibly less code overall just be getting rid of duplicated lines.</li>
</ul>

<p><br/></p>

<h1>Closing</h1>

<p>If you&#8217;d find something like this useful too, feel free to dup my Radar, #7455939.  Thanks!</p>

<p><br/>
<br/></p>

	</div>
  </content>
</entry>

<entry>
  <title>Nanodrawmo Day 1</title>
  <link href="http://www.cocoatoa.com/posts/2009/11/01/nanodrawmo_day_1/"/>
  <published>2009-11-01T00:00:00+00:00</published>
  <updated>2009-11-01T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/11/01/nanodrawmo_day_1/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>I&#8217;m giving Steve Frank&#8217;s <a href="http://stevenf.tumblr.com/post/219998282/neven-and-gus-both-expressed-interest-in">nanodrawmo</a> a try this November, but in pottery rather than drawing. Producing 50 interesting works in one month is unlikely given drying, glazing and firing times, but I&#8217;ll just give it a go and see how far I get. As I&#8217;ve not been in the studio much lately, the goal of practicing more will be met no matter what!</p>

<p>Today I made two leaf-pressed bowls and thought I&#8217;d do a writeup of the first one. I doubt I&#8217;ll document many of the rest of my entries in as much detail, but figured I&#8217;d do one for the first day.</p>

	</div>
  </content>
</entry>

<entry>
  <title>GCD and Blocking Kernel Calls</title>
  <link href="http://www.cocoatoa.com/posts/2009/09/09/gcd_and_blocking_kernel_calls/"/>
  <published>2009-09-09T00:00:00+00:00</published>
  <updated>2009-09-09T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/09/09/gcd_and_blocking_kernel_calls/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>One of the great features of <a href="http://developer.apple.com/mac/articles/cocoa/introblocksgcd.html">Grand Central Dispatch</a> is that it can make thread usage decisions on a system-wide basis. Without GCD, running several threaded applications can lead to lower performance due to extra context switching. On the other end of the spectrum, pessimistically threaded applications might under utilize your available cores. Achieving a perfect balance in the presence of a complex workload spread across multiple processes was impossible.</p>

<p>NSOperationQueue, which now uses GCD as its work horse, recommends that you leave its maximum concurrent operation count on the default setting, but you can limit the width of the queue manually if you want. NSOperations can have dependencies and priorities, and between these you might think that the operations will simply get scheduled to threads in priority order, once all their dependencies are satisfied. And, you&#8217;d be correct as far as that goes, but GCD is more clever than that!</p>

<p>Consider a silly case like:</p>

<div class="pygment"><pre>  <span class="n">dispatch_apply</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="n">queue</span><span class="p">,</span> <span class="o">^</span><span class="p">(</span><span class="n">size_t</span> <span class="n">i</span><span class="p">){</span>
        <span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
    <span class="p">});</span>
</pre></div>

<p>If GCD <em>just</em> followed the dependency, priority and <em>strictly</em> limited the number of threads to the core count, this could fill up all the threads and effectively block GCD from completing any work. System-wide services shouldn&#8217;t be this easily b0rked. Luckily, GCD has magical powers that seem to let it know when a thread performs a blocking kernel call, making a core available.  GCD will then spin up another operation thread, leaving you with more blocks in flight than you have cores!</p>

<p>On the whole, this seems great, but in specific cases it may bite developers that don&#8217;t expect it. In my real code, I&#8217;m processing a couple thousand compressed files. At the top of each block, I was opening one compressed file and reading its contents. These I/O calls are blocking and would let GCD spin up more operations. Having 1000 threads all fire up and allocate all the memory in the system wedged my machine for a while and confused me since I thought GCD was supposed to handle this.</p>

<p>It just turns out that GCD has more magic than I expected.</p>

<p>I&#8217;m not yet sure how I should best address this issue. GCD has support for monitoring readability of non-blocking file descriptors via <code>dispatch_source_create</code> which might serve, or I might create a separate queue, with a fixed concurrent operation count, for the reading portion of the operation. Suggestions welcome!</p>

<p>In retrospect, this tie between GCD and the kernel is obviously the right thing, and was hinted at WWDC, but I&#8217;ve not yet seen it specifically documented (pointers to such docs also welcome).</p>

	</div>
  </content>
</entry>

<entry>
  <title>Incremental Writing in Snow Leopard's NSFileWrapper</title>
  <link href="http://www.cocoatoa.com/posts/2009/08/30/incremental_writing_in_snow_leopard_s_nsfilewrapper/"/>
  <published>2009-08-30T00:00:00+00:00</published>
  <updated>2009-08-30T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/08/30/incremental_writing_in_snow_leopard_s_nsfilewrapper/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>In my post about some of <a href="http://cocoatoa.com/posts/2009/08/28/snow_leopard_a_bargain_at_twice_the_price/">my favorite Snow Leopard features</a>, I mentioned incremental writing in <code>NSFileManager</code>. This was a typo, of course, and I meant <code>NSFileWrapper</code>. As penance, I thought I&#8217;d go into a bit more detail.</p>

<p>First, a quick test for you to try under 10.6:</p>

<ul>
<li>Create a new RTFD document in TextEdit.</li>
<li>Drag in some large file, making sure to select &#8220;Copy&#8221; when prompted.</li>
<li>Save it. Wait for the spinning wait cursor&#8230;</li>
<li>Edit the text a bit.</li>
<li>Save again and <em>OMG</em> it is already done!</li>
</ul>

<p>If you try this under 10.5, the second save will be just as slow as the first. Worse, this pause will occur during autosave, disrupting your editing! Under 10.6, though, the file wrapper can reuse the parts of the on-disk representation that have not changed.</p>

<p>Let&#8217;s look a bit deeper. Having a such a document already set up, open Instruments, and select the &#8220;File Activity&#8221; template. Attach to TextEdit and sample an iteration of the edit/save cycle.  Selecting the &#8220;Directory I/O&#8221; Instrument, you&#8217;ll see something like this line:</p>

<p><img class="figure" alt="Instruments" src="/posts/2009/08/30/incremental_writing_in_snow_leopard_s_nsfilewrapper/Instruments.png"/></p>

<p>Note the selected line above: <code>NSFileWrapper</code> is making a call to the BSD function <a href="x-man-page://2/link"><code>link</code></a>. When creating the new directory tree representing the updated wrapper, <code>NSFileWrapper</code> creates a second reference to the unmodified attachment and then writes only the modified &#8220;TXT.rtf&#8221; file! This &#8220;hard link&#8221; is a long-time Unix filesystem feature that allows the same file to be referenced by more than one directory entry. In recent versions of HFS+, in support of Time Machine, even a directory may be referenced by more than one parent (though <a href="http://lists.apple.com/archives/darwin-dev/2007/Dec/msg00031.html">you should not be too nosy</a> about this).</p>

<p>But &#8220;Wait!&#8221;, you say. &#8220;What if some clever user has edited that attachment externally?&#8221; Say I were to pop into <code>Terminal</code> replace the contents of the embedded movie with something like?</p>

<pre><code>cat OtherLargeMovie.mov &gt; "file wrapper test.rtf/FirstLargeMovie.mov"
</code></pre>

<p>If you try this, then very next save will be slower as <code>NSFileWrapper</code> notices you&#8217;ve changed the embedded attachment and it can no longer re-use the existing disk representation. Instead it rewrites the movie from its in-memory cache, getting you back to the state it expects.</p>

<p>As an aside, editing of the internal members of a <code>NSFileWrapper</code> is the sort of edge case that keeps developers up at night. There are a variety of approaches to this that have  tradeoffs between respecting the user&#8217;s edits and providing robust predictable behavior. If you&#8217;d like to do something nicer, in your app, looking at the documentation for <code>-[NSFileWrapper needsToBeUpdatedFromPath:]</code> and <code>-updateFromPath:</code> would be a good place to start.</p>

<p>For more information, see <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSFileWrapper_Class/Reference/Reference.html#//apple_ref/c/tdef/NSFileWrapperWritingOptions">File Wrapper Writing Options</a> and the &#8220;New Support for Incremental Writing in NSFileWrapper&#8221; section of the <a href="http://developer.apple.com/mac/library/releasenotes/Cocoa/AppKit.html">Application Kit Release Notes</a>.</p>

	</div>
  </content>
</entry>

<entry>
  <title>Snow Leopard, a bargain at twice the price!</title>
  <link href="http://www.cocoatoa.com/posts/2009/08/28/snow_leopard_a_bargain_at_twice_the_price/"/>
  <published>2009-08-28T00:00:00+00:00</published>
  <updated>2009-08-28T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/08/28/snow_leopard_a_bargain_at_twice_the_price/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>I&#8217;ve been using Snow Leopard as my primary OS since WWDC&#8217;09 and regularly informing my fellow Omni Groupies that many of the individual features are worth $30 by themselves. They don&#8217;t disagree with this sentiment, but I think they might have an issue with the volume of my proclamations.</p>

<p>Today is Snow Leopard release day, and I can finally shout publicly about the amazing work in Snow Leopard! Each of these are going to make development easier and will result in more powerful Mac OS X apps for our users.</p>

<p>First, there are some huge new technologies for developers that will enable entirely new types of applications:</p>

<ul>
<li><a href="http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-August/002670.html">Blocks</a>. This has been out of the bag for a while now, but it will be years until the full effect of this enhancement is in force. Never has ^ been a more loved character.</li>
<li><a href="http://en.wikipedia.org/wiki/Grand_Central_Dispatch">Grand Central Dispatch</a>. Hand in hand with blocks, this is a new way to make efficient use of multicore systems. This will lead to easier development of applications that use all the power in your Mac.</li>
<li><a href="http://www.khronos.org/opencl/">OpenCL</a>. If taking advantage of up to 16 cores in your Mac wasn&#8217;t enough, how about running the same code on your GPU. Or several GPUs and all your CPUs?</li>
<li><a href="http://llvm.org/">Clang/LLVM</a>. Amazingly good compiler infrastructure, including a new front-end, static code analyzer, better code generation, and faster builds. If you haven&#8217;t run the clang static analyzer on your code, you have bugs. I don&#8217;t know how many, but it isn&#8217;t zero.</li>
</ul>

<p>There are also a ton of improvements to existing features. Each of these is worth the piddly $30 Apple is asking for this update:</p>

<ul>
<li>Xcode is improved in so many ways, I can&#8217;t even think about going back to 10.5.</li>
<li><code>NSImage</code>/<code>CGImage</code> impedance mismatch cleanup and <code>CALayer</code> support for <code>NSImage</code> content.</li>
<li>Per-thread garbage collection!</li>
<li>ObjC associated storage. This can be used for good|evil, but when you need it, you really, really need it.</li>
<li>Smart quotes, URL-ification, and more Text Checking support that will let us delete load of code.</li>
<li>NSPasteboard/Services has a much cleaner API and vastly improved user experience. Also, there was a song&#8230;</li>
<li>A whole ton of new CALayer subclasses that handle fiddly jobs for us.</li>
<li><code>CALayer.geometryFlipped</code>. Now you can do flipped content reasonably in CoreAnimation!</li>
<li><code>-[CALayer needsDisplayForKey:]</code>. Now we can easily add our own properties that animate the content of layers</li>
<li>GPU-dependent <code>CALayer</code> size restrictions lifted. This still seems too good to be true.</li>
<li>Sudden Termination: Apps that opt into this will quit immediately when they have nothing to save on logout/shutdown.</li>
<li><code>NSFileManager</code> rewrite. One of the cruftiest classes gets some much needed love, and some great new features like incremental writing.</li>
<li><code>NSURL</code> &#8220;bookmark&#8221; support &#8212; easy aliases for everyone!</li>
<li>Concurrent document opening. If an app takes advantage of this, it can avoid hanging when opening a large document.</li>
</ul>

<p>These are just a few of the things we&#8217;ve already benefited from or that we&#8217;ve been looking forward to for years. Beyond this, a search of the system headers for <code>10_6</code> is a seemingly never-ending litany of features developers have wanted for years and reading the release notes is a trip into the future.</p>

<p>Congratulations and a hearty &#8220;Thank you!&#8221; to everyone at Apple and all the developers submitting bug reports over the years!</p>

	</div>
  </content>
</entry>

<entry>
  <title>Suffixed Builds</title>
  <link href="http://www.cocoatoa.com/posts/2009/08/09/suffixed_builds/"/>
  <published>2009-08-09T00:00:00+00:00</published>
  <updated>2009-08-09T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/08/09/suffixed_builds/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>Developers know that when you both use and write an application, it is easy to be worried about some little programming error causing corruption of your personal documents. This is particularly true for single-document applications where you have to jump through hoops to switch between your personal data and some test data.</p>

<p>There are tools like <a href="http://roobasoft.com/rooSwitch/">rooSwitch</a> to help quickly switch between sets of preferences and application support files, but I was interested in an approach that was more automatic and allowed me to run a stable build at the same time as a development build.</p>

<p>Running your own work day-to-day, without fear of corrupting anything, means you&#8217;ll be more likely to use your app consistently and will be more likely to find real problems with its usability. Any friction that prevents this is to be avoided.</p>

	</div>
  </content>
</entry>

<entry>
  <title>First Post!</title>
  <link href="http://www.cocoatoa.com/posts/2009/05/26/first_post/"/>
  <published>2009-05-26T00:00:00+00:00</published>
  <updated>2009-08-09T00:00:00+00:00</updated>
  <id>http://www.cocoatoa.com/posts/2009/05/26/first_post/</id>
  <content type="xhtml">
	<div xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
		<p>I&#8217;m finally making a(nother) personal blog, this time with a focus on coding &amp; clay. In the past, I&#8217;ve been turned off by the pain of setting up the various blog engines, the vast expanse of terrible and complex themes, and their propensity for tying up my data. Luddite or control-freak, take your pick.</p>

	</div>
  </content>
</entry>


</feed>
