Skip to content
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

Merged
merged 2 commits into from
Aug 22, 2018

Conversation

chris05atm
Copy link
Contributor

Using a List here results in extremely long runtimes when the source
folder is large because the contains() call is O(n). The logic
ends up placing this List into a Set anyway so just use a Set here
and make contains() checks cheap.

Using a List here results in extremely long runtimes when the source
folder is large because the contains() call is O(n). The logic
ends up placing this List into a Set anyway so just use a Set here
and make contains() checks cheap.
return result;

List<SpoonFile> resultAsList = new ArrayList<>(result);
return resultAsList;
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend to avoid unnecessary local variable:

List<SpoonFile> resultAsList = new ArrayList<>(result);
return resultAsList;

Just return what is needed:

return new ArrayList<>(result);

It is shorter and easier to understand.

@@ -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()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

f -> file

@@ -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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test seems not needed anymore

return result;

List<SpoonFile> resultAsList = new ArrayList<>(result);
return resultAsList;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

@surli
Copy link
Collaborator

surli commented Aug 9, 2018

Thanks @aggie05engineer for your proposal!

Folders are never added to VirtualFolder's file field (just files)
so we can just return the file contents as a List. files is a Set
so deduplication has already been done.
@pvojtechovsky
Copy link
Collaborator

I suggest to return immutable list. WDYT?

@monperrus monperrus merged commit a576068 into INRIA:master Aug 22, 2018
@monperrus
Copy link
Collaborator

Thanks a lot for your contribution

@monperrus monperrus mentioned this pull request Sep 20, 2018
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants