RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions

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


On Fri, 13 Feb 2026 16:02:52 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` .

src/hotspot/share/oops/cpCache.hpp line 199:

> 197: 
> 198:  public:
> 199:   static int size() { return align_metadata_size(sizeof_auto<ConstantPoolCache>() / wordSize); }

Surely, this should no be a part of this PR?

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.

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

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


More information about the hotspot-dev mailing list