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

Ioi Lam iklam at openjdk.org
Fri Feb 20 04:39:58 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.

This pull request has now been integrated.

Changeset: 952ff4d7
Author:    Ioi Lam <iklam at openjdk.org>
URL:       https://git.openjdk.org/leyden/commit/952ff4d7ef17f9223ce74cd04326008a947736af
Stats:     27 lines in 3 files changed: 6 ins; 12 del; 9 mod

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

Reviewed-by: iveresov

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

PR: https://git.openjdk.org/leyden/pull/111


More information about the leyden-dev mailing list