static_cast<void>(0) vs do while

Bechberger, Johannes johannes.bechberger at sap.com
Mon Jan 15 06:51:13 UTC 2024


Hi David and Julian,

I just checked it with GCC (https://godbolt.org/z/qcYjn6dn9) and Clang (https://godbolt.org/z/zvfnba5Tf) in Godbolt: GCC without any optimizations discards the loop, Clang replaces it with a single jump, MSVC (https://godbolt.org/z/saj1sreen) and ICC (https://godbolt.org/z/saj1sreenproduce) a  full loop.
The change is therefore only a minor optimization on MSVC and ICC.

Regards
Johannes

From: hotspot-dev <hotspot-dev-retn at openjdk.org> on behalf of David Holmes <david.holmes at oracle.com>
Date: Monday, 15. January 2024 at 03:49
To: hotspot-dev at openjdk.org <hotspot-dev at openjdk.org>
Subject: Re: static_cast<void>(0) vs do while
[You don't often get email from david.holmes at oracle.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

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://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTheShermanTanker%2Fjdk%2Fcommit%2F54131b70d40a88ab4176d23821f4c32044c0043d&data=05%7C02%7Cjohannes.bechberger%40sap.com%7C87b0fb3aa6d44bb13f8208dc15749167%7C42f7676cf455423c82f6dc2d99791af7%7C0%7C0%7C638408837602750422%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=dTWgX8aIQsZuS33M%2F047H4uu3L4ZRzQGIiwh%2FfKqdhc%3D&reserved=0<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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/hotspot-dev/attachments/20240115/105fb061/attachment-0001.htm>


More information about the hotspot-dev mailing list