Performance overhead using volatile for setjmp / longjmp

For setjmp / longjmp to work, you need to declare the local variable volatile If someone compiles their code with - O3, what is the performance impact of volatile variables Will it be huge or just a little on X86 multi-core platforms?

In my opinion, it will only add a little overhead, because volatile variables can still be cached and read / write from the cache is very fast anyway opinion?

Solution

In short, the semantics of volatile are platform / compiler dependent On some compilers (such as MSVC with IA64 Architecture), volatile keyword can not only prevent the compiler from reordering operations, but also perform each read / write operation using get / release semantics, which means that there is an effective memory barrier operation On the other hand, GCC can only prevent the compiler from reordering operations before / after reading / writing volatile memory locations... On platforms with weak memory models, the acquisition and release semantics do not maintain MSVC as they do

Now on X86, due to its strictly sorted memory model, the memory barrier in using volatile keyword is not a problem. Therefore, the main disadvantage is the lack of reordering and other possible optimizations, which are executed by the compiler Having said that, it will depend on what your code looks like For example, if there are tight loops in your code, and some volatile variables are actually loop invariants, if these memory locations are limited to non variables, you will not be able to obtain some optimizations that the compiler can perform- volatile.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>