Hello.
My question pertains to _FixedArray16
in the stdlib and is really simple: Why increment the count first, only to subtract from it right after, in order to store newValue
?
extension _FixedArray16 {
internal mutating func append(_ newElement: T) {
_internalInvariant(count < capacity)
_count += 1
self[count-1] = newElement
}
}
While writing this down I realised that they are not the same count:
var _count: Int8
// ...
internal var count: Int {
@inline(__always) get { return Int(truncatingIfNeeded: _count) }
@inline(__always) set { _count = Int8(newValue) }
}
But still I can't understand why not do this instead:
self[count] = newElement
_count += 1
Is there a performance reason behind this choice assuming they don't lead to the same machine code? If there is, can someone explain it to me?
Thank you.