Violet
Violet copied to clipboard
Compile time member offsets in PyObjects
Currently we use GenericLayout to calculate offsets of PyObject members (like this).
Instead we could do the usual offset/size dance:
extension PyObject {
public static let member2Offset = calculateOffset(
previousMemberOffset: Self.member1Offset,
previousMemberSize: MemoryLayout<Member1Type>.size,
alignment: MemoryLayout<Member2Type>.alignment
)
}
func calculateOffset(previousMemberOffset: Int, previousMemberSize: Int, alignment: Int) -> Int {
var offset = previousMemberOffset + previousMemberSize
return round(offset, to: alignment)
}
Though, I have not checked what Swift compiler emits for the current version, so maybe it is not needed.