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

[Core] Optimize MethodScanner #1238

Merged
merged 1 commit into from
Oct 11, 2017
Merged

[Core] Optimize MethodScanner #1238

merged 1 commit into from
Oct 11, 2017

Conversation

lsuski
Copy link
Contributor

@lsuski lsuski commented Oct 4, 2017

Summary

Optimized MethodScanner.scan method to not unnecessarily check for all cucumber annotations

Details

MethodScanner scans each method for existence of cucumber annotation (there are many). It is redundant and slow because only methods with Before/After or StepDefAnnotation are added to backend.

Motivation and Context

On Android < 7.0 it is extremely slow, and starting with debugger is almost impossible. I've measured it and optimized method is about 150 times faster on Android <7.0 and about 10 times faster on Android >=7.0

How Has This Been Tested?

MethodScannerTest passes.
I've also tested it on my Android project and it works (and it is much more faster)

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) to 56.179% when pulling 9e3e0d1 on lsuski:master into cb30634 on cucumber:master.

@aslakhellesoy aslakhellesoy self-requested a review October 4, 2017 14:48
javaBackend.addStepDefinition(annotation, method);
}
Annotation[] methodAnnotations = method.getAnnotations();
for (Annotation annotation : methodAnnotations) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be even more efficient to use Method.getAnnotation(Class annotationClass) rather then checking all annotations. Then we can replace this for loop with two null checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about it. Could you clarify? Based on previous logic one method can be annotated with Before/After and any number of step annotations - we can't achieve it without loop. Second thing is that we are not able to get step annotation e.g. @Given using Method.getAnnotation(Class annotationClass) because we don't know what annotation is used (actually cucumber does not care if it is @Given or @Then it just should be annotation that itself is annotated with @StepDefAnnotation ), eventually this code won't work: method.getAnnotation(StepDefAnnotation.class)

Copy link
Contributor

@mpkorstanje mpkorstanje Oct 9, 2017

Choose a reason for hiding this comment

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

Derp. You are correct. I didn't have the hierarchy of annotations properly in my mind.

Copy link
Contributor

@mpkorstanje mpkorstanje left a comment

Choose a reason for hiding this comment

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

Looks good.

This does include a subtle change. Rather then accepting only annotations from cucumber.api we now accept any annotation that implements
StepDefAnnotation with a value and timeout method. Not something third parties should come to rely on. I don't think this will break anything.

@mpkorstanje mpkorstanje changed the title optimized MethodScanner.scan method to not unnecessarily check for all cucumber annotations [Core] Optimize MethodScanner Oct 11, 2017
@mpkorstanje mpkorstanje merged commit 910c574 into cucumber:master Oct 11, 2017
@aslakhellesoy
Copy link
Contributor

Hi @lsuski,

Thanks for your making your first contribution to Cucumber, and welcome to the Cucumber committers team! You can now push directly to this repo and all other repos under the cucumber organization! 🍾

In return for this generous offer we hope you will:

  • ✅ Continue to use branches and pull requests. When someone on the core team approves a pull request (yours or someone else's), you're welcome to merge it yourself.
  • 💚 Commit to setting a good example by following and upholding our code of conduct in your interactions with other collaborators and users.
  • 💬 Join the community Slack channel to meet the rest of the team and make yourself at home.
  • ℹ️ Don't feel obliged to help, just do what you can if you have the time and the energy.
  • 🙋 Ask if you need anything. We're looking for feedback about how to make the project more welcoming, so please tell us!

On behalf of the Cucumber core team,
Aslak Hellesøy
Creator of Cucumber

@lock
Copy link

lock bot commented Oct 24, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants