GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Added 4 performance rules originating from PMD-jPinpoint-rules by jborgers · Pull Request #1932 · pmd/pmd

Conversation

@jborgers

@jborgers jborgers commented Jul 22, 2019

Copy link
Copy Markdown
Contributor

Happy to add the following Java performance rules:

  • AvoidApacheCommonsFileItemNonStreaming
  • AvoidCalendarDateCreation
  • AvoidConcatInAppend and
  • AvoidConcatInLoop.

Including unit tests and examples. Originating from github.com/jborgers/PMD-jPinpoint-rules.

  • Tests and build run without problems.

PR Description:
This is the first set of rules from PMD-jPinpoint-rules, sponsored by Rabobank. I discussed the rules and PR with Juan Martín Sotuyo Dodero.

  • There is overlap with other rules for the concatenation rules. I am not sure how much overlap.
  • I am not sure about the severity of the rules, I left them at the level we use them.
  • We want to provide more extensive documentation in the future, and link to it from the rules doc.

Regards, Jeroen

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jborgers
Added 4 performance rules: AvoidApacheCommonsFileItemNonStreaming, Av…
b37355d
…oidCalendarDateCreation, AvoidConcatInAppend and AvoidConcatInLoop.

Including unit tests and examples. Originating from github.com/jborgers/PMD-jPinpoint-rules.
Links to more doc later.
@ghost

ghost commented Jul 22, 2019 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
1 Message
📖 This changeset introduces 168 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report
✅ This changeset introduces 168 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report
✅ This changeset introduces 154 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

oowekyala
oowekyala reviewed Jul 22, 2019
View reviewed changes

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks for the PR! I think some things can be simplified/ are debatable

Also, please note for the future:

  • the documentation is generated by the CI build - you don't need to generate it manually. Though now it's there, it can stay
  • it would be nice to make a separate PR for each rule next time

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/performance.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/AvoidConcatInLoop.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-java/src/main/resources/category/java/performance.xml
</example>
</rule>

<rule name="AvoidConcatInAppend"

@oowekyala oowekyala Jul 22, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Isn't that exactly what InefficientStringBuffering is meant to be?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jborgers jborgers Jul 25, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Possibly. We don't have that rule in the subset we use, probably because we didn't update that selection for some time. There is certainly overlap, not sure if they are exactly the same.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jborgers jborgers Jul 25, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

On a separate PR for each rule: Juan asked me to create a PR per batch, a couple of rules together. Not sure of best choice here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@oowekyala oowekyala Jul 25, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

They appear to have the same intention, but there are some differences in their implementation:

For example

  • your rule flags wrappedLine.append(str + "c", a, c); correctly whereas the other rule gives up
  • your rule flag sb.append(1 + 2), which is not a concatenation, so that's a false positive
  • your rule considers concatenation with a non-static final field as a constant expression, which it isn't
  • the other rule doesn't flag any cases of new StringBuilder().append(...), ie use of append directly on the constructor invocation, whereas yours does it correctly

I think we should fix the false negatives of the existing rule but not merge this new rule.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@oowekyala oowekyala Jul 25, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Actually this rule has many more problems... It uses too many // tests, so that you end up testing unrelated nodes. Consider this violation where the rule sees an append, a string concatenation somewhere in another statement (the throw statement) and reports on a common ancestor (the for statement), even though the concatenation and the append are unrelated. All the violations I've seen on the spring framework in the report are false positives either of this kind, or where there's no concatenation but just numeric addition.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@adangel adangel Jul 27, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I agree, we should enhance the existing rule InefficientStringBuffering instead of adding this new rule.

FWIW: I've noticed, that the found violation don't highlight the append - at least for the 3 violations found in checkstyle: I wouldn't know, what to fix, if I get these violations. We should aim to report exactly the node which contains the concatenation. Maybe the reason for this is the //, that Clément mentioned?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/performance.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

jborgers added 2 commits July 23, 2019 11:14
@jborgers
Refactored AvoidConcatInLoop rule to use xpath 2.0 and utilize features,
feb5822
ss suggested by oowekyala.
@jborgers
Added two tests for AvoidConcatInLoop as suggested by oowekyala: do-l…
0487e14
…oop and String field.
oowekyala
oowekyala requested changes Jul 26, 2019
View reviewed changes

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks again for your work on this. I've taken a look at the CI report though, and there are many false positives. Please take a look at the following comments

