Skip to content

8341566: Add Reader.of(CharSequence)#21371

Closed
mkarg wants to merge 24 commits intoopenjdk:masterfrom
mkarg:charsequencereader
Closed

8341566: Add Reader.of(CharSequence)#21371
mkarg wants to merge 24 commits intoopenjdk:masterfrom
mkarg:charsequencereader

Conversation

@mkarg
Copy link
Contributor

@mkarg mkarg commented Oct 5, 2024

This Pull Requests proposes an implementation for JDK-8341566: Adding the new method public static Reader Reader.of(CharSequence) will return an anonymous, non-synchronized implementation of a Reader for each kind of CharSequence implementation. It is optimized for String, StringBuilder, StringBuffer and CharBuffer.

In addition, this Pull Request proposes to replace the implementation of StringReader to become a simple synchronized wrapper around Reader.of(CharSequence) for the case of String sources. To ensure correctness, this PR...

  • ...simply moved the original code of StringBuilder to become the de-facto implementation of Reader.of(), then stripped synchronized from it on the left hand, but kept just a synchronized wrapper on the right hand. Then added a switch for optimizations within the original code, at the exact location where previously just an optimization for String lived in.
  • ...added tests for all methods (Of.java), and applied that test upon the modified StringBuilder.

Wherever new JavaDocs were added, existing phrases from other code locations have been copied and adapted, to best match the same wording.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8341596 to be approved
  • Change must be properly reviewed (2 reviews required, with at least 2 Reviewers)

Issues

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/21371/head:pull/21371
$ git checkout pull/21371

Update a local copy of the PR:
$ git checkout pull/21371
$ git pull https://git.openjdk.org/jdk.git pull/21371/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 21371

View PR using the GUI difftool:
$ git pr show -t 21371

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/21371.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 5, 2024

👋 Welcome back mkarg! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 5, 2024

@mkarg This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8341566: Add Reader.of(CharSequence)

Reviewed-by: rriggs, jpai, liach, alanb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 253 new commits pushed to the master branch:

  • afb62f7: 8342683: Use non-short forward jump when passing stop()
  • 964d8d2: 8340445: [PPC64] Wrong ConditionRegister used in ppc64.ad: flagsRegCR0 cr1
  • 7131f05: 8342043: Split Opaque4Node into OpaqueTemplateAssertionPredicateNode and OpaqueNotNullNode
  • 37cfaa8: 8338449: ubsan: division by zero in sharedRuntimeTrans.cpp
  • a1ef818: 8342825: Fix order of @param tags in module java.desktop
  • cdad728: 8342646: JTREG_TEST_THREAD_FACTORY in testing.md should be TEST_THREAD_FACTORY
  • 018db8c: 8342809: C2 hits "assert(is_If()) failed: invalid node class: Con" during IGVN due to unhandled top
  • f1f1537: 8341453: java/awt/a11y/AccessibleJTableTest.java fails in some cases where the test tables are not visible
  • 476d0f1: 8339309: unused-variable warnings happen in libfontmanager
  • d6eddcd: 8327624: Remove VM implementation that bypass verification for core reflection
  • ... and 243 more: https://git.openjdk.org/jdk/compare/f8db3a831b61bb585c5494a7a8657e37000892b4...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@RogerRiggs, @jaikiran, @liach, @AlanBateman) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 5, 2024
@openjdk
Copy link

openjdk bot commented Oct 5, 2024

@mkarg The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Oct 5, 2024
@mlbridge
Copy link

mlbridge bot commented Oct 5, 2024

@AlanBateman
Copy link
Contributor

/csr

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label Oct 6, 2024
@openjdk
Copy link

openjdk bot commented Oct 6, 2024

@AlanBateman has indicated that a compatibility and specification (CSR) request is needed for this pull request.

@mkarg please create a CSR request for issue JDK-8341566 with the correct fix version. This pull request cannot be integrated until the CSR request is approved.

@AlanBateman
Copy link
Contributor

AlanBateman commented Oct 6, 2024

Reader.of(CharSequence) looks much better than introducing CharSequenceReader. It won't have an equivalent on Writer but I think that is okay. Also it means that the user will need to deal with close throwing IOException but anything using Reader has to do this already.

I think it would be better to drop "API compatible with StringReader" from the method description. An apiNote in StringReader can direct readers to the static factory method.

Also I think drop the "lock" field from the API docs as it's a protected field and only interesting to subclasses. The Reader class does not specify if Reader is thread-safe so the method description doesn't need to say too much. For clarify then it could just say something like "the resulting Reader is not safe for use by multiple concurrent threads. If the Reader is to be used by more than one thread it should be controlled by appropriate synchronization".

