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 — [apex] #3569 - Requested changes for code review feedback by SCWells72 · Pull Request #3581 · pmd/pmd

Conversation

@SCWells72

@SCWells72 SCWells72 commented Oct 23, 2021

Copy link
Copy Markdown
Contributor

Addressed the final two code review feedback items requested by @oowekyala from PR #3574 for issue #3569. That PR was already merged, so creating a new PR for these.

Describe the PR

Made the check for whether a node contains comments more efficient. Instead of having to inspect all commented lines in the file for each comment container, we now pre-calculate an index of which lines were comments, and then we see whether any of the lines in a comment container's range are flagged in that index. Given that we're currently only going to perform this logic for ASTCatchBlockStatement nodes, hopefully this should be very efficient. If/when this logic is expanded to include other node types, we can revisit and determine whether another approach such as a binary search for lines in a given range is a better fit.

Made AbstractApexCommentContainerNode package-private.

Related issues

  • Fixes [apex] EmptyCatchBlock rule should provide an option to ignore empty catch blocks in test methods #3569 [apex] Fix for #3569 - Added new configuration properties allowCommentedBlocks and allowExceptionNameRegex to EmptyCatchBlock #3574

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

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

All reactions
@SCWells72
Made the check for whether a node contains comments more efficient. I…
e0cd2a6
…nstead of having to inspect all commented lines in the file for each comment container, we now pre-calculate an index of which lines were comments, and then we see whether any of the lines in a comment container's range are flagged in that index. Given that we're currently only going to perform this logic for ASTCatchBlockStatement nodes, hopefully this should be very efficient. If/when this logic is expanded to include other node types, we can revisit and determine whether another approach such as a binary search for lines in a given range is a better fit.

Made AbstractApexCommentContainerNode package-private.
@SCWells72

SCWells72 commented Oct 23, 2021

Copy link
Copy Markdown
Contributor Author

@oowekyala, sorry these weren't included in the merge of #3574. It looks like that was pulled in while I was away this morning/early afternoon. Here are the last two requested changes, though.

@rsoesemann if these are approved, we'll want to merge them in as well to make my changes for #3569 meet all review feedback.

All reactions

Sorry, something went wrong.

Uh oh!

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

@rsoesemann
rsoesemann merged commit 591d6c6 into pmd:master Oct 23, 2021
@SCWells72
SCWells72 deleted the pmd-3569-feedback branch October 23, 2021 19:10
@ghost

ghost commented Oct 23, 2021

Copy link
Copy Markdown
1 Message
📖 No java rules are changed!

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

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

@oowekyala oowekyala added this to the 6.40.0 milestone Oct 24, 2021
@oowekyala

oowekyala commented Oct 24, 2021

Copy link
Copy Markdown
Member

Pasting your earlier comment here to answer it

I've just pushed another commit that includes an optimization for this check. It should also address the next comment about the selected data structure. A LinkedList should be more efficient for initial population as it avoids any need for resizing the underlying storage, and then it's converted into an ArrayList to guarantee the sort order (which is important because the highest line number must be the last element), and finally used to construct an array of flags denoting which lines in the file (up to the last reported comment) were for comments. At that point access is directly indexed, and the number of entries we ever have to examine is bounded by the number of lines in the catch block itself.

I think the refactoring you made is much less efficient than the solution I was proposing (it's also more complicated).

A LinkedList should be more efficient for initial population as it avoids any need for resizing the underlying storage, and then it's converted into an ArrayList to guarantee the sort order (which is important because the highest line number must be the last element)

Appending to an ArrayList is amortized O(1). The amortized complexity factors in the cost of resizing the internal buffer. It's way more efficient to append to an ArrayList, even if it might internally grow the array several times, than to append to a LinkedList and then copy everything into an ArrayList anyway. The only instance where LinkedList could be preferred to ArrayList is if you're doing a lot of insertion/removal in the middle of the list, which is not the case here. See https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java/322742#322742

You wouldn't need to convert into an ArrayList to do the sorting if it was an ArrayList from the start. Also, we don't need the sorting at all, as is evident from how the comment list is populated.

Other things I noticed

  • Your routine containsCommentedLine is O(size of the node), as it examines all lines of the parameter node.
  • It uses an array that has the size of the file, even though we can safely assume that there are much fewer comments than lines in the file
  • You do several O(N) operations up front: creating the ArrayList for sorting, a useless Arrays.fill(false), and iterating over comment tokens (which btw you don't need to sort at all to do this). We do these operations even if the file contains no ASTCatchBlockStatement

Given that we're currently only going to perform this logic for ASTCatchBlockStatement nodes, hopefully this should be very efficient.

I hope I've shown that it isn't. Compare to the solution I was proposing:

We already have a sorted list of comments. Implementing containsCommentedLine can use binary search, which is O(log(number of comments in the file)). Once we find a comment that is after the first line of the node, we just have to check that it's also before the end line of the node. We just have to check once, because we don't need to find all comments in the node, just whether one exists, and since comments are sorted, if the first comment is after the end line, then all the following are too. There is no linear operation anywhere here. Notice that this solution doesn't get slower for larger nodes, and whether we're only doing this for ASTCatchBlockStatement or also for other nodes in the future doesn't matter.

All reactions

Sorry, something went wrong.

Uh oh!

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

oowekyala added a commit to oowekyala/pmd that referenced this pull request Oct 24, 2021
@oowekyala
Cleanup after pmd#3581
7a9d0db
@oowekyala oowekyala mentioned this pull request Oct 24, 2021
[apex] Cleanup after #3581 #3582
Merged
1 of 4 tasks
@SCWells72 SCWells72 mentioned this pull request Oct 24, 2021
[apex] #3569 - Check for commented lines now uses binary search against indexed list #3583
Closed
4 tasks done
@SCWells72

SCWells72 commented Oct 24, 2021

Copy link
Copy Markdown
Contributor Author

@oowekyala please see PR #3583.

All reactions

Sorry, something went wrong.

Uh oh!

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

adangel added a commit that referenced this pull request Oct 25, 2021
@adangel
[doc] Update release notes (#3574, #3580, #3581)
f4d34f9
adangel added a commit to adangel/pmd that referenced this pull request Oct 29, 2021
@adangel
Merge pull request pmd#3582 from oowekyala:cleanup-3581
f5fd79d
[apex] Cleanup after pmd#3581 pmd#3582

* pr-3582:
  [apex] comments: use index instead of line/column to map to node
  [apex] comments: consider position index in the same line
  [apex] EmptyCatchBlock: Improve test case
  Added a new test case verifying the expected boundary condition behavior for allowing commented blocks.
  Cleanup after pmd#3581
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

6.40.0

Development

Successfully merging this pull request may close these issues.

[apex] EmptyCatchBlock rule should provide an option to ignore empty catch blocks in test methods

Uh oh!

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

3 participants

@SCWells72 @oowekyala @rsoesemann
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.