Structure size reduction #623

Open
opened 2025-12-27 06:09:51 -06:00 by irwir · 0 comments
irwir commented 2025-12-27 06:09:51 -06:00 (Migrated from github.com)

github.com/Mbed-TLS/TF-PSA-Crypto@e704e187b2/drivers/builtin/src/pk_wrap.h (L28-L33)
Visual C in 64-bit mode aligns the pointer to 8-byte address, and this means 4 bytes were lost twice - on padding of the first and the third fields.
Ideally, fields should be ordered by size in decreasing order (moved op_type down as it could be redeclared with char size):

typedef struct { 
     void *op; 
     mbedtls_svc_key_id_t pub_id; 
     mbedtls_pk_rs_op_t op_type; 
 } mbedtls_pk_psa_restartable_ctx_t; 

However, in this particular case, swapping of the second and the third fields should eliminate padding and reduce the structure size from 24 bytes to 16:

typedef struct { 
     mbedtls_pk_rs_op_t op_type; 
     mbedtls_svc_key_id_t pub_id; 
     void *op; 
 } mbedtls_pk_psa_restartable_ctx_t; 

Changed field order should not be an API break.

https://github.com/Mbed-TLS/TF-PSA-Crypto/blob/e704e187b2e90d5e7d97ffd4ad961b5a04ec8a13/drivers/builtin/src/pk_wrap.h#L28-L33 Visual C in 64-bit mode aligns the pointer to 8-byte address, and this means 4 bytes were lost twice - on padding of the first and the third fields. Ideally, fields should be ordered by size in decreasing order (moved `op_type` down as it could be redeclared with `char` size): ``` typedef struct { void *op; mbedtls_svc_key_id_t pub_id; mbedtls_pk_rs_op_t op_type; } mbedtls_pk_psa_restartable_ctx_t; ``` However, in this particular case, swapping of the second and the third fields should eliminate padding and reduce the structure size from 24 bytes to 16: ``` typedef struct { mbedtls_pk_rs_op_t op_type; mbedtls_svc_key_id_t pub_id; void *op; } mbedtls_pk_psa_restartable_ctx_t; ``` Changed field order should not be an API break.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-Mbed-TLS/TF-PSA-Crypto#623
No description provided.