RFR: 8364128: Improve gathering of cpu feature names using FormatBuffer

Ashutosh Mehra asmehra at openjdk.org
Tue Jul 29 18:22:07 UTC 2025


On Tue, 29 Jul 2025 07:16:58 GMT, Johan Sjölen <jsjolen at openjdk.org> wrote:

>> This PR implements the code for cpu features names string using FormatBuffer. It also improves and extends FormatBuffer with an additional method that appends comma separated strings to the buffer. This is useful in creating the cpu features names string.
>> This code will also be useful in Leyden to implement cpu feature check for AOTCodeCache [0].
>> Platforms affected: x86-64 and aarch64
>> Other platforms can be done if and when Leyden changes are ported to them.
>> 
>> [0] https://github.com/openjdk/leyden/pull/84
>
> src/hotspot/share/utilities/formatBuffer.hpp line 90:
> 
>> 88:     }
>> 89:     return;
>> 90:   }
> 
> This method seems overly specific to the use-case.
> 
> Something like this is more widely applicable.
> 
> ```c++
> // Append strings returned by gen, separating each with separator.
> // Stops when gen returns null or when buffer is out of space.
> template <typename Generator>
> void join(Generator gen, const char* separator) {
>     bool first = true;
>     const char* str = gen();
>     while (str != nullptr) {
>       const char* sep = first ? "" : separator;
>       int result = append("%s%s", sep, str);
>       if (result < 0) {
>         return;
>       }
>       first = false;
>     }
>   return;
> }
> 
> 
> Example usage:
> 
> ```c++
> void VM_Version::insert_features_names(uint64_t features, CpuInfoBuffer& info_buffer) {
>   int i = 0;
>   info_buffer.join([&]() {
>     while (!supports_feature((VM_Version::Feature_Flag)i) && i < MAX_CPU_FEATURES) {
>       i++;
>     }
>     if (i >= MAX_CPU_FEATURES) {
>       return nullptr;
>     }
>     return _features_names[i];
>   }, ", ");
>   assert(!info_buffer.overflow(), "not enough buffer size");
> }

+1
I can add this to the stringStream class.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26515#discussion_r2240617283


More information about the hotspot-dev mailing list