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

performance problem caused by Sets.newSetFromMap() #1069

Closed
gissuebot opened this issue Oct 31, 2014 · 9 comments
Closed

performance problem caused by Sets.newSetFromMap() #1069

gissuebot opened this issue Oct 31, 2014 · 9 comments
Assignees

Comments

@gissuebot
Copy link

Original issue created by adi175652q on 2012-07-16 at 06:59 PM


Hi,

I am encountering a performance problem caused by Sets.newSetFromMap().
It appears in the latest Guava version (16 July). I attached a test
that exposes this problem and a one-line patch that fixes it. On my
machine, for this test, the patch provides a 386X speedup.

To run the test, just do:

$ java Test

The output for the un-patched version is:
Time is: 5027

The output for the patched version is:
Time is 13

This problem is similar to the previously fixed Issue 1013. The
problem is that:

Sets.newSetFromMap()

returns a SetFromMap (private class), which defines its
removeAll(Collection<?> c) as:

@Override public boolean removeAll(Collection<?> c) {
  return s.removeAll(c);
}

As per the fix of Issue 1013, this should be:

@Override public boolean removeAll(Collection<?> c) {
  return removeAllImpl(s, c);
}

The fix for Issue 1013 replaces several calls to "removeAll" with
calls to "removeAllImpl"
(e.g., in ForwardingSet.standardRemoveAll(Collection<?> collection)),
but not here.

Is this a bug, or am I misunderstanding the intended behavior? If so,
can you please confirm that the patch is correct?

Thanks,

Adrian

@gissuebot
Copy link
Author

Original comment posted by wasserman.louis on 2012-07-16 at 07:38 PM


Hmmmm. There are arguments to be made for both sides, but I'm not honestly sure it matters one way or the other. Either we can have something completely equivalent to Collections.newSetFromMap in JDK 6, or...we can worry about the performance of a highly rare edge case. Eh.


Labels: Type-Performance, Package-Collect

@gissuebot
Copy link
Author

Original comment posted by wasserman.louis on 2012-07-16 at 07:38 PM


Hmmmm. There are arguments to be made for both sides, but I'm not honestly sure it matters one way or the other. Either we can have something completely equivalent to Collections.newSetFromMap in JDK 6, or...we can worry about the performance of a highly rare edge case. Eh.

@gissuebot
Copy link
Author

Original comment posted by adi175652q on 2012-07-16 at 08:00 PM


Hi Louis,

We are just replacing one method call with another, which does not
affect the code complexity. This trivial change significantly
improves performance. And in fact, the patch makes this code
consistent with the fix for Issue 1013. There is no disadvantage
here, and we can gain performance.

Best,

Adrian

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2012-07-16 at 09:39 PM


Are you using JDK 1.6, and if so, why not use java.util.Collections.newSetFromMap?

@gissuebot
Copy link
Author

Original comment posted by adi175652q on 2012-07-16 at 09:57 PM


Hi Louis,

Yes, I am using JDK 1.6, but it has the same problem. I do not expect
the JDK to change very soon, but I hoped that Guava Libraries is more
agile to adopt fixes. Especially since you already implemented the
fix for Issue 1013, and you are already using it in several locations.
It's just a matter of also using it here.

Best,

Adrian

@gissuebot
Copy link
Author

Original comment posted by adi175652q on 2012-07-18 at 12:47 AM


Hi Louis,

Either we can have something completely equivalent to
Collections.newSetFromMap ...

I missed this part from Comment 1. But they are completely
equivalent:

return s.removeAll(c);

and

return removeAllImpl(s, c);

do exactly the same thing. You are not changing the behavior in any
way.

Best,

Adrian

@gissuebot
Copy link
Author

Original comment posted by wasserman.louis on 2012-07-19 at 10:32 AM


This issue still appears incompatible with Kevin's plan in issue 1001. I think I'll push the change for the moment, and it'll probably live on in the JDK 5 backport, but I think I still support the idea of deprecating this functionality and pointing Java 6 users to the JDK.

@gissuebot
Copy link
Author

Original comment posted by kurt.kluever on 2012-07-24 at 05:32 PM


In Guava for 1.6, we'll just deprecate and delegate to the JDK version. In the backport, we'll probably stick with the existing impl.


Status: Accepted
Owner: kurt.kluever

@kevinb9n
Copy link
Contributor

We're about to deprecate this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants