Тёмный
No video :(

Class Layout - Miloš Anđelković - Meeting C++ 2023 

Meeting Cpp
Подписаться 32 тыс.
Просмотров 2 тыс.
50% 1

Class Layout - Miloš Anđelković - Meeting C++ 2023
Slides: slides.meeting...
Survey: survey.meeting...
It's a regular thing to write a class that will later be used in various ways trough the application. But how often do we stop and thing about what the compiler does with it, what the compiler can do with it, and finally how the class ends up looking in memory. To novice programmers there are some rules and behaviors that might be surprising. Here we will take a stroll trough the C++ documentation and see what the compiler is allowed, by law, to do, define some terms, see what tools we can use to peek under the hood and better understand our code. All of this of course accompanied with some simple examples because seeing is believing. And finally this entire exercise would be useless unless we see what actual impact this has on performance.
This is intended to be beginner friendly so it will not cover every edge case and things like virtual inheritance and vtable pointers, but even more experienced programmers might find some interesting nuggets of information.

Опубликовано:

 

27 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 4   
@wolpumba4099
@wolpumba4099 7 месяцев назад
*Summary* *Introduction and Speaker Background* - 00:00:00 Introduction and thanks for attending. - 00:00:03 Mention of T-shirt with "move semantics" in Serbian. - 00:00:37 Speaker's origin from Serbia, currently living in Prague. - 00:00:44 Speaker's role as a teaching assistant and interaction expectations. *Creating a Class in Programming* - 00:01:45 Discussion on creating a class, including member variables and functions. - 00:02:09 The usual process of writing a class or struct in code. - 00:02:28 Warning against indiscriminate addition of elements to a class. - 00:02:42 Advice on learning the SOLID principles. *Programming Language and Code Layout* - 00:03:02 Speaker discusses the complexity of language and code layout. - 00:03:15 Example struct provided for explanation. - 00:03:29 Introduction to CPU ref and rules of game in coding. - 00:03:46 Exclusion of virtual inheritance in the talk for simplicity. *Non-Static Member Variables and Class Layout* - 00:04:12 Focus on non-static member variables affecting class size. - 00:05:00 Guarantee of continuous memory allocation for objects. - 00:05:19 Uncertainty in the order of member variables. *Changes in C++ Standards Over Time* - 00:06:28 Evolution of layout rules from pre-C++11 to C++23. - 00:07:24 Changes in member variable layout post C++11. - 00:07:59 Discussion on why layout rules exist and their impact. - 00:08:38 C++23 standardizing layout order as per declaration. - 00:09:33 Introduction of new paper allowing customizable layout. *Object and Subobject Definitions and Requirements* - 00:10:28 Explanation of subobjects and complete objects. - 00:11:15 Size requirements for objects and subobjects. - 00:12:37 Distinct address requirements for different objects. *Alignment in Programming* - 00:13:31 Discussion on alignment requirements for types. - 00:14:01 Usage of alignment operators and functions. - 00:15:06 Importance of proper alignment for performance. - 00:16:00 Performance impact of misalignment illustrated with an example. *Misalignment and Data Layout* - 00:16:50 Importance of avoiding data misalignment. - 00:17:04 Encouragement to ask questions during the presentation. *Inheritance and Object Size* - 00:17:20 Example with three base classes and inheritance. - 00:17:48 Size analysis of base classes and the inherited class. - 00:18:39 Discussion on the layout of base class sub-objects. - 00:19:06 Explanation of empty base classes taking one byte. *Compiler Dependencies and Variations* - 00:19:56 Compiler-dependent behavior in empty base class optimization. - 00:20:48 Different compilers showing varying sizes for inheritance. *Composition vs. Inheritance* - 00:21:50 Comparison of composition and inheritance. - 00:22:24 Alignment impacting object size in composition. - 00:23:02 Limitations of empty base class optimization in composition. *Optimizing Member Variables* - 00:23:23 Introduction of the `no_unique_address` attribute. - 00:23:58 Applying `no_unique_address` for composition optimization. - 00:24:49 Compiler variations in attribute support. *Examples with Stateless Objects* - 00:25:14 Examples using multiple stateless objects. - 00:25:50 Compiler disagreements on object layout. - 00:26:55 The concept of standard layout types. *Standard Layout Rules* - 00:27:00 Rules for ensuring consistent layout across compilers. - 00:27:49 Tools for checking if a type is a standard layout. *Memory Layout Inspection Tools* - 00:28:08 Using `offsetof` to inspect memory layout. - 00:29:09 Limitations of `offsetof` in base class analysis. - 00:29:24 Compiler flags for dumping class layouts. - 00:30:14 Variation in flag usage across compilers. *Detailed Class Layout Examples* - 00:31:01 Examples showing detailed class layouts. - 00:31:49 Analysis of memory offsets and sizes in class layouts. - 00:32:37 Illustration of the `no_unique_address` effect on layout. - 00:33:31 Differences in layout with and without `no_unique_address`. *Understanding Base Classes and Member Variables in Layout* - 00:34:16 Explanation that base class subobjects and member variable subobjects are treated similarly in layout. - 00:34:49 Analysis of the "my struct" class layout including string implementation. *Alignment and Memory Wastage* - 00:36:11 Discussion on memory wastage due to alignment requirements. *Compiler Differences in Handling Inheritance* - 00:37:16 Examination of how MSVC handles inheritance differently. - 00:37:48 Comparison with Clang's handling of inheritance layout. - 00:38:56 Conclusion that compilers do not consistently agree on layout. *General Advice on Compiler Assumptions* - 00:39:12 Caution against making assumptions about compiler behavior. - 00:39:28 Speaker invites questions and takes a water break. *Performance Impact of Data Layout* - 00:39:35 Introduction to performance impact based on data layout. - 00:39:57 Comparison of 'good' and 'simple' data classes in terms of layout and size. - 00:41:07 Implementation of constructors and less-than operators for performance illustration. *Benchmark Setup and Methodology* - 00:41:45 Description of benchmark setup including hardware and software. - 00:42:47 Methodology for generating benchmark data. *Benchmark Results and Analysis* - 00:43:55 Presentation and analysis of benchmark results. - 00:46:14 Relative performance comparison between 'good' and 'simple' data in sorting. - 00:47:03 Accumulate operation performance, illustrating benefits of optimal data layout. *Audience Questions and Interaction* - 00:47:42 Audience interaction and questions about the presented data. - 00:48:53 Question regarding packing of 'simple' data for performance. - 00:50:27 Audience question about the conclusiveness of the results. *Concluding Remarks and References* - 00:50:41 Speaker's concluding remarks, noting variability in results across systems. - 00:51:36 Mention of original talk and different benchmark conditions. - 00:51:26 Comments on the consistency of data performance across tests. *Continued Discussion on Benchmark Results* - 00:51:44 Acknowledgement of the benchmark's limitations and invitation for critique. - 00:51:58 Emphasis on measuring performance before optimization. *Audience Questions on Data Consistency and Cache* - 00:52:17 Audience inquiry about data consistency and cache behavior. - 00:52:41 Speaker's admission of not conducting extensive analysis on consistency. *Discussion on Compiler Behavior and Padding* - 00:52:56 Speculation on compiler behavior regarding packing `bool` values. - 00:53:17 Humorous comment on Vector pool preferences for layout efficiency. - 00:53:34 Observation on the acceptability of padding in certain contexts. *Slide Review and Audience Queries* - 00:54:00 Request from audience to review a previous slide for clarity. - 00:54:21 Discussion on the consistency of comparison operations in sort benchmarks. *Further Clarification and Audience Interaction* - 00:55:51 Speaker's clarification on data generation and comparison consistency. - 00:56:03 Audience engagement and further inquiries. *Questions on 64-bit vs. 32-bit Environments* - 00:56:12 Query about the impact of using 64-bit versus 32-bit environments. - 00:57:13 Explanation of processor word size and its relation to data layout. *Concluding Remarks and Acknowledgements* - 00:57:41 Speaker's concluding statement and audience applause. Disclaimer: I used chatgpt4 to summarize the video transcript. I created the transcript with whisper base model.
@Roibarkan
@Roibarkan 7 месяцев назад
9:35 “Language support for class layout control” is paper P1112, which wasn’t accepted to any standard yet, but I believe that (currently) there is consensus in the committee (in EWG) to continue working on it aiming for standardization, perhaps C++26.
@Roibarkan
@Roibarkan 7 месяцев назад
55:55 Indeed I believe those examples of operator< aren’t conforming with std::sort and are considered undefined behavior. More about this topic can be found in this great cppcon talk: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-cMRyQkrjEeI.html
@milosandjelkovic3344
@milosandjelkovic3344 7 месяцев назад
I wish that talk was online before I gave this talk. In a later discussion, I realized I misunderstood the question, however my answer still somewhat stands, the operator< is inconsistent, but it's consistently inconsistent between the two struts, so it should be "fine" as long as it behaves the same. However, since this is an undefined behavior all bets are off.
Далее
Smoke 😱
00:26
Просмотров 1,2 млн
ЛОВИМ НОВЫХ МОНСТРОВ В LETHAL COMPANY
2:42:22
Секрет фокусника! #shorts
00:15
Просмотров 32 млн
Why Function Pointers are Awesome
11:11
Просмотров 6 тыс.
Delivering Safe C++ - Bjarne Stroustrup - CppCon 2023
1:29:16
Andrew Kelley   Practical Data Oriented Design (DoD)
46:40
Smoke 😱
00:26
Просмотров 1,2 млн