RFR: 8330532: Improve line-oriented text parsing in HotSpot [v3]
John R Rose
jrose at openjdk.org
Tue Apr 23 06:11:27 UTC 2024
On Tue, 23 Apr 2024 00:51:54 GMT, Ioi Lam <iklam at openjdk.org> wrote:
>> 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.
I could be wrong about this, but it seems like it’s completely up to the C++ class to define what aspects of its implementation are fixed for its `const` methods. The `const` keyword does not mean “everything is immutable about this, at all levels and inside all boundaries”. But then it’s up to the class author to choose which boundary `const` applies to. The thing wrapped inside the i-stream is, I will claim, implementation which can change state even in `const` functions.
If not, the alternative is to have almost no `const` functions at all in the i-stream class. That’s less useful, because `const` means something very useful, in the context of the i-stream class. It means that the line will not shift. You can keep on reading, and even writing, the line buffer, as long as you call only `const` functions.
The reuse of the line buffer, in this way, is a core part of the i-stream design, and part of its value proposition: You don’t get a new allocation on every read-line op (as you would with Java). But in order to draw bright lines around THAT very useful notion of invariance (during which line buffers are reusable), you NEED `preload` to be const, even if it does internal book keeping, even if it consults the input source at times.
In short, a rigid idea of `const` is inconsistent with the carefully balanced performance characteristics of this design.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/18833#discussion_r1575689215
More information about the hotspot-dev
mailing list