RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions [v4]

Leo Korinth lkorinth at openjdk.org
Wed Feb 18 16:19:00 UTC 2026


On Wed, 18 Feb 2026 16:08:40 GMT, Leo Korinth <lkorinth at openjdk.org> wrote:

>> `sizeof_auto` is created so that we can reduce the amount of casting and thus make the code more type safe. The normal `sizeof` will return a `size_t` although the size of most stuff can be represented in a `uint8_t`. `sizeof_auto` will return the size in an as small unsigned type as is possible. The result is that expressions that uses `sizeof_auto` can convert to most integral types, and no explicit narrowing cast will be needed.
>> 
>> The change in `cpCache.hpp` is an example where we would get an implicit narrowing warning (if we had those enabled) using normal `sizeof`, but where `sizeof_auto` will not make the expression a `size_t` .
>
> Leo Korinth has updated the pull request incrementally with one additional commit since the last revision:
> 
>   add examples when it is good using sizeof/sizeof_auto

After a discussion at work, I have added two examples showing when to use `sizeof` and when to use `sizeof_auto`. At the discussion Kim also noted he was not all happy with the naming of `sizeof_auto`. We discussed using something with capital letters but others (and I) did not like it. I will give everyone some time to come up with a better name. I will probably try to integrate sometime middle next week if no better name has been agreed on. Also remember that the name can always be changed in the future as well.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/29716#issuecomment-3921747879


More information about the hotspot-dev mailing list