RFR: 8378150: Clean up C++ iterator style in compactHashtable.hpp and trainingData.hpp

Ioi Lam iklam at openjdk.org
Fri Feb 20 04:39:57 UTC 2026


On Thu, 19 Feb 2026 23:51:59 GMT, Igor Veresov <iveresov at openjdk.org> wrote:

>> We have some iterators that the iterator by reference. However, this requires the `const_cast<>` hack:
>> 
>> https://github.com/openjdk/leyden/blob/d2c9165dfbd32b592d4a4978ed17e2ead8b98357/src/hotspot/share/oops/trainingData.hpp#L220-L227
>> 
>> Without the `const_cast<Function&>` hack, the following code:
>> 
>> 
>> iterate([&](TrainingData* td) { td->verify(); })
>> 
>> will generate a C++ error:
>> 
>> 
>> trainingData.hpp:232:54: error: cannot bind non-const lvalue reference of type
>> 'TrainingData::TrainingDataSet::verify() const::<lambda(TrainingData*)>&' 
>> to an rvalue of type 
>> 'TrainingData::TrainingDataSet::verify() const::<lambda(TrainingData*)>'
>> 
>> 
>> The `const_cast` is incorrect, as it hides the fact that `fn` can be a mutable object.
>> 
>> **FIX**
>> 
>> - Change all `Function& fn` to `Function fn` in iterator templates
>> - Remove all `const_cast<>` hacks
>> - Wrap stateful iterator objects in a lambda function. The only case is `PrecompileIterator`.
>> 
>> **Note**
>> 
>> I am making a PR in the Leyden repo first as the `PrecompileIterator` code doesn't exist in mainline yet. Once this proves to work, I will upstream the relevant parts to the mainline.
>
> Looks good to me

Thanks @veresov for the review

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

PR Comment: https://git.openjdk.org/leyden/pull/111#issuecomment-3931599907


More information about the leyden-dev mailing list