RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions [v2]
Albert Mingkun Yang
ayang at openjdk.org
Mon Feb 16 17:08:13 UTC 2026
On Mon, 16 Feb 2026 13:42:53 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:
>
> Fix test for platforms that can not handle unsigned max sizes
> it is checked at runtime, not compile time.
> it is checked only on debug builds
Both points are true, but for the specific goal here—reducing narrowing conversion warnings—I don’t think either of those limitations are material. The compiler will still complain at any remaining implicit narrowing sites, so we’re not relying on runtime checking to catch them.
> you need to use it, it will make the code harder to read, it is easier to use sizeof_auto
On the readability point, I think that’s somewhat subjective. Introducing a new `sizeof`-like API changes the type semantics of `sizeof`, which feels like a broader conceptual shift. Since we already have `checked_cast` to make narrowing explicit, using it seems more localized and avoids adding another abstraction to the codebase.
Just my 2c.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/29716#issuecomment-3909601472
More information about the hotspot-dev
mailing list