RFR: 8330532: Improve line-oriented text parsing in HotSpot [v2]

Ioi Lam iklam at openjdk.org
Tue Apr 23 00:58:04 UTC 2024


On Mon, 22 Apr 2024 22:00:58 GMT, John R Rose <jrose at openjdk.org> wrote:

>> src/hotspot/share/utilities/istream.hpp line 207:
>> 
>>> 205:       const_cast<inputStream*>(this)->fill_buffer();
>>> 206:     }
>>> 207:   }
>> 
>> Why `const_cast` and assign this method `const` when it clearly is not? This shouldn't be `const`, is my point.
>
> The method is const because the logical state of the stream is invariant, as visible to the API user.  If the implementation needs an invisible internal state change, it needs a const-cast (or mutable field, sometimes, will work).  If `preload` were made non-const as you suggest, we’d need to move the const-casting elsewhere, and it would be less clear that `preload` preserves API-visible state.  So the code, as it is, is the most convenient place to put the const-cast, as an internal implementation decision.

This page suggests using the `mutable` keyword for this situation (but `const_cast<>` is acceptable as a last resort)
https://isocpp.org/wiki/faq/const-correctness#mutable-data-members

However, to use `mutable` in this case, we would probably need to declare every field as `mutable`, this means that the meaning of `const` will be very difficult to understand (at least we can't use the C++ compiler to tell us which function can't (should) be `const` and which function cannot be `const`.)

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

PR Review Comment: https://git.openjdk.org/jdk/pull/18833#discussion_r1575509200


More information about the hotspot-dev mailing list