RFR: 8274400: HotSpot Style Guide should permit use of alignof [v3]

Xin Liu xliu at openjdk.org
Tue Jan 17 09:08:16 UTC 2023


On Fri, 30 Dec 2022 02:09:11 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:

> > What is the use-case for permitting use of `alignof`? Usually we don't add such permits without some use-case in mind.
> > [...]
> > I'm not opposed to permitting the use of `alignof`, but wondering why we might need it.
> 
> Responding to myself, I've found a usage category where `alignof` would be appropriate. We have various places where we are aligning a pointer to the size of some type, where it would be more appropriate to align to the alignment of that type. See, for example, `Message::calc_size()` (logging/logAsyncWriter.hpp), which uses `sizeof(void*)` where it seems like it should be using `alignof(Message)`.

Alignment of 'sizeof(void*)' here is a performance hint. We would like to see the address of next Message is aligned of pointer size. It eases the load instruction of 'Message::_output'. It's more effective to load from the aligned address. 
 

    static constexpr size_t calc_size(size_t message_len) {
      return align_up(sizeof(Message) + message_len + 1, sizeof(void*));
    }


In this context, alignof(Message) is correct but a little bit wasteful.

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

PR: https://git.openjdk.org/jdk/pull/11761


More information about the hotspot-dev mailing list