<?xml version="1.0"?>
<!-- RSS generated by my own two hands and bloggerizer.pl -->
<rss version="2.0">
  <channel>
    <title>Hamster Emporium</title>
    <link>http://www.sealiesoftware.com/blog/</link>
    <description>Greg Parker's web log</description>
    <item>
      <title>[objc explain]: Weak-import classes</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/09/09/objc_explain_Weak-import_classes.html</link>
      <pubDate>09 Sep 2009 13:30 PDT</pubDate>
      <description>      &lt;p&gt;
	Weak-import classes are a useful new Objective-C feature that you
	can't use yet. 
      &lt;/p&gt;
      &lt;p&gt;
	Weak import is a solution when you want to use something from a
	framework, but still need to be compatible with older versions of
	the framework that didn't support it yet. Using weak import you
	can test if the feature exists at runtime before you try to use
	it. 
      &lt;/p&gt;
      &lt;p&gt;
	Objective-C has not previously supported weak import for
	classes. Instead you had to use clumsy runtime introspection to
	check whether a class was available, store a pointer to that class
	in a variable, and use that variable when you wanted to send a
	message to the class. Even worse, there was no reasonable way to
	create your own subclass of a superclass that might be
	unavailable. Some developers put the subclass in a separate
	library that was not loaded until after checking that the
	superclass was present, but even that trick is not allowed on
	iPhone OS. 
      &lt;/p&gt;
      &lt;p&gt;
	Weak import for C functions works by checking the weak-imported
	function pointer's value before calling it:
      &lt;/p&gt;
      &lt;pre&gt;    if (NSNewFunction != NULL) {
        NSNewFunction(...);
    } else {
        // NSNewFunction not supported on this system
    }&lt;/pre&gt;
      &lt;p&gt;
	The same mechanism is a natural fit with Objective-C classes and 
	Objective-C's handling of messages to nil. These constructs are
	much nicer than &lt;code&gt;NSClassFromString()&lt;/code&gt; or a separate 
	&lt;code&gt;NSBundle&lt;/code&gt;. 
      &lt;/p&gt;
      &lt;pre&gt;    if ([NSNewClass class] != nil) {
        [NSNewClass doSomething];
    } else {
        // NSNewClass is unavailable on this system
    }&lt;/pre&gt;
      &lt;pre&gt;    @interface MySubclass : NSNewClass ... @end
    MySubclass *obj = [[MySubclass alloc] init];
    if (!obj) {
        // MySubclass (or a superclass thereof) is unavailable on this system
    }&lt;/pre&gt;
      &lt;p&gt;
	Weak import of Objective-C classes is now available. But you can't
	use it yet. First, it's only supported today on iPhone OS 3.1; 
	it's expected to arrive in a future Mac OS. 
      &lt;/p&gt;
      &lt;p&gt;
	Second, there's nothing you can do with weak import until the
	first OS update &lt;i&gt;after&lt;/i&gt; iPhone OS 3.1. Then you could write an app
	that adopted new features in that future version, and used weak import
	to be compatible with 3.1. (It still could not run on 3.0 or 2.x,
	because those systems lack the runtime machinery to process the
	weak import references.)
      &lt;/p&gt;
      &lt;p&gt;
	Weak import for Objective-C did not make Snow Leopard for
	scheduling reasons. Assuming it ships in Mac OS X 10.7 Cat Name
	Forthcoming, you won't be able to use it until Mac OS X 10.8
	LOLcat. If you are a major Mac developer, and you would like to
	use Objective-C weak import on 10.7 and run on 10.6.x, please file
	a bug report and we may be able to get the runtime changes into a
	Snow Leopard software update.
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>Colorized keyboard backlight</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/09/05/Colorized_keyboard_backlight.html</link>
      <pubDate>05 Sep 2009 1:15 PDT</pubDate>
      <description>    &lt;p&gt;
      I use my MacBook Pro for astronomy. The backlit keyboard would be
      great in the dark, but its white light is bad for night
      vision. &lt;a href="/keyboard/index.html"&gt;This mod&lt;/a&gt; makes it red, or any other color you want.
    &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Selector uniquing in the dyld shared cache</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/09/01/objc_explain_Selector_uniquing_in_the_dyld_shared_cache.html</link>
      <pubDate>01 Sep 2009 2:10 PDT</pubDate>
      <description>      &lt;p&gt;
	Mac OS X Snow Leopard cuts in half the launch-time overhead of
	starting the Objective-C runtime, and simultaneously saves a few
	hundred KB of memory per app. This comes for free to every app,
	courtesy of one of the few pieces of Mac OS X that lives below
	even the Objective-C runtime: dyld.
      &lt;/p&gt;
