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

Igor Veresov iveresov at openjdk.org
Thu Feb 19 23:54:17 UTC 2026


On Wed, 18 Feb 2026 06:28:05 GMT, Ioi Lam <iklam 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

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

Marked as reviewed by iveresov (Committer).

PR Review: https://git.openjdk.org/leyden/pull/111#pullrequestreview-3829108895


More information about the leyden-dev mailing list