{"id":1947,"date":"2011-07-21T10:28:37","date_gmt":"2011-07-21T14:28:37","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/?p=1947"},"modified":"2011-07-21T10:28:38","modified_gmt":"2011-07-21T14:28:38","slug":"bit-hacking","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/1947\/bit-hacking","title":{"rendered":"Bit Hacking"},"content":{"rendered":"<p>Lion is the first operating system to require, and to fully take advantage of, 64-bit addressing modes in the Intel chips that power Apple&#8217;s Macintosh computers. One of the side-effects of this is that every object identifier in Mac OS X&#8217;s Cocoa programming framework (typically an address in memory), is now twice as long as it was in a 32-bit environment.<\/p>\n<p>Apple has apparently taken advantage of the 64-bit runtime in Lion by optimizing the Objective C runtime itself to use some of these extra bits for, shall we say, clever purposes.\u00a0<a href=\"http:\/\/objectivistc.tumblr.com\/\">Bavarious describes an optimization<\/a> through which Apple is able to replace previously full-fledged opaque objects such as NSNumber with an object-placeholder that exists entirely as the 64-bit &#8220;object address&#8221; itself. This means that, for a wide range of &#8220;simple&#8221; objects, no additional memory allocation is required, and no retain\/release memory management is required for the &#8220;object.&#8221;<\/p>\n<p>The trick relies on a implementation detail of the system, that allocated blocks of memory will always be aligned at 16-byte offsets into the address space. This leaves a bunch of numbers that can be represented in 64-bits, that cannot reasonably be assigned to any other object. To understand this practically, imagine that your neighborhood&#8217;s postal addresses are all assigned at offsets of 10: 30, 40, 50, etc. A clever postal service could institute an addressing system that uses an &#8220;invalid&#8221; address such as &#8220;31,&#8221; to perhaps mean &#8220;deliver to 30 with expedited afternoon delivery.&#8221;<\/p>\n<p>Cleverness like this with encoding extra information in memory addresses is a time-honored tradition. I recall the days of 24-bit addressing on classic Mac OS, where Apple, and many 3rd party developers, observed that the high 8 bits of a typical memory address could be tweaked and used to store additional information, because the system would never reference those bits when resolving a particular address.<\/p>\n<p>In those days, using those extra bits turned out to be a pretty significant headache when 32-bit addressing ultimately came along, and lots of code had this &#8220;crufty&#8221; treatment of addresses to clean up. Perhaps it is a memory of situations like this that caused Jon &#8220;Wolf&#8221; Rentzsch to <a href=\"http:\/\/www.delicious.com\/url\/a01d14b2e7785507e55d15edf3a131d6\">comment in his bookmarking<\/a> of the above-referenced blog post:<\/p>\n<blockquote>\n<p>&#8220;Every tagged pointer has its lowest bit set, hence tagged pointers are odd integers&#8221; <strong>Strikes me as a really bad idea. <\/strong>[Emphasis Mine]<\/p>\n<\/blockquote>\n<p>But the difference now, in this scenario, is the &#8220;cute hacking&#8221; is all being done by a central power, with and in terms of opaque objects that only Apple has the authority to change. I think this is a really clever hack that will undoubtedly lead to some serious performance gains in Lion and beyond. It&#8217;s hard to imagine specific outcomes that will make Apple regret adopting this strategy. In the worst case scenario, an addressing system of future Macs will not leave any &#8220;spare&#8221; bits to be exploited, so the runtime will simply revert to its previous behavior.<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lion is the first operating system to require, and to fully take advantage of, 64-bit addressing modes in the Intel chips that power Apple&#8217;s Macintosh computers. One of the side-effects of this is that every object identifier in Mac OS X&#8217;s Cocoa programming framework (typically an address in memory), is now twice as long as [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,20,15],"tags":[],"class_list":["post-1947","post","type-post","status-publish","format-standard","hentry","category-apple","category-hacking","category-programming"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/1947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=1947"}],"version-history":[{"count":1,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/1947\/revisions"}],"predecessor-version":[{"id":1948,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/1947\/revisions\/1948"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=1947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=1947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=1947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}