Skip to content

Improve builder value fill#20

Open
hduelme wants to merge 2 commits intomapstruct:mainfrom
hduelme:improve-builder-value-fill
Open

Improve builder value fill#20
hduelme wants to merge 2 commits intomapstruct:mainfrom
hduelme:improve-builder-value-fill

Conversation

@hduelme
Copy link
Contributor

@hduelme hduelme commented Feb 13, 2026

I changed the generated code to use java switch string matching, if there are more than 3 strings to check. For this I extracted the builder fill code into fillBuilder macro.
I although changed the loop iteration to use entrySet.

@hduelme
Copy link
Contributor Author

hduelme commented Feb 13, 2026

I tested the this improvement with mapstruct and found a small improvement (about 100ms for mapstruct-processor in MapperGem), manly due to the reduction in String comparison.

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.

This reads way better @hduelme. Thanks a lot for the improvements. I've left some comments.

Apart of the comments, I see that we have a lot of values.get( methodName ) and defaultMethod.getValue(). Won't it be a bit better if we assign them at the beginning of the if / switch?

e.g.

String methodName = defaultMethod.getKey();
switch ( methodName ) {
    case "myClassWithDefault":
        builder.setMyclasswithdefault( GemValue.create( values.get( methodName ), defaultMethod.getValue(), TypeMirror.class ) );
        break;

Would become

String methodName = defaultMethod.getKey();
AnnotationValue defaultValue = defaultMethod.getValue();
AnnotationValue value = values.get( methodName );
switch ( methodName ) {
    case "myClassWithDefault":
        builder.setMyclasswithdefault( GemValue.create( value, defaultValue, TypeMirror.class ) );
        break;

</#if>
for ( Map.Entry<String, AnnotationValue> defaultMethod : defaultValues.entrySet() ) {
String methodName = defaultMethod.getKey();
<#macro fillBuilder gemValueInfo indnet>
Copy link
Member

Choose a reason for hiding this comment

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

I think that it'll be better if we move the macro in the bottom. I personally find it a bit weird to have it in the middle.

Also, I think that indnet should be indent, right?

<#if (gemInfo.gemValueInfos?size < 4)>
<#list gemInfo.gemValueInfos as gemValueInfo>
<#if gemValueInfo_index != 0>else </#if>if ( "${gemValueInfo.name}".equals( methodName ) ) {
<#assign indentString = " ">
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we assign this just before we start the <#list?

switch ( methodName ) {
<#list gemInfo.gemValueInfos as gemValueInfo>
case "${gemValueInfo.name}":
<#assign indentString = " ">
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we assign this just before we start the <#list?

@filiphr
Copy link
Member

filiphr commented Feb 14, 2026

I tested the this improvement with mapstruct and found a small improvement (about 100ms for mapstruct-processor in MapperGem), manly due to the reduction in String comparison.

That's great

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.

2 participants