fix: resolve @InjectMockKs dependency order for interface#1526
Merged
Raibaz merged 3 commits intoApr 22, 2026
Merged
Conversation
…interface types Signed-off-by: logan <gnlee95@gmail.com>
…isSubclassOf only on null Signed-off-by: logan <gnlee95@gmail.com>
Signed-off-by: logan <gnlee95@gmail.com>
Collaborator
|
Thanks a lot for looking into this and for the detailed analysis! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@InjectMockKsfailing to resolve dependency order when a constructor parameter is an interface (or superclass) typetypeToPropertylookup as the fast path and fall back toisSubclassOfonly when no exact provider is found, preserving existing performanceProblem
When
@InjectMockKsproperties depend on an interface (or any supertype) rather than the concrete provider type, dependency-order resolution introduced in #1500 could not match them. The exact-type lookup never found the implementation, so the graph treated the dependency as absent and the consumer was initialized before its dependency.Here
consumer'sInterfaceDependencyparameter never matched theInterfaceDependencyImplprovider, so the dependency edge was missing andconsumercould be created without the implementation being available.Solution
Match providers against a constructor parameter type using a subtype check instead of strict equality. To avoid a hot-path regression (subtype reflection is much more expensive than a hash lookup), probe the existing
typeToPropertymap first and fall back to a subtype scan over its entries only on miss.typeToProperty[paramType] ?: typeToProperty.entries .firstOrNull { (providerType, _) -> providerType.isSubclassOf(paramType) } ?.valueThis covers both interface implementations and class inheritance while keeping the common concrete-type case at O(1) per parameter.
Changes
modules/mockk/src/jvmMain/kotlin/io/mockk/impl/annotations/JvmMockInitializer.ktbuildDependencyGraph(...):typeToProperty.entriesusingKClass.isSubclassOfwhen no exact provider existsmodules/mockk/src/commonTest/kotlin/io/mockk/it/InjectMocksTest.ktinterfaceDependency()— consumer depends on an interface provided by a sibling@InjectMockKsTest
Performance impact of DependencyOrder in Mock initialization
I ran JMH benchmarks comparing initWithDependencyOrder vs initWithoutDependencyOrder across different dependency graph shapes and sizes. The nested interface test covers interface nesting up to 5 levels deep.
Focusing on the more stable and realistic case (size = 20, thrpt mode):
• independent: initWithoutDependencyOrder is ~0.8% faster than initWithDependencyOrder
• wide: initWithoutDependencyOrder is ~3.1% faster than initWithDependencyOrder
• linear: initWithoutDependencyOrder is ~5.5% faster than initWithDependencyOrder
• diamond: initWithoutDependencyOrder is ~2.5% faster than initWithDependencyOrder
• interface: initWithoutDependencyOrder is ~9.9% faster than initWithDependencyOrder
• nested interface: initWithoutDependencyOrder is ~14.6% faster than initWithDependencyOrder
Overall, initWithoutDependencyOrder again does not show overhead compared to initWithDependencyOrder. For size = 20, it is on average about 6.1% lower cost per operation. In throughput terms, applying DependencyOrder introduces about 6.7% throughput regression on average for size = 20. Existing cases show 3-5% overhead and show comparable results to previous tests. However, tests with interfaces show performance degradation of up to 14.6%. I think the figure is acceptable because we applied opt-in.
main summary
overhead summary