[swift-dev] [Discussion] New refcount representation

Greg Titus greg at omnigroup.com
Wed Mar 16 15:49:57 CDT 2016

> On Mar 16, 2016, at 1:42 PM, Brent Royal-Gordon via swift-dev <swift-dev at swift.org> wrote:
> This is damned clever.

Yes, I agree!

>> The MSB bit also becomes set if you increment or decrement a retain count too far. That means we can implement the RR fast path with a single conditional branch after the increment or decrement:
> You don't talk much about it, but I think you're suggesting that the strong and unowned refcounts would still be packed into the same fields. It's worth noting that your clever high-bit-indicates-slow-path trick would *not* work on the unowned refcount, though that matters less since it doesn't get checked as often.

My understanding is that there would not be an unowned refcount here (inline in the object). The first time you make a weak reference to the object it gets converted to have the out-of-band refcount structure, and the strong and unowned refcounts both get stored there (in an undefined-by-this-proposal way).

>>       // out-of-object refcount     (MSB bits 0b10x)
>>       // or refcount has overflowed (MSB bits 0b111)
>>       // or refcount is constant    (MSB bits 0b110)
> Is this going to work on 32-bit? Even if we assume that valid pointers don't start with 0b1, can we assume that they don't start with 1b01?

The out-of-object refcount flag here is only 2 bits (0b10 in the high bits), so presumably you store the other 2 bits of the pointer in the low bits which would always be zero, assuming word-aligned pointers. So in 32-bit you can just left shift by 2 and still have the whole normal pointer length available.

	- Greg

More information about the swift-dev mailing list