================
@@ -804,10 +804,38 @@ Check for performance anti-patterns when using Grand 
Central Dispatch.
 
 .. _optin-performance-Padding:
 
-optin.performance.Padding
-"""""""""""""""""""""""""
+optin.performance.Padding (C, C++, ObjC)
+""""""""""""""""""""""""""""""""""""""""
 Check for excessively padded structs.
 
+This checker detects structs with excessive padding, which can lead to wasted 
memory and decreased performance. Padding bytes are added by compilers to align 
data within the struct for performance optimization or memory alignment 
purposes. However, excessive padding can significantly increase the size of the 
struct without adding useful data, leading to inefficient memory usage, cache 
misses, and decreased performance.
+
+.. code-block:: c
+
+   #include <stdio.h>
+   // #pragma pack(1) // Uncomment it to disable structure padding
+   struct TestStruct {
+       char data1;  // 1 byte
+       char data2;  // 1 byte
+       int data3;   // 4 bytes
+   };             // Total size: 6 bytes
+   
+   int main () {
+       struct TestStruct struct1;
+       printf("Structure size: %d",sizeof(struct1)); // Structure size: 8
+       return 0;
+   }
+   
+Total memory used is 8 bytes due to structure padding. In this case, padding 
is of 2 bytes. Padding is done to decrease the number of CPU cycles needed to 
access data members of the structure, it increases the performance of the 
processor but at the penalty of memory.
+Padding can be disabled by using the pragma directive.
+Padding can also be decreased by putting data members of the structure in 
descending order of their size.
+
+Reports are only generated if the excessive padding exceeds ``AllowedPad`` in 
bytes. AllowedPad is the threshold value of padding.
+
+AllowedPad Option:
+- Default Value: 24 bytes
+- Usage: ``AllowedPad=<value>``
----------------
steakhal wrote:

I think you could just drop this, and just mention the default value in the 
previous paragraph.
Maybe add a note like:
> To enable set this option from the command-line, pass the `-analyzer-config 
> optin.performance.Padding:AllowedPad=24` option.

https://github.com/llvm/llvm-project/pull/86411
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to