o3de / sig-core

5 stars 6 forks source link

Update Best-Practice Guide to include guidance on reducing compile time cost #53

Open lemonade-dm opened 1 year ago

lemonade-dm commented 1 year ago

To help improve O3DE C++ build times, it is recommended to update the C++ Best Practice Guide, with options on how to best reduce compilation time going forward.

There are several ways to reduce O3DE compile going forward. One of which is reducing the amount of non-template inline functions. Other ways include

Useful macros are

Other Recommendations: In order to iterative improve build times, the following is recommended

  1. instead using the AZ_TYPE_INFO/AZ_RTTI macro directly within the declaration of a class in a header, the AZ_TYPE_INFO_WITH_NAME_DECL and AZ_RTTI_NO_TYPE_INFO_DECL macros are used to declare the TypeInfo and RTTI functions without defining them. In a cpp file the AZ_TYPE_INFO_WITH_NAME_IMPL and AZ_RTTI_NO_TYPE_INFO_IMPL macros can be used to define the TypeInfo/RTTI functions only once.
  2. Avoid adding inline definitions of functions in headers and instead aggressively move function definitions to translation units(.cpp files). Non-templated inline function bodies are compiled everytime they are included and it adds ups over time For example the PoolAllocator code was ranked for 4-7 in template functions that took the most time to instantiate contributing about 580 seconds(8 minutes 40 seconds) of compilation time. Now that time can get parallelized over the number of cores building at once, but even with 8 cores this adds up to over a minute
    155581 ms: AZ::Internal::PoolAllocatorHelper<AZ::PoolSchema>::RTTI_IsContainType (1117 times, avg 139 ms)
    143757 ms: AZ::Internal::PoolAllocatorHelper<AZ::PoolSchema>::TYPEINFO_Uuid (1117 times, avg 128 ms)
    143698 ms: AZ::Internal::PoolAllocatorHelper<AZ::PoolSchema>::RTTI_Type (1117 times, avg 128 ms)
    136643 ms: AZ::Internal::AggregateTypes<AZ::PoolSchema>::Uuid<AZ::CanonicalType... (1117 times, avg 122 ms)

    With the changes to move the PoolAllocator instantiations and TypeInfo functions to its cpp file, those functions now longer appear in the top 10000 templates that are instantiated.

AMZN-alexpete commented 10 months ago

Notes from recent meeting: There are possibly underlying design issue that could be addressed that are likely more effort but might address these issues:

  1. Using codegen instead of relying on templates could reduce compile times.
  2. At some point, the build in c++ RTTI might be faster and smaller than our implementations. As of most recent testing (by Alan?) the memory cost was 20% more than our implementation.