Skip to content

Conversation

@Adagedo
Copy link

@Adagedo Adagedo commented Jun 4, 2025

Overview

My PR optimizes the performance and readability of the BooleanExecutionCondition class by memoizing the annotationType.SimpleName method.
I did not alter any logic though. I only introduced a lightweight internal optimization .


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

@Adagedo

This comment was marked as outdated.

@sbrannen sbrannen changed the title memoized annotationType.getsimple name to improve performance. Memoize `annotationType.getSimpleName() to improve performance Jun 5, 2025
@sbrannen

This comment was marked as outdated.

@sbrannen sbrannen marked this pull request as draft June 5, 2025 06:59
@sbrannen sbrannen changed the title Memoize `annotationType.getSimpleName() to improve performance Memoize annotationType.getSimpleName() to improve performance Jun 5, 2025
Copy link
Member

@sbrannen sbrannen left a comment

Choose a reason for hiding this comment

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

Congratulations on submitting your first PR for JUnit! 👍

My PR optimizes the performance and readability of the BooleanExecutionCondition class by memoizing the annotationType.SimpleName method.

There is no performance gain with that change since annotationType.getSimpleName() is only invoked once.

I would also argue that it does not really improve the readability of the code.

However, the two subclasses of BooleanExecutionCondition both declare private final String annotationName fields.

Thus, if you change the annotationName field you have introduced to protected and modify AbstractJreCondition and AbstractJreRangeCondition so that they no longer declare or set the annotationName field, I would reconsider accepting this PR.

.orElseGet(this::enabledByDefault);
}


Copy link
Member

Choose a reason for hiding this comment

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

Please do not modify lines of code unrelated to your change, and please do not add or remove blank lines.

Copy link
Author

Choose a reason for hiding this comment

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

alright thank you, my apologies, it was unoticed

String reason = "@%s is not present".formatted(this.annotationName);
return enabled(reason);
}

Copy link
Member

Choose a reason for hiding this comment

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

Please restore this blank line.

Copy link
Author

Choose a reason for hiding this comment

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

Ok I will do just that

Copy link
Contributor

Choose a reason for hiding this comment

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

@sbrannen , it would be great to add that into .editorconfig so IDEs trim the whitespace automatically.

I've filed a PR for that: #4623

@sbrannen sbrannen changed the title Memoize annotationType.getSimpleName() to improve performance Store annotationType.getSimpleName() in a field in BooleanExecutionCondition Jun 5, 2025
@sormuras
Copy link
Member

sormuras commented Jun 5, 2025

Micro "optimization" shifting the work from the method (maybe not called) to the constructor (always called). Let's not do it - keep it as-is.

@sbrannen
Copy link
Member

sbrannen commented Jun 5, 2025

Micro "optimization" shifting the work from the method (maybe not called) to the constructor (always called). Let's not do it - keep it as-is.

@sormuras, I think what I proposed in #4619 (review) is worth doing.

@sormuras
Copy link
Member

sormuras commented Jun 5, 2025

I'd rather convert those fields into variables. Why keep/introduce an extra state for something that's only used locally in a single method?

@sbrannen
Copy link
Member

sbrannen commented Jun 5, 2025

Why keep/introduce an extra state for something that's only used locally in a single method?

If we create a private field in in BooleanExecutionCondition like in the original proposal in this PR, then I'd agree with you.

However, AbstractJreCondition and AbstractJreRangeCondition currently both store the simple name in private fields.

So, if we want to continue to store the simple name in fields, we should just store the simple name once in a protected field in the common superclass.

I'd rather convert those fields into variables.

If we don't want to continue storing the simple name in fields in any of the affected classes in that hierarchy, yes, we could calculate the simple name based on super.annotationType.getSimpleName() in AbstractJreCondition.validatedVersions() and AbstractJreRangeCondition.isCurrentVersionWithinRange() and leave BooleanExecutionCondition.enabledByDefault() as is.

I'm also OK with going with that approach.

@marcphilipp and @sormuras, WDYT?

@marcphilipp
Copy link
Member

I'd rather convert those fields into variables.

If we don't want to continue storing the simple name in fields in any of the affected classes in that hierarchy, yes, we could calculate the simple name based on super.annotationType.getSimpleName() in AbstractJreCondition.validatedVersions() and AbstractJreRangeCondition.isCurrentVersionWithinRange() and leave BooleanExecutionCondition.enabledByDefault() as is.

I'm also OK with going with that approach.

I'd prefer that approach. 👍

@Adagedo Adagedo marked this pull request as ready for review June 5, 2025 10:36
@marcphilipp
Copy link
Member

Team decision: @sbrannen himself will change the code according to his proposal in #4619 (comment).

@marcphilipp marcphilipp closed this Jun 6, 2025
sbrannen added a commit that referenced this pull request Jun 6, 2025
This commit removes the annotationName fields from AbstractJreCondition
and AbstractJreRangeCondition and replaces them with local variables.

See #4619
@sbrannen
Copy link
Member

sbrannen commented Jun 6, 2025

@Adagedo, thanks again for the PR, even though we decided to address this differently.

Team decision: @sbrannen himself will change the code according to his proposal in #4619 (comment).

Done in 483fcc9.

@marcphilipp
Copy link
Member

@Adagedo, thanks again for the PR, even though we decided to address this differently.

+1 👍

@Adagedo
Copy link
Author

Adagedo commented Jun 6, 2025

@sbrannen My pleasure

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants