[clang] [Clang][analyzer] add documentation for optin performance padding (padding checker) #73675 (PR #86411)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 25 08:01:57 PDT 2024


================
@@ -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 {
----------------
steakhal wrote:

My problem with this `TestStruct` is that there is no way you could get rid of the two bytes padding using standard C++.

Maybe a better example would be if one could reorder the fields to actually benefit from the better layout.
As a rule of thump people should sort fields decreasing by alignment (or sizeof) to achieve the best layout. (maybe this technique could be also mentioned in the docs).

Lastly, this code example does not raise an issue of this checker, so it would be better to showcase a bug that we actually report.

Aside from that, we could also have a `OptimalStruct` that applies the technique I mentioned and demonstrates that the issue goes away.

https://github.com/llvm/llvm-project/pull/86411


More information about the cfe-commits mailing list