does order of members of objects of a class have any impact on performance?

前端 未结 3 1902
小蘑菇
小蘑菇 2021-02-05 12:44

May order of members in binary architecture of objects of a class somehow have an impact on performance of applications which use that class? and I\'m wondering about how to dec

3条回答
  •  死守一世寂寞
    2021-02-05 13:09

    Absolutely agree with Potatoswatter. However one more point should be added about the CPU cache lines.

    If your application is multithreaded and different threads read/write members of your structure - it's very important to make sure those members are not within the same cache line.

    The point is that whenever a thread modifies a memory address that is cached in other CPU - that CPU immediately invalidates the cache line containing that address. So that improper members order may lead to the unjustified cache invalidation and performance degradation.

提交回复
热议问题