You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Serw <se...@gmail.com> on 2024/12/13 21:08:38 UTC
Subject: Proposal: Standardizing String Concatenation in Apache Commons
Dear Apache Commons Community,
In my recent PR #1332, I replaced a StringBuilder usage with string
concatenation in a toString method for improved readability. During the
review, it was suggested this change should align with a broader project
standard.
Would the community support using string concatenation over StringBuilder
for simple cases? Modern JDKs (8+) optimize string concatenation
efficiently, making it both concise and performant.
If agreed, I can implement consistent changes across the codebase.
Looking forward to your feedback!
PR - https://github.com/apache/commons-lang/pull/1332
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Gary Gregory <ga...@gmail.com>.
On Sat, Dec 14, 2024 at 1:56 PM Piotr P. Karwasz
<pi...@mailing.copernik.eu> wrote:
>
> Hi Gary,
>
> On 14.12.2024 17:45, Gary Gregory wrote:
> > What passes the criteria of "simple cases"?
>
> I would classify all sequences of `new StringBuilder()`, `append` and
> `toString` without any branching or loops as simple cases.
Sounds reasonable. Maybe "no control flow" is "simple".
>
> > Should this be enforced or can it be enforced? With Checkstyle? Somethings
> > else?
>
> This can be enforced by running the OpenRewrite rule I mentioned
> before[1] on a regular basis and check that nothing was actually rewritten.
>
> The only problem with applying OpenRewrite rule is that they can break
> the formatting of the source code. Do we have an automated way to
> reformat the source code to conform to the CheckStyle rules?
We do not. I use Eclipse, and some components have an
Eclipse-formatted XML config file. This is not great and not as good
as something like Spotless, which is used nowhere in Commons. It could
be introduced in commons-parent.
Gary
>
> [1]
> https://docs.openrewrite.org/recipes/staticanalysis/replacestringbuilderwithstring
>
> > Considering:
> > - that we support Java 8 and up to the current version.
> > - that we don't say which Java vendor we support.
> > - that the JLS (which version...) only suggests that a vendor could
> > implement string concatenation using one of the Java stock classes.
> >
> > Is there a risk that changing to not using our unsynchronized class have
> > performance side-effects?
>
> There is no bytecode equivalent to string concatenation, so it is up to
> the compiler to translate string concatenations in methods calls. If you
> compile for Java 8 you'll end up using `StringBuilder`. If you compile
> for Java 9 or higher, you'll end up with a specialized lambda.
>
> Piotr
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by "Piotr P. Karwasz" <pi...@mailing.copernik.eu>.
Hi Gary,
On 14.12.2024 17:45, Gary Gregory wrote:
> What passes the criteria of "simple cases"?
I would classify all sequences of `new StringBuilder()`, `append` and
`toString` without any branching or loops as simple cases.
> Should this be enforced or can it be enforced? With Checkstyle? Somethings
> else?
This can be enforced by running the OpenRewrite rule I mentioned
before[1] on a regular basis and check that nothing was actually rewritten.
The only problem with applying OpenRewrite rule is that they can break
the formatting of the source code. Do we have an automated way to
reformat the source code to conform to the CheckStyle rules?
[1]
https://docs.openrewrite.org/recipes/staticanalysis/replacestringbuilderwithstring
> Considering:
> - that we support Java 8 and up to the current version.
> - that we don't say which Java vendor we support.
> - that the JLS (which version...) only suggests that a vendor could
> implement string concatenation using one of the Java stock classes.
>
> Is there a risk that changing to not using our unsynchronized class have
> performance side-effects?
There is no bytecode equivalent to string concatenation, so it is up to
the compiler to translate string concatenations in methods calls. If you
compile for Java 8 you'll end up using `StringBuilder`. If you compile
for Java 9 or higher, you'll end up with a specialized lambda.
Piotr
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Gary Gregory <ga...@gmail.com>.
Hello serwserw293@gmail.com,
Thank you for your email.
Here are a few questions:
What passes the criteria of "simple cases"?
Should this be enforced or can it be enforced? With Checkstyle? Somethings
else?
Considering:
- that we support Java 8 and up to the current version.
- that we don't say which Java vendor we support.
- that the JLS (which version...) only suggests that a vendor could
implement string concatenation using one of the Java stock classes.
Is there a risk that changing to not using our unsynchronized class have
performance side-effects?
Ty!
Gary
On Sat, Dec 14, 2024, 10:08 AM Serw <se...@gmail.com> wrote:
> Dear Apache Commons Community,
>
> In my recent PR #1332, I replaced a StringBuilder usage with string
> concatenation in a toString method for improved readability. During the
> review, it was suggested this change should align with a broader project
> standard.
>
> Would the community support using string concatenation over StringBuilder
> for simple cases? Modern JDKs (8+) optimize string concatenation
> efficiently, making it both concise and performant.
>
> If agreed, I can implement consistent changes across the codebase.
>
> Looking forward to your feedback!
>
> PR - https://github.com/apache/commons-lang/pull/1332
>
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by "Piotr P. Karwasz" <pi...@mailing.copernik.eu>.
Hi Serw,
On 13.12.2024 22:08, Serw wrote:
> Would the community support using string concatenation over StringBuilder
> for simple cases? Modern JDKs (8+) optimize string concatenation
> efficiently, making it both concise and performant.
Since the Javac compiler replaces string concatenation with
StringBuilder, I wouldn't expect any performance improvement (at least
until we keep the JRE 8 compatibility).
Personally I find concatenated strings more concise and easy to read, so
I would be +1 for this change. There is even an OpenRewrite recipe[1]
that can apply it automatically.
Piotr
[1]
https://docs.openrewrite.org/recipes/staticanalysis/replacestringbuilderwithstring
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Peter Burka <pe...@quux.net>.
I'm not sure Sun/Oracle ever formally specified this behavior, but as a JVM
engineer since Java 0.98 I can attest to it. This behavior is also
described in JEP-280: "Currently javac translates String concatenation into
StringBuilder::append chains" (referring to Java 8).
The advantages of using StringBuilder explicitly, that I can think of, are:
* No risk of accidentally constructing multiple StringBuilders, e.g. (strA
+ strB) + (strC + strD)
* Ability to explicitly set the right capacity, e.g. new StringBuilder(new
size)
Peter
On Sun, Dec 15, 2024, 11:03 AM Elliotte Rusty Harold <el...@ibiblio.org>
wrote:
> On Sun, Dec 15, 2024 at 2:00 PM Peter Burka <pe...@quux.net> wrote:
> >
> > Before Java 9, javac always generated StringBuilder calls for string
> > concatenation (or StringBuffer prior to Java 2). Using + is less verbose,
> > generates the same code, is more readable, and, when we do finally bump
> the
> > compile target, will generate better code.
> >
>
> reference?
>
>
> --
> Elliotte Rusty Harold
> elharo@ibiblio.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Elliotte Rusty Harold <el...@ibiblio.org>.
On Sun, Dec 15, 2024 at 2:00 PM Peter Burka <pe...@quux.net> wrote:
>
> Before Java 9, javac always generated StringBuilder calls for string
> concatenation (or StringBuffer prior to Java 2). Using + is less verbose,
> generates the same code, is more readable, and, when we do finally bump the
> compile target, will generate better code.
>
reference?
--
Elliotte Rusty Harold
elharo@ibiblio.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Peter Burka <pe...@quux.net>.
Before Java 9, javac always generated StringBuilder calls for string
concatenation (or StringBuffer prior to Java 2). Using + is less verbose,
generates the same code, is more readable, and, when we do finally bump the
compile target, will generate better code.
Peter
On Sun, Dec 15, 2024, 7:13 AM Elliotte Rusty Harold <el...@ibiblio.org>
wrote:
> Thanks. In that case I'm -1 on this since it is not available in Java
> 8, and even in Java 9 is only possible, not necessarily implemented.
>
> We can revisit in a few years if the JDK has moved on by then.
>
> On Sun, Dec 15, 2024 at 1:05 AM Peter Burka <pe...@quux.net> wrote:
> >
> > I presume this is referring to
> > https://openjdk.org/jeps/280 (JEP 280: Indify String Concatenation)
> which
> > was implemented in Java 9.
> >
> > On Sat, Dec 14, 2024, 6:26 PM Elliotte Rusty Harold <el...@ibiblio.org>
> > wrote:
> >
> > > On Sat, Dec 14, 2024 at 3:08 PM Serw <se...@gmail.com> wrote:
> > >
> > > > Would the community support using string concatenation over
> StringBuilder
> > > > for simple cases? Modern JDKs (8+) optimize string concatenation
> > > > efficiently, making it both concise and performant.
> > >
> > > Interesting. Do you have a reference for that?
> > >
> > > --
> > > Elliotte Rusty Harold
> > > elharo@ibiblio.org
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > For additional commands, e-mail: dev-help@commons.apache.org
> > >
> > >
>
>
>
> --
> Elliotte Rusty Harold
> elharo@ibiblio.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Elliotte Rusty Harold <el...@ibiblio.org>.
Thanks. In that case I'm -1 on this since it is not available in Java
8, and even in Java 9 is only possible, not necessarily implemented.
We can revisit in a few years if the JDK has moved on by then.
On Sun, Dec 15, 2024 at 1:05 AM Peter Burka <pe...@quux.net> wrote:
>
> I presume this is referring to
> https://openjdk.org/jeps/280 (JEP 280: Indify String Concatenation) which
> was implemented in Java 9.
>
> On Sat, Dec 14, 2024, 6:26 PM Elliotte Rusty Harold <el...@ibiblio.org>
> wrote:
>
> > On Sat, Dec 14, 2024 at 3:08 PM Serw <se...@gmail.com> wrote:
> >
> > > Would the community support using string concatenation over StringBuilder
> > > for simple cases? Modern JDKs (8+) optimize string concatenation
> > > efficiently, making it both concise and performant.
> >
> > Interesting. Do you have a reference for that?
> >
> > --
> > Elliotte Rusty Harold
> > elharo@ibiblio.org
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
> >
--
Elliotte Rusty Harold
elharo@ibiblio.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Peter Burka <pe...@quux.net>.
I presume this is referring to
https://openjdk.org/jeps/280 (JEP 280: Indify String Concatenation) which
was implemented in Java 9.
On Sat, Dec 14, 2024, 6:26 PM Elliotte Rusty Harold <el...@ibiblio.org>
wrote:
> On Sat, Dec 14, 2024 at 3:08 PM Serw <se...@gmail.com> wrote:
>
> > Would the community support using string concatenation over StringBuilder
> > for simple cases? Modern JDKs (8+) optimize string concatenation
> > efficiently, making it both concise and performant.
>
> Interesting. Do you have a reference for that?
>
> --
> Elliotte Rusty Harold
> elharo@ibiblio.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Elliotte Rusty Harold <el...@ibiblio.org>.
On Sat, Dec 14, 2024 at 3:08 PM Serw <se...@gmail.com> wrote:
> Would the community support using string concatenation over StringBuilder
> for simple cases? Modern JDKs (8+) optimize string concatenation
> efficiently, making it both concise and performant.
Interesting. Do you have a reference for that?
--
Elliotte Rusty Harold
elharo@ibiblio.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: Subject: Proposal: Standardizing String Concatenation in Apache Commons
Posted by Eric Bresie <eb...@gmail.com>.
What performance tests are define to help in determining before and after
performance improvements?
Eric Bresie
ebresie@gmail.com
On Sat, Dec 14, 2024 at 9:08 AM Serw <se...@gmail.com> wrote:
> Dear Apache Commons Community,
>
> In my recent PR #1332, I replaced a StringBuilder usage with string
> concatenation in a toString method for improved readability. During the
> review, it was suggested this change should align with a broader project
> standard.
>
> Would the community support using string concatenation over StringBuilder
> for simple cases? Modern JDKs (8+) optimize string concatenation
> efficiently, making it both concise and performant.
>
> If agreed, I can implement consistent changes across the codebase.
>
> Looking forward to your feedback!
>
> PR - https://github.com/apache/commons-lang/pull/1332
>