&lt;h4&gt;dyld and the shared cache&lt;/h4&gt;
      &lt;p&gt;
	dyld is the dynamic loader and linker. When your process starts,
	dyld loads your executable and its shared libraries into memory,
	links the cross-library C function and variable references
	together, and starts execution on its way towards
	&lt;code&gt;main()&lt;/code&gt;. 
      &lt;/p&gt;
      &lt;p&gt;
	In theory a shared library could be different every time your
	program is run. In practice, you get the same version of
	the shared libraries almost every time you run, and so does every
	other process on the system. The system takes advantage of this by
	building the dyld shared cache. The shared cache contains a copy
	of many system libraries, with most of dyld's linking and loading
	work done in advance. Every process can then share that shared
	cache, saving memory and launch time. 
      &lt;/p&gt;
      &lt;p&gt;
	(Incidentally, the shared cache beats the pants off the pre-Leopard
	prebinding system that was supposed to achieve the same
	optimizations. Remember the post-install "Optimizing System
	Performance" step that often took longer than the install itself?
	That was prebinding being updated. Rebuilding the shared cache is 
	so blazingly fast that the installer doesn't bother to report it
	anymore.) 
      &lt;/p&gt;
      &lt;h4&gt;Objective-C selector uniquing&lt;/h4&gt;
      &lt;p&gt;
	Leopard's dyld shared cache is great for C code, but it didn't do
	anything to help Objective-C's startup overhead. The single
	biggest launch cost for Objective-C is selector uniquing. The app
	and every shared library contain their own copies of selector
	names like "alloc" and "init". The runtime needs to choose a
	single canonical SEL pointer value for each selector name, and
	then update the metadata for every call site and method list to 
	use the blessed unique value. This means building a big hash table
	(memory), calling &lt;code&gt;strcmp()&lt;/code&gt; a lot (time), and
	modifying copy-on-write metadata (more memory). 
      &lt;/p&gt;
      &lt;p&gt;
	There are tens of thousands of unique selectors present in a
	typical process. If you run &lt;code&gt;`strings
	/usr/lib/libobjc.dylib`&lt;/code&gt; on Leopard you can see the
	thirty-thousand-line 
	&lt;a
	href="http://www.opensource.apple.com/source/objc4/objc4-371/runtime/objc-sel-table.h"&gt;built-in
	selector table&lt;/a&gt; that was a previous attempt to reduce the
	memory 
	cost. Even so the cost goes up with every new class and
	method added to Cocoa.framework; left unchecked, an identical app
	would take longer to launch and use more memory after every OS
	upgrade. 
      &lt;/p&gt;
      &lt;p&gt;
	The obvious solution? Do the work of selector uniquing in the dyld
	shared cache. Build a selector table into the shared cache itself,
	and update the selector references in the cached copy of the
	shared libraries. Then you save memory because every process
	shares the same selector table, and save time because the runtime
	does not need to rebuild it during every app launch. The runtime
	only needs to fix the selector references from the app itself. The
	catch? 
	Selectors are too dynamic to be implemented as C symbols, so the
	shared cache construction tool needed to be taught how to read and
	write Objective-C's metadata. 
      &lt;/p&gt;
      &lt;h4&gt;Optimization WIN&lt;/h4&gt;
      &lt;p&gt;
	Snow Leopard's dyld shared cache uniques Objective-C selectors,
	and Snow Leopard's Objective-C runtime recognizes when the
	selectors in a shared library are already uniqued courtesy of the
	shared cache. About half of the runtime's initialization time is
	eliminated, making warm app launch several tenths of a second
	faster. Typical memory savings is 200-500 KB per process, 
	adding up to a few megabytes system-wide. When this optimization
	ships on the iPhone OS side, it's estimated to save 1 MB on a 128
	MB device. The iPhone performance team would pay any number of
	arms and legs for that kind of gain. 
      &lt;/p&gt;
      &lt;p&gt;
	You can watch the system in action with various debugging flags.
      &lt;/p&gt;
      &lt;p&gt;
      &lt;code&gt;$ sudo /usr/bin/update_dyld_shared_cache -debug -verify&lt;br&gt;
[...]&lt;br&gt;
update_dyld_shared_cache: for x86_64, uniquing objc selectors&lt;br&gt;
update_dyld_shared_cache: for x86_64, found 68761 unique objc selectors&lt;br&gt;
update_dyld_shared_cache: for x86_64, 541736/590908 bytes (91%) used in	libobjc unique selector section&lt;br&gt;
update_dyld_shared_cache: for x86_64, updated 205230 selector references&lt;br&gt;
&lt;/code&gt;
      &lt;/p&gt;
      &lt;p&gt;
      &lt;code&gt;$ OBJC_PRINT_PREOPTIMIZATION=YES /usr/bin/defaults&lt;br&gt;
objc[424]: PREOPTIMIZATION: selector preoptimization ENABLED (version 3)&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /usr/lib/libobjc.A.dylib&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation&lt;br&gt;
&lt;/code&gt;
&lt;/p&gt;
      &lt;p&gt;
	You can estimate the memory savings with the
	&lt;code&gt;&lt;a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/allmemory.1.html"&gt;allmemory&lt;/a&gt;&lt;/code&gt; tool. Record post-launch memory usage of an
	app run with and without environment variable
	&lt;code&gt;OBJC_DISABLE_PREOPTIMIZATION=YES&lt;/code&gt;. Look for the count
	of dirty pages; each dirty page is 4 KB eaten by that
	process. With 64-bit TextEdit I see the dirty page count jump from
	725 to 1069 after disabling the optimization. This is an
	overestimate - many of those pages would have been not-dirty in
	Leopard because of the old built-in selector table - but it does
	show the magnitude of the win.
      &lt;/p&gt;
      &lt;p&gt;
	The Objective-C runtime does more than just selector uniquing
	during launch. Future improvements to the dyld shared cache may
	precompute some of that other work, to further improve launch
	time, save memory, and reduce the cost of linking to Objective-C
	code that you don't actually use. But selector uniquing as seen in
	Snow Leopard is by far the biggest bang for the buck.
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Thread-local garbage collection</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/08/28/objc_explain_Thread-local_garbage_collection.html</link>
      <pubDate>28 Aug 2009 13:00 PDT</pubDate>
      <description>      &lt;p&gt;
	Mac OS X Snow Leopard introduces thread-local collection, a big
	enhancement to the Objective-C garbage collector. Thread-local
	collection is a more efficient way to reclaim much of the
	garbage in most programs. It also scales better to more threads
	and more cores than the other algorithms used by the Objective-C
	GC.
      &lt;/p&gt;
      &lt;h4&gt;A brief history of garbage collection&lt;/h4&gt;
      &lt;p&gt;
	The simplest algorithm used by the Objective-C GC is &lt;b&gt;full
	collection&lt;/b&gt;. The GC scans all live objects in the entire heap, 
	discovers (approximately) all dead objects, and reclaims
	them. This is slow, especially if you have a large
	population of not-dead objects, but does find all possible
	garbage. Historically this is mostly 1960-era technology, except
	for the machinery that allows other threads to run mostly
	unhindered while the scan completes. 
      &lt;/p&gt;
      &lt;p&gt;
	The second algorithm used by the Objective-C GC is &lt;b&gt;generational
	collection&lt;/b&gt;. This takes advantage of the &lt;i&gt;generational
	hypothesis&lt;/i&gt;: most objects die young. The heap is divided into
	at least two generations: new objects and old objects. After
	allocating some amount of new objects, the collector runs a
	generational collection. First, it scans only the "new" objects
	and any "old" objects into which a pointer to a "new" object was
	stored. Then the now-dead "new" objects are reclaimed, and the
	surviving "new" objects are aged, moving them to the next 
	generation. The advantage of generational GC is that it collects
	lots of garbage (most objects die young) with much less work (it
	does not need to scan most of the "old" objects). Full collections
	are still needed to reclaim objects that survive infancy and die
	later, but run less often. Generational collection is 1980-era
	technology. 
      &lt;/p&gt; 
      &lt;h4&gt;Thread-local collection&lt;/h4&gt;
      &lt;p&gt;
	The third algorithm is the new &lt;b&gt;thread-local collection&lt;/b&gt;. TLC
	is similar to generational collection: it scans and reclaims a
	subset of objects, trying to get lots of bang for the collector's
	buck. The &lt;i&gt;thread-local&lt;/i&gt; hypothesis: most objects die without
	being reachable by any other thread. Newly-allocated objects are
	marked thread-local to the allocating thread. If a thread-local
	object becomes accessible to another thread (for example, a
	pointer to it is written into a global variable), then it has
	"escaped" and is moved
	out of the thread-local set. In a thread-local collection, a
	thread scans its own stack and its set of thread-local objects, and
	reclaims the dead objects.
      &lt;/p&gt;
      &lt;p&gt;
	The advantage of thread-local collection is that it requires no
	synchronization with other threads. Normally, a thread performing
	GC work needs to coordinate with the other threads. For example,
	the other threads change a pointer variable after the GC thread
	has looked at it, or start pointing to an object that the GC
	thread thinks is dead. Thread-local collection avoids these
	complications. The thread-local objects are
	by definition reachable only by one thread. The other threads
	have no way acquire a pointer to any of those objects, or change 
	pointer
	values inside them. A thread performing thread-local collection
	can work quickly on its own, without interference from other threads.
      &lt;/p&gt;
      &lt;p&gt;
	Having each thread "clean up after itself" reduces 
	bottlenecks in the collector that will only get worse as threads
	and cores increase. It's trivial to run thread-local
	collections on multiple threads simultaneously. And it's very fast
	because the only memory to scan is the thread's own stack and its 
	surviving thread-local objects. The pause time for a thread during
	generational or full GC is almost as big as the pause time for
	that thread to run a thread-local collection - but TLC can then
	immediately reclaim some garbage, 
	whereas the other algorithms need to do much more work and
	coordinate with all of the other threads 
	before they can actually reclaim anything.
      &lt;/p&gt;
      &lt;h4&gt;How you can help&lt;/h4&gt;
      &lt;p&gt;
	Thread-local collection works best when objects remain unreachable
	to other threads. In the Objective-C collector, this means
	avoiding CFRetain() of temporary objects when possible. A
	CFRetained pointer could go anywhere behind the collector's back,
	bypassing the write barrier that the collector uses to keep track
	of escaping objects. (This is one place that Snow Leopard leaves
	room for improvement: the system frameworks often allocate objects
	with a CF retain count of one and immediately release them, making
	them ineligible for thread-local collection.) Other ways for an
	object to escape thread-local collection include storing a pointer
	into a global variable; storing a pointer into some other object
	that itself is not thread-local; and making a weak reference or
	associated reference to the object.
      &lt;/p&gt;
      &lt;p&gt;
	If your thread has just created and discarded a lot of temporary
	objects, you can give the collector a hint that now might be a
	good time to run. &lt;code&gt;-[NSGarbageCollector
	collectIfNeeded]&lt;/code&gt; and &lt;code&gt;-[NSAutoreleasePool
	drain]&lt;/code&gt; are two such hints. These may run a thread-local
	collection first, and may follow up with generational or full
	collection. 
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: So you crashed in objc_msgSend(): iPhone Edition</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/06/08/objc_explain_So_you_crashed_in_objc_msgSend_iPhone_Edition.html</link>
      <pubDate>08 Jun 2009 23:40 PDT</pubDate>
      <description>      &lt;p&gt;
	&lt;a
	href="archive/2008/09/22/objc_explain_So_you_crashed_in_objc_msgSend.html"&gt;So
	you crashed in objc_msgSend()&lt;/a&gt; has been updated with register
	usage for iPhone's ARM processor. The table now looks like this:
	&lt;table border=1 cellspacing=0 cellpadding=4&gt;
	  &lt;th&gt;&lt;td align=center colspan=2&gt;&lt;code&gt;objc_msgSend&lt;/code&gt;&lt;br&gt;&lt;code&gt;objc_msgSend_fpret&lt;/code&gt;&lt;/td&gt;
	    &lt;td align=center colspan=2&gt;&lt;code&gt;objc_msgSend_stret&lt;/code&gt;&lt;/td&gt;&lt;/th&gt;
	  &lt;tr&gt;&lt;td align=center&gt;&amp;nbsp;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;receiver&lt;/code&gt;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;SEL&lt;/code&gt;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;receiver&lt;/code&gt;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;SEL&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;i386&lt;/td&gt;&lt;td align=center&gt;eax*&lt;/td&gt;&lt;td align=center&gt;ecx&lt;/td&gt;
	    &lt;td align=center&gt;eax*&lt;/td&gt;&lt;td align=center&gt;ecx&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;x86_64&lt;/td&gt;&lt;td align=center&gt;rdi&lt;/td&gt;&lt;td align=center&gt;rsi&lt;/td&gt;
	    &lt;td align=center&gt;rsi&lt;/td&gt;&lt;td align=center&gt;rdx&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;ppc&lt;/td&gt;&lt;td align=center&gt;r3&lt;/td&gt;&lt;td align=center&gt;r4&lt;/td&gt;
	    &lt;td align=center&gt;r4&lt;/td&gt;&lt;td align=center&gt;r5&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;ppc64&lt;/td&gt;&lt;td align=center&gt;r3&lt;/td&gt;&lt;td align=center&gt;r4&lt;/td&gt;
	    &lt;td align=center&gt;r4&lt;/td&gt;&lt;td align=center&gt;r5&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;arm&lt;/td&gt;&lt;td align=center&gt;r0&lt;/td&gt;&lt;td align=center&gt;r1&lt;/td&gt;
	    &lt;td align=center&gt;r1&lt;/td&gt;&lt;td align=center&gt;r2&lt;/td&gt;&lt;/tr&gt;
	&lt;/table&gt;
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>Valgrind for Mac OS X goes mainline</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/06/03/Valgrind_for_Mac_OS_X_goes_mainline.html</link>
      <pubDate>03 Jun 2009 19:20 PDT</pubDate>
      <description>      &lt;p&gt;
	Thanks to the heroic work of Nicholas Nethercote and Julian
	Seward, the Mac OS X port of Valgrind is &lt;a
	href="http://blog.mozilla.com/nnethercote/2009/05/28/mac-os-x-now-supported-on-the-valgrind-trunk/"&gt;now
	available on Valgrind's trunk&lt;/a&gt;. This is a big step forward
	towards an official &lt;a
	href="http://valgrind.org"&gt;Valgrind&lt;/a&gt; release with Mac OS X support. 
      &lt;/p&gt;
      &lt;p&gt;
	For those of you with &lt;a
	href="http://www.apple.com/macosx/snowleopard/"&gt;Snow Leopard&lt;/a&gt;
	seeds, Valgrind won't 
	work. Valgrind operates at the low-level unsupported guts of the
	kernel/Libc interface. When the kernel and Libc change, Valgrind
	needs to adapt or die. Valgrind support for Snow Leopard will not
	be available until the open-source release of Snow Leopard's
	kernel and Libc at the earliest, which in turn is not before Snow
	Leopard itself ships.  
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Monomorphic dispatch</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/05/14/objc_explain_Monomorphic_dispatch.html</link>
      <pubDate>14 May 2009 20:04 PDT</pubDate>
      <description>      &lt;p&gt;
	Polymorphic dispatch means a single call site could branch to one
	of several different implementations. C function calls are not
	polymorphic; Objective-C methods and C++ virtual methods are
	polymorphic. 
      &lt;/p&gt;
      &lt;p&gt;
	The monomorphic dispatch optimization is used when a call site
	could call different implementations in principle, but can only 
	ever call one particular implementation in reality. Then the
	optimizer can eliminate the polymorphic dispatcher's overhead and
	jump directly to the right place or even inline the callee
	locally. This is a classic 
	optimization for dynamic-compiled runtimes from Smalltalk to Java
	and JavaScript.
      &lt;/p&gt;
      &lt;p&gt;
	There are some complications. First is dynamically-loaded code
	like shared libraries or eval() operations. If your new code
	provides a second implementation of a call that was previously
	monomorphic, you need to be able to undo the previous optimization
	on the fly and recompile it or fall back to an interpreter. Any
	dynamic compiler worth the name can do this nowadays.
      &lt;/p&gt;
      &lt;p&gt;
	Second, the area to search for additional implementations depends
	on the type-strictness of your language. If the compile-time type
	of the receiver rigidly defines the allowed runtime types, then
	the implementation need only be unique within that part of the
	hierarchy for the optimization to work. &lt;code&gt;Window.title&lt;/code&gt;
	and &lt;code&gt;Employee.title&lt;/code&gt; wouldn't interfere with each other
	at a strictly-typed call site whose receiver is of type
	&lt;code&gt;Employee&lt;/code&gt; (or a subclass thereof). 
      &lt;/p&gt;
      &lt;p&gt;
	How does Objective-C fit in here? In general, it doesn't. The
	monomorphic optimization is hard to apply to Objective-C. The two
	problems above loom large because of the language's definition,
	even if it suddenly acquired a runtime recompiler tomorrow.
      &lt;/p&gt;
      &lt;p&gt;
	Objective-C's call sites are not type-strict. The code may say the
	receiver is of some type, but at runtime it could actually be a
	Distributed Objects proxy or a unit test mock or a scripting
	bridge shim. You'd have to look at all classes to decide if a 
	selector has multiple implementations, instead of searching only a
	subtree of the class hierarchy.
      &lt;/p&gt;
      &lt;p&gt;
	Even worse, there are zero selectors that have only a single
	implementation. They all have at least two: the one that exists in 
	some class, and the one from every other class that calls
	-forwardInvocation:. You can never jump directly to any 
	implementation, because if your receiver object is of the wrong
	type then you need to call the forwarding machinery instead. And
	checking the receiver's type quickly eats any optimization profit;
	you can only make a handful of checks before your cost is the
	same as &lt;code&gt;objc_msgSend()&lt;/code&gt;. 
      &lt;/p&gt;
      &lt;p&gt;
	There are some important cases where monomorphic dispatch would
	still work in Objective-C. The container classes in particular
	have only one or two real implementations, so a receiver type
	check could be fast enough. And in other places you can make a single
	relatively expensive type check but then re-use the result many
	times, such as a series of &lt;code&gt;[self ...]&lt;/code&gt; calls. The
	tricky part is identifying which selectors and call sites would
	optimize well, without taking too much time or memory to do so.
      &lt;/p&gt;
      &lt;p&gt;
	The monomorphic dispatch optimization will be present in some
	future dynamic-recompiling Objective-C runtime, but it won't work
	as well as it does in other less-dynamic languages.
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Classes and metaclasses</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html</link>
      <pubDate>14 Apr 2009 20:35 PDT</pubDate>
      <description>      &lt;p&gt;
	Objective-C is a class-based object system. Each object is an
	instance of some class; the object's &lt;code&gt;isa&lt;/code&gt; pointer
	points to its class. That class describes the object's data:
	allocation size and ivar types and layout. The class also
	describes the object's behavior: the selectors it responds to and
	instance methods it implements.
      &lt;/p&gt;
      &lt;p&gt;
	The class's method list is the set of instance methods, the
	selectors that the object responds to. When 
	you send a message to an instance, &lt;code&gt;objc_msgSend()&lt;/code&gt;
	looks through the method list of that object's class (and
	superclasses, if any) to decide what method to call. 
      &lt;/p&gt;
      &lt;p&gt;
	Each Objective-C class is also an object. It has an
	&lt;code&gt;isa&lt;/code&gt; pointer and other data, and can respond to
	selectors. When you call a "class method" like &lt;code&gt;[NSObject
	alloc]&lt;/code&gt;, you are actually sending a message to that class
	object. 
      &lt;/p&gt;
      &lt;p&gt;
	&lt;a href="/blog/class diagram.pdf"&gt;&lt;img width=50% src="/blog/class diagram.pdf" align=right&gt;&lt;/a&gt;
	Since a class is an object, it must be an instance of some other
	class: a metaclass. The metaclass is the description of the class
	object, just like the class is the description of ordinary
	instances. In particular, the metaclass's method list is the
	class methods: the selectors that the class object responds
	to. When you send a message to a class - an instance of a
	metaclass - &lt;code&gt;objc_msgSend()&lt;/code&gt; looks through the method
	list of the metaclass (and its superclasses, if any) to decide
	what method to call. Class methods are described by the metaclass
	on behalf of the class object, just like instance methods are
	described by the class on behalf of the instance objects.
      &lt;/p&gt;
      &lt;p&gt;
	What about the metaclass? Is it metaclasses all the way down?
	No. A metaclass is an instance of the root class's metaclass; the
	root metaclass is itself an instance of the root metaclass. The
	&lt;code&gt;isa&lt;/code&gt; chain ends in a cycle here: instance to class to
	metaclass to root metaclass to itself. The behavior of metaclass
	&lt;code&gt;isa&lt;/code&gt; pointers rarely matters, since in the real world
	nobody sends messages to metaclass objects. 
      &lt;/p&gt;
      &lt;p&gt;
	More important is the superclass of
	a metaclass. The metaclass's superclass chain parallels the
	class's superclass chain, so class methods are inherited in
	parallel with instance methods. And the root metaclass's
	superclass is the root class, so each class object responds to the
	root class's instance methods. In the end, a class object is an
	instance of (a subclass of) the root class, just like any other
	object. 
      &lt;/p&gt;
      &lt;p&gt;
	Confused? The diagram may help. Remember, when a message is sent
	to any object, the method lookup starts with that object's
	&lt;code&gt;isa&lt;/code&gt; pointer, then continues up the superclass
	chain. "Instance methods" are defined by the class, and "class
	methods" are defined by the metaclass plus the root (non-meta) class.
      &lt;/p&gt;
      &lt;p&gt;
	In proper computer science
	language theory, a class and metaclass hierarchy can be more
	free-form, with deeper metaclass chains and multiple classes
	instantiated from any single metaclass. Objective-C uses metaclasses
	for practical goals like class methods, but otherwise tends to
	hide metaclasses. For example, &lt;code&gt;[NSObject class]&lt;/code&gt; is
	identical to &lt;code&gt;[NSObject self]&lt;/code&gt;, even though in formal
	terms it ought to return the metaclass that
	&lt;code&gt;NSObject-&gt;isa&lt;/code&gt; points to. The Objective-C language is
	a set of practical compromises; here it limits the class schema
	before it gets too, well, &lt;i&gt;meta&lt;/i&gt;. 
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Non-fragile ivars</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/01/27/objc_explain_Non-fragile_ivars.html</link>
      <pubDate>27 Jan 2009 21:30 PDT</pubDate>
      <description>      &lt;p&gt;
	Non-fragile instance variables are a headline feature of the
	modern Objective-C runtime available on iPhone and 64-bit
	Mac. They provide framework developers more flexibility without
	losing binary compatibility, and pave the way for
	automatically-synthesized property ivars and ivars declared
	outside a class's interface.
      &lt;/p&gt;
      &lt;h4&gt;The fragile base class problem&lt;/h4&gt;
      &lt;p&gt;
	Fragile ivars are a subset of the classic &lt;i&gt;fragile base
	class&lt;/i&gt; problem. In some languages, a superclass cannot be
	changed without also recompiling all subclasses of that class. For
	example, adding data members or virtual member functions to a C++
	superclass will break binary compatibility with any subclass of
	that class, even if the added members are private and invisible to
	the subclass. 
      &lt;/p&gt;
      &lt;p&gt;
	In classic Objective-C, methods are mostly
	non-fragile, thanks to dynamic message dispatch. You can freely
	add methods to a superclass, as long 
	as you don't have name conflicts. But Objective-C ivars are
	fragile on 32-bit Mac.
      &lt;/p&gt;
      &lt;h4&gt;32-bit Mac: fragile Objective-C ivars&lt;/h4&gt;
      &lt;p&gt;
	Say you're writing the world's next great pet shop
	application for Mac OS X Leopard. You might have this 
	&lt;code&gt;PetShopView&lt;/code&gt; subclass of 
	&lt;code&gt;NSView&lt;/code&gt;, with arrays for the puppies and kittens in
	the pet shop.
      &lt;/p&gt;
      &lt;p&gt;
	&lt;table&gt;
	  &lt;tr&gt;
	    &lt;td valign=top&gt;
	      &lt;table border=1 cellspacing=0&gt;
		&lt;tr&gt;&lt;td colspan=2&gt;&lt;i&gt;&lt;code&gt;NSView (Leopard)&lt;/code&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;Class isa&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSRect bounds&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;20&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSView *superview&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;24&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSColor *bgColor&lt;/code&gt;&lt;/tr&gt;
	      &lt;/table&gt;
	    &lt;/td&gt;
	    &lt;td valign=top&gt;
	      &lt;table border=1 cellspacing=0&gt;
		&lt;tr&gt;&lt;td colspan=2&gt;&lt;i&gt;&lt;code&gt;PetShopView&lt;/code&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;Class isa&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSRect bounds&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;20&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSView *superview&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;24&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSColor *bgColor&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#dddddd&gt;&lt;td align=right&gt;&lt;code&gt;28&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSArray *kittens&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#dddddd&gt;&lt;td align=right&gt;&lt;code&gt;32&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSArray *puppies&lt;/code&gt;&lt;/tr&gt;
	      &lt;/table&gt;
	    &lt;/td&gt;
	  &lt;/tr&gt;
	&lt;/table&gt;
      &lt;/p&gt;
      &lt;p&gt;
	Then Mac OS X Def Leopard comes out, with its new multi-paw
	interface technology. The AppKit developers add some paw-tracking
	code to &lt;code&gt;NSView&lt;/code&gt;.
      &lt;/p&gt;
      &lt;p&gt;
	&lt;table&gt;
	  &lt;tr&gt;
	    &lt;td valign=top&gt;
	      &lt;table border=1 cellspacing=0&gt;
		&lt;tr&gt;&lt;td colspan=2&gt;&lt;i&gt;&lt;code&gt;NSView (Def Leopard)&lt;/code&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;Class isa&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSRect bounds&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;20&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSView *superview&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;24&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSColor *bgColor&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;28&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;NSSet *touchedPaws&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/tr&gt;
	      &lt;/table&gt;
	    &lt;/td&gt;
	    &lt;td valign=top&gt;
	      &lt;table border=1 cellspacing=0&gt;
		&lt;tr&gt;&lt;td colspan=2&gt;&lt;i&gt;&lt;code&gt;PetShopView&lt;/code&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;Class isa&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSRect bounds&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;20&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSView *superview&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;24&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSColor *bgColor&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#dddddd&gt;&lt;td align=right&gt;&lt;code&gt;28&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;&lt;strike&gt;&lt;font color=#440000&gt;&lt;b&gt;NSArray *kittens&lt;/b&gt;&lt;/font&gt;&lt;/strike&gt;&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#dddddd&gt;&lt;td align=right&gt;&lt;code&gt;32&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSArray *puppies&lt;/code&gt;&lt;/tr&gt;
	      &lt;/table&gt;
	    &lt;/td&gt;
	  &lt;/tr&gt;
	&lt;/table&gt;
      &lt;/p&gt;
      &lt;p&gt;
	Unfortunately, your kittens are doomed by fragile
	ivars. Alternatively, the AppKit developers are trapped with
	whatever ivars they chose in Mac OS X 10.0.
      &lt;/p&gt;
      &lt;h4&gt;iPhone and 64-bit Mac: non-fragile Objective-C ivars&lt;/h4&gt;
      &lt;p&gt;
	What you and the AppKit developers really want is something like
	this.
      &lt;/p&gt;
      &lt;p&gt;
	&lt;table&gt;
	  &lt;tr&gt;
	    &lt;td valign=top&gt;
	      &lt;table border=1 cellspacing=0&gt;
		&lt;tr&gt;&lt;td colspan=2&gt;&lt;i&gt;&lt;code&gt;NSView (Def Leopard)&lt;/code&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;Class isa&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSRect bounds&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;20&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSView *superview&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;24&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSColor *bgColor&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;28&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;NSSet *touchedPaws&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/tr&gt;
	      &lt;/table&gt;
	    &lt;/td&gt;
	    &lt;td valign=top&gt;
	      &lt;table border=1 cellspacing=0&gt;
		&lt;tr&gt;&lt;td colspan=2&gt;&lt;i&gt;&lt;code&gt;PetShopView&lt;/code&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;Class isa&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSRect bounds&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;20&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSView *superview&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;24&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSColor *bgColor&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#eeeeee&gt;&lt;td align=right&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;28&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;NSSet *touchedPaws&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#dddddd&gt;&lt;td align=right&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;32&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSArray *kittens&lt;/code&gt;&lt;/tr&gt;
		&lt;tr bgcolor=#dddddd&gt;&lt;td align=right&gt;&lt;code&gt;&lt;font color=#004400&gt;&lt;b&gt;36&lt;/b&gt;&lt;/font&gt;&lt;/code&gt;&lt;/td&gt;
		  &lt;td&gt;&lt;code&gt;NSArray *puppies&lt;/code&gt;&lt;/tr&gt;
	      &lt;/table&gt;
	    &lt;/td&gt;
	  &lt;/tr&gt;
	&lt;/table&gt;
      &lt;/p&gt;
      &lt;p&gt;
	Here, the runtime has recognized that &lt;code&gt;NSView&lt;/code&gt; is now
	larger than it was when &lt;code&gt;PetShopView&lt;/code&gt; was compiled. The
	subclass ivars slide in response, without recompiling any code,
	and the kittens are saved by a dynamic runtime.
      &lt;/p&gt;
      &lt;h4&gt;How it works&lt;/h4&gt;
      &lt;p&gt;
	The generated code for classic Objective-C ivar access works like
	a C &lt;code&gt;struct&lt;/code&gt; field. The offset to the ivar is a
	constant determined at compile time. The new ivar code instead
	creates a variable for each ivar which contains the offset to that
	ivar, and all code that accesses the ivar uses that
	variable. At launch time, the runtime can change any ivar offset
	variable if it detects an oversize superclass. 
      &lt;/p&gt;
      &lt;p&gt;
	In the pet shop example, 
	&lt;code&gt;_OBJC_IVAR_PetShopView_kittens&lt;/code&gt; is 28 at
	compile time, 
	but the runtime changes it to 32 when it sees the Def Leopard
	version of &lt;code&gt;NSView&lt;/code&gt;. No code needs to be recompiled,
	and the performance overhead of the extra ivar offset variable is
	small. AppKit is happy, you're happy, and the kittens are happy.
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>Infinity isn't as long as you think</title>
      <link>http://www.sealiesoftware.com/blog/archive/2008/11/30/Infinity_isnt_as_long_as_you_think.html</link>
      <pubDate>30 Nov 2008 3:29 PDT</pubDate>
      <description>      &lt;p&gt;
	My intuition about infinite time used to go something like this:
	every possible event will happen, if you wait long enough. 
      &lt;/p&gt;
      &lt;p&gt;
	Consider a random walk on a 2-D grid. Will the drunkard return to
	the starting point? Yes, if you wait long enough: you can prove
	that the random walk will, eventually, return. (Formally, it
	returns "almost surely" or "with probability 1". For example, the
	random walk could proceed due east forever and never return; it's
	not impossible, just infinitely improbable.) 
      &lt;/p&gt;
      &lt;p&gt;
	In fact, you can prove the same thing about every other point on
	the grid, not just the start. Each point will ("almost surely") be
	visited during the random walk. If you wait long enough, the
	drunkard will wander everywhere. Score a point for the "everything
	happens eventually" idea.
      &lt;/p&gt;
      &lt;p&gt;
	But that intuition is wrong. Forever is not always enough.
      &lt;/p&gt;
      &lt;p&gt;
	Move the drunkard to a 3-D grid. Now the probability of returning
	to the start is not "almost sure". Instead, it's a little over
	one-third. If you start three drunkards on a 3-D random walk, you
	would expect only one of them to ever come back, even if you wait
	forever. And the odds are even worse with more dimensions.
      &lt;/p&gt;
      &lt;p&gt;
	Some events still don't happen after infinite time. Infinity isn't
	as long as you think.
      &lt;/p&gt;
      &lt;p&gt;
	&lt;a href="http://en.wikipedia.org/wiki/Random_walk"&gt;http://en.wikipedia.org/wiki/Random_walk&lt;/a&gt;&lt;br&gt;
	&lt;a href="http://mathworld.wolfram.com/PolyasRandomWalkConstants.html"&gt;http://mathworld.wolfram.com/PolyasRandomWalkConstants.html&lt;/a&gt;
      &lt;/p&gt;
</description>
    </item>
  </channel>
</rss>