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

Cache LifecycleMethods results: findBeforeTryMethods, findAfterTryMethods, etc #429

Closed
vlsi opened this issue Dec 14, 2022 · 1 comment · Fixed by #430
Closed

Cache LifecycleMethods results: findBeforeTryMethods, findAfterTryMethods, etc #429

vlsi opened this issue Dec 14, 2022 · 1 comment · Fixed by #430

Comments

@vlsi
Copy link
Contributor

vlsi commented Dec 14, 2022

Testing Problem

LifecycleMethods consumes significant CPU time performing the same reflective lookup.

Suggested Solution

Cache the results via https://docs.oracle.com/javase/7/docs/api/java/lang/ClassValue.html or something like that.

Profiling results

Allocation:

allocation profiling of LifecycleMethods

CPU:

CPU profiling of LifecycleMethods

@vlsi
Copy link
Contributor Author

vlsi commented Dec 14, 2022

Sample fix:

+       private static final ClassValue<List<Method>> findBeforeTryMethods = new ClassValue<List<Method>>() {
+               @Override
+               protected List<Method> computeValue(Class<?> testClass) {
+                       return findMethods(testClass, false, true, BeforeTry.class, HierarchyTraversalMode.TOP_DOWN);
+               }
+       };
+
        static List<Method> findBeforeTryMethods(Class<?> testClass) {
-               return findMethods(testClass, false, true, BeforeTry.class, HierarchyTraversalMode.TOP_DOWN);
+               return findBeforeTryMethods.get(testClass);
        }

+       private static final ClassValue<List<Method>> findAfterTryMethods = new ClassValue<List<Method>>() {
+               @Override
+               protected List<Method> computeValue(Class<?> testClass) {
+                       return findMethods(testClass, false, true, AfterTry.class, HierarchyTraversalMode.BOTTOM_UP);
+               }
+       };
+
        static List<Method> findAfterTryMethods(Class<?> testClass) {
-               return findMethods(testClass, false, true, AfterTry.class, HierarchyTraversalMode.BOTTOM_UP);
+               return findAfterTryMethods.get(testClass);
        }

WDYT?

It improves shrink time from ~60s to 30sec for case like #428 (comment) (increasing .withMaxTransformations(12) makes the case WAY longer to execute)

vlsi pushed a commit to vlsi/jqwik that referenced this issue Dec 14, 2022
vlsi pushed a commit to vlsi/jqwik that referenced this issue Dec 14, 2022
jlink pushed a commit that referenced this issue Dec 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant