Skip to content

Commit

Permalink
Reduce the size of FabricInfo by reordering some members.
Browse files Browse the repository at this point in the history
On 32-bit systems this changes the size from 152 bytes to 144 bytes.

On 64-bit systems this changes the size from 168 bytes to 152 bytes.
  • Loading branch information
bzbarsky-apple committed Jul 26, 2023
1 parent 1d49d9e commit 160cf60
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,69 @@ class DLL_EXPORT FabricInfo
return TLV::EstimateStructOverhead(sizeof(uint16_t), Crypto::P256SerializedKeypair::Capacity());
}

NodeId mNodeId = kUndefinedNodeId;
FabricId mFabricId = kUndefinedFabricId;
FabricIndex mFabricIndex = kUndefinedFabricIndex;
static void StaticAsserts()
{
static_assert(sizeof(void *) == 4 || sizeof(void *) == 8,
"Unexpected pointer width, computation of expected size will be off.");

constexpr size_t expected_size = []() -> size_t {
// mNodeId, mFabricId, mCompressedFabricId are 8 bytes each.
size_t totalSize = 8 * 3;

// Root public key is a vtable pointer followed by 65 bytes of data,
// but the space it takes up needs to end on a void * alignment
// boundary. So it's 72 (= 69 + 3) bytes on 32-bit and 80 (= 73 + 7)
// bytes on 64-bit.
if constexpr (sizeof(void *) == 4)
{
totalSize += 72;
}
else
{
totalSize += 80;
}

// mFabricLabel is 33 bytes and has no alignment requirements.
totalSize += 33;

// mFabricIndex, mVendorId, mHasExternallyOwnedOperationalKey are
// 1, 2, 1 bytes respectively, and doing them in that order has no
// alignment issues.
totalSize += 1 + 2 + 1;

// mOperationalKey has to be aligned on a void * alignment
// boundary. We are 37 bytes past an 8-byte boundary here, so we
// have a 3-byte gap. Then the pointer itself. Then we need to end
// up 8-byte aligned, so on 32-bit we have another 4-byte gap.
if constexpr (sizeof(void *) == 4)
{
totalSize += 3 + 4 + 4;
}
else
{
totalSize += 3 + 8;
}

return totalSize;
}();

static_assert(sizeof(FabricInfo) == expected_size, "FabricInfo size unexpected N");
}

NodeId mNodeId = kUndefinedNodeId;
FabricId mFabricId = kUndefinedFabricId;
// We cache the compressed fabric id since it's used so often and costly to get.
CompressedFabricId mCompressedFabricId = kUndefinedCompressedFabricId;
// We cache the root public key since it's used so often and costly to get.
Crypto::P256PublicKey mRootPublicKey;

VendorId mVendorId = VendorId::NotSpecified;
char mFabricLabel[kFabricLabelMaxLengthInBytes + 1] = { '\0' };
mutable Crypto::P256Keypair * mOperationalKey = nullptr;
bool mHasExternallyOwnedOperationalKey = false;

FabricIndex mFabricIndex = kUndefinedFabricIndex;
VendorId mVendorId = VendorId::NotSpecified;
bool mHasExternallyOwnedOperationalKey = false;

mutable Crypto::P256Keypair * mOperationalKey = nullptr;

CHIP_ERROR CommitToStorage(PersistentStorageDelegate * storage) const;
CHIP_ERROR LoadFromStorage(PersistentStorageDelegate * storage, FabricIndex newFabricIndex, const ByteSpan & rcac,
Expand Down

0 comments on commit 160cf60

Please sign in to comment.