-
Notifications
You must be signed in to change notification settings - Fork 170
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
MapEnvelope shouldn't waste the hashCode object #1085
Comments
@llorllale/z please, pay attention to this issue |
@Shryne/z this project will fix the problem faster if you donate a few dollars to it; just click here and pay via Stripe, it's very fast, convenient and appreciated; thanks a lot! |
I've read #947 but I don't think it adds something to this issue. Yes, the new implementation with the scalar fixes that, but now it breaks this rule: Make sure you instantiate everything or almost everything in your secondary constructors Additionally, it requires the construction of an object on each call. This is unnecessary and it'll probably go against the rule from PMD (and therefore qulice) if these methods are called inside of a loop: "Avoid instantiating new objects inside loops" Conclusion: I think there are multiple problems:
|
Sounds like maybe you just don't agree with the style. |
@Shryne ping Should we close this? |
Sry, I forgot about it.
public static void main(final String[] args) {
// cactoos
final var first = new MapOf<>(
new MapEntry<>(1, 2),
new MapEntry<>(3, 4),
new MapEntry<>(5, 6)
);
final var second = new MapOf<>(
new MapEntry<>(6, 7),
new MapEntry<>(8, 9)
);
var number = 1_000_000;
var start = System.currentTimeMillis();
for (int round = 0; round < number; ++round) {
first.equals(second);
}
System.out.println(System.currentTimeMillis() - start);
// standard java
final var third = new HashMap<Integer, Integer>(3);
third.put(1, 2);
third.put(3, 4);
third.put(5, 6);
final var fourth = new HashMap<Integer, Integer>(2);
fourth.put(6, 7);
fourth.put(8, 9);
number = 1_000_000;
start = System.currentTimeMillis();
for (int round = 0; round < number; ++round) {
third.equals(fourth);
}
System.out.println(System.currentTimeMillis() - start);
}
About PMD: I didn't mean you're breaking PMD rules inside your project. I meant that if I use the Map from cactoos and call
I guess I don't have more to say, so if you aren't convinced, I guess this can be closed. |
@Shryne I want to focus on your point about performance. What sort of application are you building that is being negatively impacted by cactoos' performance? |
I am working on a GUI library and I've already noticed that I need to reduce the number of throwaway objects. |
@Shryne what are the constraints on your resources (platform, RAM, cpu, etc) |
Currently, MapEnvelope (and probably some other classes, for example, CollectionEnvelope) creates a scalar inside of hashCode and calls
value()
immediately:cactoos/src/main/java/org/cactoos/map/MapEnvelope.java
Lines 161 to 179 in 7ea22b9
I guess you are desperately trying to hold onto the oop rules, but creating an object inside of a method is also against the rules. You are just misusing the object as a (less readable, less efficient and longer) function. Same applies to
equals
and partly totoString
.The simplest solution would be to hold the hashCode scalar in an instance variable.
Alternatively, you could also use the traditional, procedural (but shorter) approach inside of hashCode (and equals).
The text was updated successfully, but these errors were encountered: