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 Tue, 23 Apr 2024 00:50:00 GMT, Ioi Lam <iklam at openjdk.org> wrote:
>> 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`.)
On a separate note, `preload()` can cause data to be read from the input. For non-seekable input (such as sockets), this doesn't seem like a `const` operation to me.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/18833#discussion_r1575510055
More information about the hotspot-dev
mailing list