Violet icon indicating copy to clipboard operation
Violet copied to clipboard

Compile time member offsets in PyObjects

Open LiarPrincess opened this issue 3 years ago • 0 comments

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.

LiarPrincess avatar May 25 '22 08:05 LiarPrincess