What are some tricks that a processor does to optimize code?

与世无争的帅哥 提交于 2020-01-20 18:11:40

问题


I am looking for things like reordering of code that could even break the code in the case of a multiple processor.


回答1:


Wikipedia has a fairly comprehensive list of optimization techniques here.




回答2:


The most important one would be memory access reordering.

Absent memory fences or serializing instructions, the processor is free to reorder memory accesses. Some processor architectures have restrictions on how much they can reorder; Alpha is known for being the weakest (i.e., the one which can reorder the most).

A very good treatment of the subject can be found in the Linux kernel source documentation, at Documentation/memory-barriers.txt.

Most of the time, it's best to use locking primitives from your compiler or standard library; these are well tested, should have all the necessary memory barriers in place, and are probably quite optimized (optimizing locking primitives is tricky; even the experts can get them wrong sometimes).




回答3:


Yes, but what exactly is your question?

However, since this is an interesting topic: tricks that compilers and processors use to optimize code should not break code, even with multiple processors, in the absence of race conditions in that code. This is called the guarantee of sequential consistency: if your program does not have any race conditions, and all data is correctly locked before accessing, the code will behave as if it were executed sequentially.

There is a really good video of Herb Sutter talking about this here:

http://video.google.com/videoplay?docid=-4714369049736584770

Everyone should watch this :)




回答4:


DavidK's answer is correct, however it is also very important to be aware of the memory model for your language/runtime. Even without race conditions and with sequential consistency and mutex usage your code can still break when data is being cached by different threads running in the different cores of the cpu. Some languages, Java is one example, ensure the state of data between threads when a mutex lock is used, but it is rarely enough to simply ensure that no two threads can access the data at the same time. You need to use the mutex in a correct way to ensure that the language runtime synchronizes the data state between the two threads. In java this is done by having the two threads synchronize on the same object.

Here is a good page explaining the problem and how it's dealt with in javas memory model.

http://gee.cs.oswego.edu/dl/cpj/jmm.html



来源:https://stackoverflow.com/questions/272246/what-are-some-tricks-that-a-processor-does-to-optimize-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!