RFR: 8255455: Refactor ThreadHeapSampler::_log_table as constexpr [v2]
Ioi Lam
iklam at openjdk.java.net
Tue Oct 27 23:11:20 UTC 2020
On Tue, 27 Oct 2020 14:56:33 GMT, Claes Redestad <redestad at openjdk.org> wrote:
>> The static `ThreadHeapSampler::_log_table` is currently initialized on JVM bootstrap to an overhead of ~67k instructions (linux-x64). By turning the initialization into a constexpr, we can precalculate the helper table at compile time, which trades a runtime overhead for a small, 8kb, static footprint increase.
>>
>> I compared `fast_log2` with the `log2` builtin with a naive benchmarking experiment[1] (not included in this PR) and show that the `fast_log2` is ~2.5x faster than `log2` on my system. And that without the lookup table we'd be much worse. So I think it makes sense to preserve this optimization, but get rid of the startup overhead:
>>
>> [5.428s][debug][heapsampling] log2, 0.0751173 secs
>> [5.457s][debug][heapsampling] fast_log2, 0.0298244 secs
>> [5.622s][debug][heapsampling] fast_log2_uncached, 0.1645569 secs
>>
>> I've verified that this refactoring does not affect performance in this naive setup.
>>
>> [1] https://github.com/openjdk/jdk/compare/master...cl4es:log2_micro?expand=1
>
> Claes Redestad has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains three additional commits since the last revision:
>
> - Merge branch 'master' into threadHeapSampler_constexpr
> - Remove _log_table_initialized assert
> - Refactor ThreadHeapSampler::_log_table as constexpr
Changes requested by iklam (Reviewer).
src/hotspot/share/runtime/threadHeapSampler.cpp line 48:
> 46: };
> 47:
> 48: const FastLogTable<ThreadHeapSampler::FastLogCount> ThreadHeapSampler::_log_table;
To guarantee that `_log_table` is evaluated at C++ compile time, it's best to change the code to
constexpr FastLogTable<ThreadHeapSampler::FastLogCount> _log_table;
There are 2 reasons:
1. C++ guarantees compile-time evaluation only if the expression is used in a "constexpr context". You can read more from [here](https://isocpp.org/blog/2013/01/when-does-a-constexpr-function-get-evaluated-at-compile-time-stackoverflow).
2. In the future, if the `FastLogTable` constructor is modified in a way that cannot be evaluated at compile time, (e.g., someone removes `constexpr` from the constructor's declaration by mistake, the C++ compiler will catch that and tell you that `_log_table` cannot be `constexpr`.
Unfortunately, you cannot use `constexpr` in forward declarations, so you should either move the definition of `FastLogTable` to the hpp file, or remove the declaration of `_log_table` from the hpp file.
-------------
PR: https://git.openjdk.java.net/jdk/pull/880
More information about the hotspot-runtime-dev
mailing list