[swift-dev] Question about size of Character type

Ole Begemann ole at oleb.net
Fri Aug 19 16:20:25 CDT 2016


Thanks Jordan!

> On 19 Aug 2016, at 23:04, Jordan Rose <jordan_rose at apple.com> wrote:
> 
> We have an old Radar about this, rdar://problem/16754935 <rdar://problem/16754935>. It's probably just a case we're missing in enum layout. My guess is that it's because we don't have a whole spare bit in a RawPointer, but we should be able to pick some up either from alignment or from ABI knowledge.
> 
> Jordan
> 
>> On Aug 19, 2016, at 13:30, Ole Begemann via swift-dev <swift-dev at swift.org <mailto:swift-dev at swift.org>> wrote:
>> 
>> The Character type is essentially defined like this [1]:
>> 
>> struct Character {
>>    enum Representation {
>>        // A _StringBuffer whose first grapheme cluster is self.
>>        // NOTE: may be more than 1 Character long.
>>        case large(_StringBuffer._Storage)
>>        case small(Builtin.Int63)
>>    }
>> 
>>    var _representation: Representation
>> }
>> 
>> Note the type of the associated value for `case .small`, `Builtin.Int63`. Presumably, this is so that the type fits into a single word, including the single bit that is needed for the enum case.
>> 
>> However, `MemoryLayout<Character>.size` returns 9. Given the above, I would have expected 8 bytes.
>> 
>> Why? Is this a potential optimization that hasn't been implemented? Or am I missing something?
>> 
>> Thanks
>> Ole
>> 
>> [1]: https://github.com/apple/swift/blob/master/stdlib/public/core/Character.swift <https://github.com/apple/swift/blob/master/stdlib/public/core/Character.swift>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160819/2fda62cf/attachment.html>


More information about the swift-dev mailing list