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

Ioi Lam iklam at openjdk.org
Wed Feb 18 06:33:16 UTC 2026


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.

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

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

Changes: https://git.openjdk.org/leyden/pull/111/files
  Webrev: https://webrevs.openjdk.org/?repo=leyden&pr=111&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8378150
  Stats: 27 lines in 3 files changed: 6 ins; 12 del; 9 mod
  Patch: https://git.openjdk.org/leyden/pull/111.diff
  Fetch: git fetch https://git.openjdk.org/leyden.git pull/111/head:pull/111

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


More information about the leyden-dev mailing list