-
-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert from List to Set to speed up contains() logic. #2354
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,15 +45,17 @@ public void addFolder(SpoonFolder o) { | |
|
||
@Override | ||
public List<SpoonFile> getAllFiles() { | ||
List<SpoonFile> result = new ArrayList<>(); | ||
Set<SpoonFile> result = new HashSet<>(); | ||
|
||
for (SpoonFile f : getFiles()) { | ||
// we take care not to add a file that was already found in a folder | ||
if (!result.contains(f)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test seems not needed anymore |
||
result.add(f); | ||
} | ||
} | ||
return result; | ||
|
||
List<SpoonFile> resultAsList = new ArrayList<>(result); | ||
return resultAsList; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend to avoid unnecessary local variable:
Just return what is needed:
It is shorter and easier to understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, in order to improve the perf, it looks interesting to store the results locally in the instance to avoid computing it again each time. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand the code well enough unfortunately. Yesterday was my first day playing with spoon. files appears to be mutable given addFile() and addFolder() methods. There is no need to call getFiles() here at all since getFiles() just returns a List representation of the Set files anyway. If there are thread concerns then there are already issues with non-protected blocks so I think just using the files as is works ok? Submitting new change today. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah actually I had second thought about my comment after posting it. So let's forgot this idea for now. |
||
} | ||
|
||
@Override | ||
|
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.
f -> file