static_cast<void>(0) vs do while

David Holmes david.holmes at oracle.com
Mon Jan 15 02:48:53 UTC 2024


Hi Julian,

On 12/01/2024 3:24 pm, Julian Waters wrote:
> Hi all,
> 
> In my personal fork of HotSpot I have the following commit
> https://github.com/TheShermanTanker/jdk/commit/54131b70d40a88ab4176d23821f4c32044c0043d
> which replaces some occurrences of do while with a discarding
> static_cast (static_cast<void>(0) is a nop), to avoid inefficiencies
> in the compiled code for debug mode, when optimizations are turned off
> (Namely a compare and jump back to the start of the loop on a
> condition that is always false). Is this minor optimization worth
> committing upstream to HotSpot? It should also mean that the compiled
> code is clearer when the methods containing asserts are disassembled.

Sorry that just looks weird to me - I'm thinking "what on earth is a 
cast doing here?". `do { ... } while (0)` is a long standing idiomatic 
way to write a multi-line macro so that it can be used as a statement. I 
would think any compiler worth its salt would see the loop never repeats 
and just discard it.

Cheers,
David

> best regards,
> Julian


More information about the hotspot-dev mailing list