About using one PR per rule, I think this would have made it easier to review rules separately, and to merge incrementally. While the rule about FileItems and AvoidConcatInLoops are not far from ready, AvoidConcatInAppend should IMO not be merged, and changes to CalendarDateCreation may delay the other rules. So I think separate PRs would have been a better choice

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/performance.xml
class="net.sourceforge.pmd.lang.rule.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidcalendardatecreation">
<description>Problem: A Calendar is a heavyweight object and expensive to create. &#13;

@oowekyala oowekyala Jul 25, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The assumption of the rule doesn't hold if the calendar is not created just to call this method, or if it is eg passed as a parameter and not created locally. Consider for example this violation in the Spring Framework, which looks like a false positive.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/performance.xml
</example>
</rule>

<rule name="AvoidConcatInAppend"

@oowekyala oowekyala Jul 25, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Actually this rule has many more problems... It uses too many // tests, so that you end up testing unrelated nodes. Consider this violation where the rule sees an append, a string concatenation somewhere in another statement (the throw statement) and reports on a common ancestor (the for statement), even though the concatenation and the append are unrelated. All the violations I've seen on the spring framework in the report are false positives either of this kind, or where there's no concatenation but just numeric addition.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/performance.xml
<property name="version" value="2.0"/>
<property name="xpath">
<value><![CDATA[
(//ForStatement | //WhileStatement | //DoStatement)//AssignmentOperator[

@oowekyala oowekyala Jul 25, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The rule should be checking that the variable being concatenated is defined outside the loop.
Consider this violation:

      for (String fileExtension : fileExtensions) {
				if (fileExtension.charAt(0) != '.') {
					fileExtension = "." + fileExtension; // here
				}
				this.fileExtensions.add(fileExtension);
			}

The concatenation is local to each iteration and replacing it with a StringBuilder would only be noise.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/performance.xml
</example>
</rule>

<rule name="AvoidApacheCommonsFileItemNonStreaming"

@oowekyala oowekyala Jul 26, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Maybe a better name would be UseIOStreamsWithApacheCommonsFileItem? IMHO "streaming" immediately hints at the Stream API and not IO streams, so I think it would be clearer

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread pmd-java/src/main/resources/category/java/performance.xml
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidapachecommonsfileitemnonstreaming">
<description>
Problem: Use of FileItem.get and FileItem.getString could exhaust memory since they load the entire file into memory&#13;
Solution: Use streaming methods and buffering.

@oowekyala oowekyala Jul 26, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Could you mention getInputStream() explicitly in the description? Like for the name of the rule, the "streaming methods" here sounds unclear to me

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
adangel
adangel reviewed Jul 27, 2019
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/performance.xml
class="net.sourceforge.pmd.lang.rule.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidconcatinappend">
<description>Concatenation of Strings is used inside an StringBuilder.append argument. Problem: Each statement with one or more +-operators creates a hidden temporary StringBuilder, a char[] and a new String object, which all have to be garbage collected.&#13;

@adangel adangel Jul 27, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

FYI: Just use markdown syntax for the description. No need to use &#13; for a new line, just add a empty line. We'll parse the text here (including the line breaks) and use it in our markdown-based rule documentation.
In order to easily read this on github, also consider breaking the lines after 120 characters.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
adangel
adangel reviewed Jul 27, 2019
View reviewed changes
Comment thread ...et/sourceforge/pmd/lang/java/rule/performance/xml/AvoidApacheCommonsFileItemNonStreaming.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel reviewed Jul 27, 2019
View reviewed changes
Comment thread ...rc/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/AvoidConcatInAppend.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Violation: Avoid contact in append method invocations</description>

@adangel adangel Jul 27, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

typo: contact -> concat

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@adangel adangel added a:new-rule Proposal to add a new built-in rule is:WIP For PRs that are not fully ready, or issues that are actively being tackled labels Jul 27, 2019
@oowekyala oowekyala mentioned this pull request Aug 15, 2019
[java] Make binary operators left-recursive #1979
Merged
php-coder
php-coder reviewed Oct 4, 2019
View reviewed changes
Comment thread docs/pages/pmd/rules/java/performance.md

private String good(String arg) {
StringBuilder sb = new StringBuilder();
sb.append("arg='").append(arg).append("'");

@php-coder php-coder Oct 4, 2019

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The better one:

StringBuilder sb = new StringBuilder("arg='").append(arg).append("'");

Perhaps, it's better to use best practices or at least mention that this example isn't the best.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@adangel adangel Oct 4, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Agreed, we should change the sample code. Note: performance.md is generated, the actual change has to happen in performance.xml.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@php-coder php-coder Oct 4, 2019

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Note: performance.md is generated, the actual change has to happen in performance.xml.

Is it something that can be automated with Danger? :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
php-coder
php-coder reviewed Oct 4, 2019
View reviewed changes
Comment thread docs/pages/pmd/rules/java/performance.md
String log = "";
List<String> values = Arrays.asList("tic ", "tac ", "toe ");
for (String val : values) {
log += val;

@php-coder php-coder Oct 4, 2019

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is string concatenation is still an issue nowadays (2019) ? I've heard that there were optimizations for that in JDK9, for example.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@adangel adangel Oct 4, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

There is indeed some optimization done with Java 9:

  • http://openjdk.java.net/jeps/280
  • https://stackoverflow.com/questions/46512888/how-is-string-concatenation-implemented-in-java-9

Before Java9, inside the loop, the code generated was basically:

StringBuilder temp = new StringBuilder();
log = temp.append(log).append(val).toString();

Since Java9, it looks more like (roughly):

log = StringConcatFactory.makeConcatWithConstants(..., log, val);

So, the rule is definitely less relevant for java9+.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jsotuyod jsotuyod May 12, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

it's worth noting, that this specific example would be unaffected by Java 9+ optimizations, as the concat is done within a loop

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
adangel added a commit to adangel/pmd that referenced this pull request Jun 13, 2020
@adangel
[doc] Update release notes, refs pmd#1932
b8e1ec9
@adangel adangel mentioned this pull request Jun 13, 2020
[java] New Performance Rule UseIOStreamsWithApacheCommonsFileItem #2574
Merged
4 tasks done
adangel added a commit to adangel/pmd that referenced this pull request Jun 13, 2020
@adangel
[doc] Update release notes, refs pmd#1932
1a71796
@adangel adangel mentioned this pull request Jun 13, 2020
[java] New Performance Rule AvoidConcatInLoop #2575
Closed
6 tasks
adangel added a commit to adangel/pmd that referenced this pull request Jun 13, 2020
@adangel
[doc] Update release notes, refs pmd#1932
488f388
@adangel adangel mentioned this pull request Jun 13, 2020
[java] New Performance Rule AvoidCalendarDateCreation #2576
Merged
5 tasks done
adangel added a commit to adangel/pmd that referenced this pull request Jun 14, 2020
@adangel
[java] Add test cases for InefficientStringBuffering
67cca2a
Originally from pmd#1932

Author: jborgers <[email protected]>
@adangel adangel mentioned this pull request Jun 14, 2020
[java] InefficientStringBuffering/AppendCharacterWithChar: Fix false negatives with concats in appends #2591
Merged
8 tasks done
@adangel

adangel commented Jun 14, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This PR is replaced by the following new PRs:

  • [java] InefficientStringBuffering/AppendCharacterWithChar: Fix false negatives with concats in appends #2591
  • [java] New Performance Rule AvoidCalendarDateCreation #2576
  • [java] New Performance Rule AvoidConcatInLoop #2575
  • [java] New Performance Rule UseIOStreamsWithApacheCommonsFileItem #2574
  • [java] UseStringBufferForStringAppends: fix false negative with fields #2600
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel closed this Jun 14, 2020
adangel added a commit to adangel/pmd that referenced this pull request Jun 18, 2020
@adangel
[java] UseStringBufferForStringAppends: fix false negative with fields
dda76be
And also fix false positive when concatenation is local to each
for-loop iteration.

Test cases originally from pmd#1932

Author: jborgers <[email protected]>
@adangel adangel mentioned this pull request Jun 18, 2020
[java] UseStringBufferForStringAppends: fix false negative with fields #2600
Merged
4 tasks done
adangel added a commit to adangel/pmd that referenced this pull request Jun 18, 2020
@adangel
[doc] Update release notes, refs pmd#2600, refs pmd#1932
18c3796
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jsotuyod jsotuyod jsotuyod left review comments
@adangel adangel adangel left review comments
@oowekyala oowekyala oowekyala requested changes
+1 more reviewer
@php-coder php-coder php-coder left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

a:new-rule Proposal to add a new built-in rule is:WIP For PRs that are not fully ready, or issues that are actively being tackled

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@jborgers @adangel @php-coder @jsotuyod @oowekyala
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.