<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Tue, 12 May 2026 00:23:59 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>mikeash - 2016-11-22 15:10:09</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>&lt;b&gt;Alex:&lt;/b&gt; If your pointers are 16-byte aligned, then a tagged pointer could be identified by having any of the lower four bits be non-zero. That would allow for 15 tagged classes, rather than only 8 as the Objective-C implementation does.
&lt;br /&gt;
&lt;br /&gt;One problem is that pointers aren't necessarily 16-byte aligned. Objects allocated on the heap are, but things like class pointers or constant strings may not be. You should at least be able to count on 8-byte alignment on a 64-bit CPU, so you could identify a tagged pointer based on any of the lower three bits being 1.
&lt;br /&gt;
&lt;br /&gt;I'd speculate that only checking the least significant bit is faster (and this is an operation that has to be done as part of the message sending fast path) and Apple probably doesn't really need more than 8 tagged classes anyway, at least for now.</description><guid isPermaLink="true">515575ed51aa8e58700b3a0dd3aa2889</guid><pubDate>Tue, 22 Nov 2016 15:10:09 GMT</pubDate></item><item><title>Alex - 2016-11-20 17:40:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>"For whatever reason, the Objective-C implementation of tagged pointers has the bottom bit always set to 1 in a tagged pointer, with the other three bits indicating the class of the tagged pointer. I'm not sure why they chose to implement it in this way"
&lt;br /&gt;
&lt;br /&gt;This is how every implementation of tagged pointers I've seen works.  How else would you implement it?</description><guid isPermaLink="true">7e1d91913923de52dd919d861fa196ae</guid><pubDate>Sun, 20 Nov 2016 17:40:26 GMT</pubDate></item><item><title>J Osborne - 2012-08-17 18:06:02</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>A less historic bit addressable machine then the C205 would be the TI34010 and TI34020 which are a late 1980's, early 1990's combination of a general purpose CPU and a graphics processor.
&lt;br /&gt;
&lt;br /&gt;There was even a C compiler available.   More then one in fact.
&lt;br /&gt;
&lt;br /&gt;Well technically those are the CPUs, the machines would be all of MicroProse's arcade line (F-15 Strike Eagle, B.O.T.T.S., the tank game that I worked on but can't remember the name of), and also NARC and Hard Drivin' (which at the time I thought was a 680x0 with custom microcode, but I guess I was mistaken).
&lt;br /&gt;
&lt;br /&gt;Of corse in addition to being bit addressable these machines also had polygon painting instructions :-)
&lt;br /&gt;</description><guid isPermaLink="true">af5c93fbc53fecbc127da143ced89a1c</guid><pubDate>Fri, 17 Aug 2012 18:06:02 GMT</pubDate></item><item><title>Matthew Burke - 2012-08-14 16:31:09</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>Mike, a very lucid explanation (as always).  
&lt;br /&gt;
&lt;br /&gt;For anybody interested in another implementation of this idea, I recommend reading &lt;a href="http://www.lua.org/doc/jucs05.pdf"&gt;http://www.lua.org/doc/jucs05.pdf&lt;/a&gt; which describes Lua's implementation.  Section 3 of the paper describes how values are represented. The paper is a little out of date--it describes Lua 5.0; the current version is 5.2.  For specific details, Lua's source is quite readable.  The relevant portion is found at &lt;a href="http://www.lua.org/source/5.2/lobject.h.html"&gt;http://www.lua.org/source/5.2/lobject.h.html&lt;/a&gt;.</description><guid isPermaLink="true">33ef403d3e335f7de71e0e43ab5baf42</guid><pubDate>Tue, 14 Aug 2012 16:31:09 GMT</pubDate></item><item><title>mikeash - 2012-08-13 18:09:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>&lt;b&gt;Warmi:&lt;/b&gt; I don't think that's an accurate characterization. These are still objects, they're just objects that can be stored directly within the pointer. As for system-specific non-portable hacks, that's true but also applies to everything your compiler outputs, as well as a good chunk of the runtime. (The &lt;code&gt;objc_msgSend&lt;/code&gt; is written in assembly, and is definitely &lt;i&gt;not&lt;/i&gt; portable.)
&lt;br /&gt;
&lt;br /&gt;Not everything has to be portable. Tagged pointers can be turned off at the flip of a switch and everything keeps working fine, just a bit more slowly, so there's literally no risk here. If Apple introduces a platform where the current code doesn't work, they can either fix it up to work there, or they can turn it off.
&lt;br /&gt;
&lt;br /&gt;I'm a bit surprised by the negative reactions to this stuff from some of the commenters. This is an ancient and time-honored technique that has many upsides and no downside whatsoever the way Apple implemented it, aside from the (relatively minor) engineering time needed to build it.</description><guid isPermaLink="true">75923b8c58d5495ce1724fdf567c9d39</guid><pubDate>Mon, 13 Aug 2012 18:09:55 GMT</pubDate></item><item><title>Warmi - 2012-08-11 05:42:05</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>Hmm so you come up with a language/framework where you declare that everything is an object ... And then you go ahead and implement incredibly system specific and non portable hacks because it turns out that making everything into an object makes your code crawl.
&lt;br /&gt;
&lt;br /&gt;Clever.</description><guid isPermaLink="true">9a72b1c77424e7b2f2595b4e23d43a5f</guid><pubDate>Sat, 11 Aug 2012 05:42:05 GMT</pubDate></item><item><title>Dave Polaschek - 2012-08-06 13:55:29</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>Historical note - there were systems (the Control Data Cyber 205 was one) in which pointers had no alignment restrictions - pointers were 64-bit (the native word size of the machine) but were BIT-aligned. The C205 didn't have a rotate or bit-shift instruction. If you wanted such a result, you simply set your pointer to the correct bit of the word and read (or wrote) the data that way. Yeah, there would be a performance-hit (3 clocks instead of 1 for the read or write, IIRC) if your pointer &amp;amp; 0x40 was non-zero, and you had to be careful if you didn't want to affect the next word in memory, but it would work.</description><guid isPermaLink="true">5fcb2d5348b883be7f00909b7070a7db</guid><pubDate>Mon, 06 Aug 2012 13:55:29 GMT</pubDate></item><item><title>NIkita Pestrov - 2012-08-03 21:31:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>Here is the russian version of the Theory part)
&lt;br /&gt;&lt;a href="http://habrahabr.ru/post/149012/"&gt;http://habrahabr.ru/post/149012/&lt;/a&gt;</description><guid isPermaLink="true">74712f20d19ede2878e4c86590d9943e</guid><pubDate>Fri, 03 Aug 2012 21:31:58 GMT</pubDate></item><item><title>Nikita Pestrov - 2012-08-03 17:36:09</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>Thank you for a great article, Mike!</description><guid isPermaLink="true">4d3da2d7547aaea9f5df6f363ed6bb6b</guid><pubDate>Fri, 03 Aug 2012 17:36:09 GMT</pubDate></item><item><title>Richard Stacpoole - 2012-07-30 02:47:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>I was also thinking of the work done to make 32 bit clean code.
&lt;br /&gt;Not using any bits above the 48bits the hardware can read will help with this.  I just hope that tricks like this don't lead to other problems in the future.
&lt;br /&gt;Thanks for the replies.</description><guid isPermaLink="true">eae53a1a6123f86e94370d961cab19c2</guid><pubDate>Mon, 30 Jul 2012 02:47:15 GMT</pubDate></item><item><title>Daniel Eggert - 2012-07-29 10:52:54</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>It's worth noting that this optimisation is not really about saving memory. Yes, a tagged pointer NSNumber will use less memory than a "real" object, but the huge performance benefit comes from not having to allocate and deallocate objects.
&lt;br /&gt;
&lt;br /&gt;Creating a new object is very, very expensive as compared to creating a local variable.
&lt;br /&gt;
&lt;br /&gt;int a = 42;
&lt;br /&gt;int b = 128;
&lt;br /&gt;int c = a + b;
&lt;br /&gt;
&lt;br /&gt;is literally "for free" compared to
&lt;br /&gt;
&lt;br /&gt;id d = [[NSObject alloc] init];
&lt;br /&gt;
&lt;br /&gt;Allocation has to call malloc, which has to thread safe, may go off and mess with the virtual memory system etc. Then the object has to be initialised etc. There's a lot going on there that you don't see. Creating a tagged pointer for an NSNumber is orders of magnitude faster because it doesn't have to do any of that.</description><guid isPermaLink="true">803de375e80482dff73bba5f2ca58e90</guid><pubDate>Sun, 29 Jul 2012 10:52:54 GMT</pubDate></item><item><title>mikeash - 2012-07-27 19:07:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>&lt;b&gt;Chris L:&lt;/b&gt; Fortunately, there's no possibility for such trouble here. Since the creation of tagged pointers is limited to Apple frameworks, they are free to change the implementation at will to suit any new architecture changes that occur, without breaking any third-party code.</description><guid isPermaLink="true">3135c23032816a892ea156416f10737d</guid><pubDate>Fri, 27 Jul 2012 19:07:03 GMT</pubDate></item><item><title>bbum - 2012-07-27 15:52:22</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>It is only dangerous if you break the API contract.  In particular, direct access to the "isa" of an object was deprecated with the modern runtime.   Since all of the other runtime functions (and NSObject API) are aware of tagged pointers, a tagged pointer behaves just like any other object when viewed through the public API. </description><guid isPermaLink="true">57bb3b1689dd6c36470b5f74380c0f46</guid><pubDate>Fri, 27 Jul 2012 15:52:22 GMT</pubDate></item><item><title>Chris L - 2012-07-27 15:14:00</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>It reminds me of when a lot of Mac software had to be rewritten to be "32-bit clean".</description><guid isPermaLink="true">c695a75132186ed3b172b5acfbbc6e08</guid><pubDate>Fri, 27 Jul 2012 15:14:00 GMT</pubDate></item><item><title>mikeash - 2012-07-27 15:03:52</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>&lt;b&gt;Richard Stacpoole:&lt;/b&gt; What exactly is dangerous about it? It's tricky, but if you do it correctly, it works fine.</description><guid isPermaLink="true">805f4e25b9b156c735d627abee491835</guid><pubDate>Fri, 27 Jul 2012 15:03:52 GMT</pubDate></item><item><title>Nicolas Bouilleaud - 2012-07-27 14:27:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>I made a few experiments with Tagged Pointers a while ago on Lion. 
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://github.com/n-b/TaggedPointersTests"&gt;http://github.com/n-b/TaggedPointersTests&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;One interesting finding is that although Apple did the "safe" thing with NSNumbers representing doubles, they went with all the trouble for NSDate : NSDate objects are conceptually equivalent to an NSTimeInterval (a double). If the double value "fits" in the 7 first bytes, the NSDate is actually a tagged pointer.</description><guid isPermaLink="true">3796d0d530a85511ea30c2328cc5df49</guid><pubDate>Fri, 27 Jul 2012 14:27:25 GMT</pubDate></item><item><title>Richard Stacpoole - 2012-07-27 14:16:10</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html#comments</link><description>Isn't even Apple using this just asking for trouble?  I appreciate code performance improvements as much as the next person but do we really need to play such dangerous games?
&lt;br /&gt;</description><guid isPermaLink="true">f403bd9b11aab0b3cc8cb9f015c77479</guid><pubDate>Fri, 27 Jul 2012 14:16:10 GMT</pubDate></item></channel></rss>
