[swift-dev] Question about size of Character type

Ole Begemann ole at oleb.net
Fri Aug 19 15:30:40 CDT 2016


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


More information about the swift-dev mailing list