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

RecipeSpec recipe caching #3609

Merged
merged 3 commits into from
Oct 26, 2023
Merged

RecipeSpec recipe caching #3609

merged 3 commits into from
Oct 26, 2023

Conversation

nmck257
Copy link
Collaborator

@nmck257 nmck257 commented Oct 9, 2023

What's changed?

RecipeSpec methods recipeFromYaml, recipeFromResource, and recipeFromResources now memoize Recipes to avoid repeated redundant Environment instantiation / classpath scanning

What's your motivation?

Speed of unit tests; these methods (especially when calling scanYamlResources) can cost a few hundred milliseconds, which adds up fast if a large test class is using them.

Have you considered any alternatives or workarounds?

  • For a lighter touch, we could store only the Environment built with the scanYamlResources call in a lazy-init static field; this probably delivers ~most of the speed benefits with less code and less mem
  • Using a memoization/caching library might be nicer, but, at the cost of another dependency
  • Hashing together the relevant Strings (instead of joining them) might make for cheaper keys, but I struggled to implement that concisely since doing fancy stuff with varargs is a bit painful in java (no spread operator), esp 8 (fewer helper methods for instantiating lists, etc)
  • I tried caching the customized testClassSpec objects used by rewriteRun, but that was impractical since RewriteTest is an interface and can't support instance variables
  • A step further on the above, I even experimented with a static WeakHashMap<Consumer<RecipeSpec>, RecipeSpec> to cache testClassSpec objects based on the customizer (eg this::defaults) which would produce them, but I couldn't get keys with consistent memory references. And even if I could figure out clever lambda syntax to make that happen, it would depend on the test class being configured to as TestInstanceLifecycle(PER_CLASS), which is non-default and has other ramifications

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ IDEA auto-formatter on affected files

…ipeFromResources` now memoize Recipes to avoid repeated redundant Environment instantiation / classpath scanning
@nmck257
Copy link
Collaborator Author

nmck257 commented Oct 9, 2023

test failures look unrelated (something about the rewrite-gradle model failing to resolve the rewrite-core:8.8.0-SNAPSHOT dep)

@timtebeek timtebeek added enhancement New feature or request performance labels Oct 10, 2023
@sambsnyd sambsnyd merged commit 494c691 into main Oct 26, 2023
1 check passed
@sambsnyd sambsnyd deleted the feature/recipe-spec-cache branch October 26, 2023 22:32
@sambsnyd
Copy link
Member

Thanks @nmck257 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants