JAVA and .Net Complier Rearrange code lines feature.

來源:互聯網
上載者:User

In my previous post. I have been talking about the .Net JIT re-order instructions feature. Actually . In java world . 

The javac compiler has performs next to no optimisations.

The JIT native compiler could re-order instructions where there is a memory ordering problem. However, the CPU also can re-order instructions and memory updates which have the same effect.

What is achieved by allowing this freedm to the compiler ?

The main benefit is code portability. The more guarantees you provide, the more difficult it is to ensure every platform actually does this.

There is also a significant performance improvement by allowing the CPU to execute instructions as and when it can rather than in a strict order.

Is it really possible for the compiler to produce code which is more efficient by rearranging the code ?

Yes. but the re-ordering done by the CPU is more significant.

I am yet to see a practical case for this. I feel sometimes that the benefits if any are far outweighed by the concurrency risks this can introduce.

Is there any way that programmer can tell compiler not to rearrange lines like this ?

This is why you use memory barriers like volatilesynchronized blocks and Lock. When you use these you get thread safety guarantees.

I know that using synchronization primitives effectively handles the side-effects of rearranging , but I am asking if there is any direct way ( compiler option ) to turn this off ?

You can turn off the JIT, but most re-ordering is done by the CPU so it wouldn't achieve much.

Avoiding re-ordering of updates is such a small part of the thread safety problem (its biggest issue is that is obscure and rarely occurs which makes testing it hard) And once you write thread safe code, this is alleviated.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.