Skip to content

Conversation

@Obolrom
Copy link
Contributor

@Obolrom Obolrom commented May 5, 2024

Fixes #3113

  • Inject VersionInformation to IterableCreation class
  • Implement code generating using new factory methods for HashSet, HashMap, LinkedHashSet, LinkedHashSet
  • Separate tests for JDK 19+

@Obolrom
Copy link
Contributor Author

Obolrom commented May 7, 2024

Basic implementation of the feature has been done. However, now 30 tests fail with errors similar to
Screenshot from 2024-05-07 22-30-49

@filiphr Could you please conduct a review and help me to understand how to fix those tests?
It seems we should add tests for testing the generated code for JDK 19+

Copy link
Member

@filiphr filiphr left a comment

Choose a reason for hiding this comment

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

I've added some comments. We need a bit of cleanup before we get this on main.

Also for the failing tests, seems like we'll need to adjust our testing fixtures to be able to pick up different fails based on the java version we are testing with.

@Obolrom Obolrom changed the title #3113 Add java HashSet, HashMap, LinkedHashSet, LinkedHashSet new factory methods for java >= 19 #3113 Add java LinkedHashSet, LinkedHashSet new factory methods for java >= 19 May 18, 2024
@Obolrom Obolrom requested a review from filiphr May 19, 2024 14:39
@filiphr
Copy link
Member

filiphr commented Jun 1, 2024

Thanks a lot @Obolrom. Looks way way cleaner now. I'll have a look for the fixtures to work on multiple java version

@Obolrom Obolrom requested a review from filiphr June 1, 2024 15:07
@jccampanero
Copy link
Contributor

jccampanero commented Jun 10, 2024

@filiphr @Obolrom Sorry for the late reply.

I have been working on the review too.

My main concern about the provided code is that, in my opinion, it actually doesn't deal with the actual problem. Please, let me trying explaining myself.

The problem the new factory methods trying addressing is the following.

Let's say you need to allocate N elements.

When you create an ArrayList, for instance, you do something like the following:

List<String> list = new ArrayList<>(N);

The JVM will ensure that enough room for the N elements will be allocated.

In contrast, if you do the same with a HashMap, for example:

Map<String,Object> map = new HashMap<>(N);

and you actually need N elements to be allocated, you are in trouble, because in fact the JVM will allocate N*<load factor>, by default, 0.75, elements instead: i.e., if you push N elements actually to that HashMap it will be internally rehashed.

Addressing this issue is the reason behind the new factory methods. Please, see the actual code implementation.

In other words, I think the important point here is the actual initial capacity and not the load factor, as the implementation proposed suggests, as the implementation is focused on.

What do you think?

@filiphr
Copy link
Member

filiphr commented Jun 10, 2024

@jccampanero, sorry but I don't think that I understand your comment. The fact that Map<String,Object> map = new HashMap<>(N); does not load the right amount of data is something that we already know and that we handle in MapStruct.

The current implementation looks like:

Map<String,Object> map = new HashMap<>( Math.max( (int) ( N / .75f ) + 1, 16 );

The goal of this PR is to generate code (on Java 19 or later) that looks like:

Map<String,Object> map = HashMap.newHashMap( N );

With this PR, based on the java version of the code we would either use the load factor or the new factory method.

@jccampanero
Copy link
Contributor

Hi @filiphr, thank you very much for your feedback.

I absolutely agree with you. I was merely trying to provide some context, but I understand the purpose of the PR. I apologize if I didn't explain myself clearly earlier.

First, I want to say that the provided implementation is quite good. Thank you, @Obolrom.

However, I believe it is very focused on the load factor when we should be concentrating more on the factory method itself. I have performed a code review which I hope clarifies my point.

Thank you very much for your patience, @filiphr, and great job, @Obolrom.

Copy link
Contributor

@jccampanero jccampanero left a comment

Choose a reason for hiding this comment

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

Great job @Obolrom.

@Obolrom
Copy link
Contributor Author

Obolrom commented Jun 12, 2024

I'll have a look for the fixtures to work on multiple java version

@filiphr any updates about supporting fixtures for multiple java versions?

@filiphr
Copy link
Member

filiphr commented Jun 18, 2024

@jccampanero I understood your comments now. @Obolrom I did some polishing from the feedback provided by @jccampanero. Let me know if you are OK with this.

I also gave it a go to have java 21 based fixtures. I'm not super happy with the solution and it doesn't work well with the current version of the eclipse plugin for some reason.

@Obolrom
Copy link
Contributor Author

Obolrom commented Jun 19, 2024

@filiphr code looks a little bit cleaner now.
Regarding the Eclipse plugin. I think the problem is with the hardcoded compiler max source version.
Please check EclipseCompilingExtension.getSourceVersion() method

@Obolrom Obolrom requested a review from jccampanero June 19, 2024 06:18
@Obolrom
Copy link
Contributor Author

Obolrom commented Jun 19, 2024

@filiphr, I did the fix for the Eclipse plugin. Please, share your thoughts how we can improve this solution.
@jccampanero, what do you think?

Copy link
Contributor

@jccampanero jccampanero left a comment

Choose a reason for hiding this comment

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

@Obolrom, I think it looks great now.

Regarding the tests, I would prefer if @filiphr provides his opinion.

Great job, @Obolrom!

@Obolrom
Copy link
Contributor Author

Obolrom commented Jul 6, 2024

@filiphr could you please conduct a code review? It seems we are close to merge this PR 😊

@Obolrom Obolrom requested a review from jccampanero July 18, 2024 08:51
@Obolrom Obolrom force-pushed the 3113_hashmap_hashset_factory_method branch from 52db9da to deb1b91 Compare July 23, 2024 06:26
@Obolrom
Copy link
Contributor Author

Obolrom commented Jul 23, 2024

@filiphr, I have rebased the branch and fixed a test
@thunderhook, could you please take a look at the PR?

@thunderhook
Copy link
Contributor

@thunderhook, could you please take a look at the PR?

I'm sorry, I'm on holiday at the moment. Maybe I can make it in about 2 weeks if Filip hasn't had a look yet.

@Obolrom
Copy link
Contributor Author

Obolrom commented Aug 8, 2024

@filiphr , it seems the PR in status 'changes requested'. However, those changes are outdated.
Or have I missed something?

Obolrom

This comment was marked as off-topic.

@filiphr
Copy link
Member

filiphr commented Aug 11, 2024

@filiphr , it seems the PR in status 'changes requested'. However, those changes are outdated.
Or have I missed something?

It is like that, because it still looks at my old review. I'll try to find some time to review this. Sorry that it is taking longer @Obolrom

@Obolrom Obolrom force-pushed the 3113_hashmap_hashset_factory_method branch from c7d00b4 to bf29109 Compare August 30, 2024 18:10
@Obolrom
Copy link
Contributor Author

Obolrom commented Aug 30, 2024

@filiphr @thunderhook
I have updated the branch, it is up to date now.

@filiphr
Copy link
Member

filiphr commented Sep 1, 2024

Thanks @Obolrom. I'll try to review it it this week during my holidays.

@filiphr filiphr merged commit 4d9894b into mapstruct:main Sep 2, 2024
@filiphr
Copy link
Member

filiphr commented Sep 2, 2024

I finally got the chance to merge this. Thanks @Obolrom. I did some final polishing in e34abec

@Obolrom
Copy link
Contributor Author

Obolrom commented Sep 2, 2024

@filiphr thank you a lot

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.

Use Java HashSet and HashMap new factory method with known capacity when on Java 19

4 participants