The parameter name is currently "c", maybe you mean "cs"? The method will need to specify NPE for the of(null) case.

Updated JavaDocs according to Alan Bateman's review comments:
* Dropped "API compatible with StringReader" from description
* @APinote in StringReader refers to static factory method
* Dropped "lock" field from API docs
* Added "The resulting Reader is not safe for use by multiple concurrent threads. If the Reader is to be used by more than one thread it should be controlled by appropriate synchronization"
* Deviating from your proposal, I renamed parameter "c" to "source" for clarity as the name "cs" already exists as an internal variable
* Method specifies NPE for `of(null)` case

I addition, JavaDocs now says "reader", not "stream" anymore.
@mkarg
Copy link
Contributor Author

mkarg commented Oct 6, 2024

Thank you, Alan! I have updated the JavaDocs according to your comments:

  • Dropped "API compatible with StringReader" from description
  • @APinote in StringReader refers to static factory method
  • Dropped "lock" field from API docs
  • Added "The resulting Reader is not safe for use by multiple concurrent threads. If the Reader is to be used by more than one thread it should be controlled by appropriate synchronization"
  • Deviating from your proposal, I renamed parameter "c" to "source" for clarity as the name "cs" already exists as an internal variable
  • Method specifies NPE for of(null) case

I addition, JavaDocs now says "reader", not "stream" anymore.

Dropping non-public JavaDocs
@mkarg
Copy link
Contributor Author

mkarg commented Oct 6, 2024

/csr

@AlanBateman Can you please review the CSR request so I can finish it? Thanks!

…rts the mark and reset ... could move up to become the second paragraph.'
@mkarg
Copy link
Contributor Author

mkarg commented Oct 23, 2024

Thank you, Alan. I now have adjusted the JavaDocs to your exact proposals.

Kindly requesting final reviews from everybody, so we can finally move this PR through the door. Thank you! :-)

@mkarg
Copy link
Contributor Author

mkarg commented Oct 23, 2024

@liach Alan's latest minor edit request auto-removed your approval from this PR. Kindly asking to reapply your approval. Thanks.

@liach
Copy link
Member

liach commented Oct 23, 2024

The javadoc text changes look fine; since this does not constitute specification changes, we don't need to re-draft our CSR to reflect the text changes.

Copy link
Member

@liach liach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff from Jaikiran and Roger's latest review is: 487b938...97e3144

There's no implementation change, so assuming the previous reviewers' approvals hold.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 23, 2024
@mkarg
Copy link
Contributor Author

mkarg commented Oct 23, 2024

I do share Chen's assumption, therefore requesting integration now.

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Oct 23, 2024
@openjdk
Copy link

openjdk bot commented Oct 23, 2024

@mkarg
Your change (at version 97e3144) is now ready to be sponsored by a Committer.

@liach
Copy link
Member

liach commented Oct 23, 2024

Please allow us to run some internal testing to ensure there won't be intermittent test failures before anyone sponsors this patch.

Copy link
Member

@jaikiran jaikiran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.

@kevinb9n
Copy link
Contributor

kevinb9n commented Oct 23, 2024

An issue with the simple name of is that we may encounter calls like this:

Reader.of("/usr/share/dict/words")

... that may look like they do something different. I can't say whether we should consider this a serious issue or not, though. I don't know if it justifies a big ugly name like ofCharSequence or not.

@liach
Copy link
Member

liach commented Oct 23, 2024

I think this scenario has a close analogy with java.util.Scanner constructors. Does such a fault happen to Scanner often?

@mkarg
Copy link
Contributor Author

mkarg commented Oct 24, 2024

An issue with the simple name of is that we may encounter calls like this:

Reader.of("/usr/share/dict/words")

... that may look like they do something different. I can't say whether we should consider this a serious issue or not, though. I don't know if it justifies a big ugly name like ofCharSequence or not.

There always will be a small group this misunderstands things and does not read JavaDocs, we can never find "the perfect API" to prevent this completely. The question is if we need to make the API worse in turn and bother all others with ugly lengthy names? I personally think the actual risk here is negligible, and the solution ("RTFM") is rather simple to implement. ;-)

To sum up, IMHO as soon as Chen finished the internal tests, we should close this discussion and merge this PR as-is.

@liach
Copy link
Member

liach commented Oct 24, 2024

Thanks Markus! The java.io tests succeed with repeats and the only test failures in tier 1-3 were ones problem-listed by f7a61fc.

FYI, besides Kevin's question, there was another concern about this API's interaction with BufferedReader (as you know, many Reader users wrap any Reader into a BufferedReader, especially you want to read all lines from a reader). We currently decide that we might explore line reading capability to Reader, but anyways that will not affect this patch.

And I have consulted with Kevin in private; he thinks this minor confusion is fine.

Sponsoring.

/sponsor

@openjdk
Copy link

openjdk bot commented Oct 24, 2024

Going to push as commit 3c14c2b.
Since your change was applied there have been 270 commits pushed to the master branch:

  • b0ac633: 8342075: HttpClient: improve HTTP/2 flow control checks
  • 85774b7: 8342882: RISC-V: Unify handling of jumps to runtime
  • 2c31c8e: 8339730: Windows regression after removing ObjectMonitor Responsible
  • f0b130e: 8339296: Record deconstruction pattern in switch fails to compile
  • e96b4cf: 8342387: C2 SuperWord: refactor and improve compiler/loopopts/superword/TestDependencyOffsets.java
  • f7a61fc: 8342931: ProblemList failing tests from JDK-8335912
  • 25c2f48: 8338544: Dedicated Array class descriptor implementation
  • 158b93d: 8335912: Add an operation mode to the jar command when extracting to not overwriting existing files
  • 28d23ad: 8340177: Malformed system classes loaded by bootloader crash the JVM in product builds
  • 98403b7: 8342854: [JVMCI] Block secondary thread reporting a JVMCI fatal error
  • ... and 260 more: https://git.openjdk.org/jdk/compare/f8db3a831b61bb585c5494a7a8657e37000892b4...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 24, 2024
@openjdk openjdk bot closed this Oct 24, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Oct 24, 2024
@openjdk
Copy link

openjdk bot commented Oct 24, 2024

@liach @mkarg Pushed as commit 3c14c2b.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@mkarg mkarg deleted the charsequencereader branch October 24, 2024 15:44
@mkarg
Copy link
Contributor Author

mkarg commented Nov 23, 2024

I have drafted a release note for JDK-8341566 in JDK-8344910. Kindly asking for reviews! :-)

@AlanBateman
Copy link
Contributor

I have drafted a release note for JDK-8341566 in JDK-8344910. Kindly asking for reviews! :-)

Thanks for creating this. I've edited it so that it is now focused on highlighting the new API and nothing else. Chen had some good suggestions too.

@mlbridge
Copy link

mlbridge bot commented Nov 24, 2024

Mailing list message from Markus Karg on core-libs-dev:

Thank you so much for your kind reviews, Alan and Chen! :-)

I have slighly extended Alan's draft to let people better understand the differences, hence to be able to actively chose between StringReader and Reader.of(CharSequence):

"The static factory method `java.io.Reader.of(CharSequence)` has been added in get a `Reader` that reads characters from a `CharSequence` (`String` and `StringBuilder` are examples of a `CharSequence`). The returned `Reader` is more efficient than using a `java.io.StringReader` in some cases, as the latter requires conversion to `String` and synchronization."

Is that OK? I see the risk that people cannot make a well-founded decision without.

I am not a native English speaker. Do we need to add "TO" in "...added in TO get a Reader..."?

-Markus

-----Urspr?ngliche Nachricht-----
Von: core-libs-dev [mailto:core-libs-dev-retn at openjdk.org] Im Auftrag von Alan Bateman
Gesendet: Sonntag, 24. November 2024 15:50
An: core-libs-dev at openjdk.org
Betreff: Re: RFR: 8341566: Add Reader.of(CharSequence) [v15]

On Sat, 23 Nov 2024 15:08:38 GMT, Markus KARG <duke at openjdk.org> wrote:

I have drafted a release note for [JDK-8341566](https://bugs.openjdk.org/browse/JDK-8341566) in [JDK-8344910](https://bugs.openjdk.org/browse/JDK-8344910). Kindly asking for reviews! :-)

Thanks for creating this. I've edited it so that it is now focused on highlighting the new API and nothing else. Chen had some good suggestions too.

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

PR Comment: https://git.openjdk.org/jdk/pull/21371#issuecomment-2496042639

@mlbridge
Copy link

mlbridge bot commented Nov 24, 2024

Mailing list message from Roger Riggs on core-libs-dev:

Hi Markus,

Thanks for drafting the release note and improvments.

Slight update "add in get a" -> "added to get a".

Regards, Roger

p.s.? An Oracle Tech writer also reviews and may do minor cleanup to
each release note to give a consistent use of terms and format.

On 11/24/24 11:12 AM, Markus Karg wrote:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.