RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions

Johan Sjölen jsjolen at openjdk.org
Mon Feb 16 12:21:17 UTC 2026


On Mon, 16 Feb 2026 12:14:47 GMT, Johan Sjölen <jsjolen 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` .
>
> test/hotspot/gtest/utilities/test_globalDefinitions.cpp line 330:
> 
>> 328: };
>> 329: 
>> 330: TEST(globalDefinitions, sizeof_auto) {
> 
> I know that this isn't something that we follow today, but it's good practice to avoid underscores in test names when using Gtest. If you can call it `SizeofAuto` or `sizeofAuto` then I think that's preferred.

Nit: I think you can split these into multiple tests with more descriptive names overall, but that's up to you.

For example, L341-L346 could be in a test called `SizeofAutoReturnsTheNumberOfBytesInAnArray` or something like that. This is very much my opinion rather than our style guide, so truly a nit.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/29716#discussion_r2812150313


More information about the hotspot-dev mailing list