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

Johan Sjölen jsjolen at openjdk.org
Tue Apr 23 10:34:29 UTC 2024


On Tue, 23 Apr 2024 06:08:36 GMT, John R Rose <jrose at openjdk.org> wrote:

>> 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.

I had to read up on this and I believe that John is right.

To quote https://isocpp.org/wiki/faq/const-correctness#const-member-fns

>The trailing const on inspect() member function should be used to mean the method won’t change the object’s abstract (client-visible) state. That is slightly different from saying the method won’t change the “raw bits” of the object’s struct.

I'm not objecting to the usage of `const` for this method, considering this. Reviewing code is learning on the job!

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

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


More information about the hotspot-dev mailing list