Skip to content

Conversation

@arnaudgiuliani
Copy link
Member

Performance Optimization: Scope Resolution & Thread Safety

This PR introduces two major performance improvements to Koin's core resolution engine:

  1. Double-Checked Locking Pattern

Problem: Original implementation synchronized every instance access, causing lock contention even for already-created instances.

Solution: Implement lock-free fast path with double-checked locking:

// Fast path: check without locking
val existing = values[context.scope.id]
if (existing != null) return existing

// Slow path: create with locking
return synchronized(this) {
values[context.scope.id] ?: create(context)
}

Changes:

  • Added @volatile to SingleInstanceFactory.value for memory visibility
  • Removed unnecessary locks from size(), saveValue(), isCreated()
  • Lock-free reads after initialization
  1. Optimized Scope Resolution (CoreResolverV2)

Problem: Multiple sequential lookups with poor cache locality and unnecessary iterations.

Solution: Consolidated resolution logic in single method:

  • Old order: injected params → registry → stack → scope source → archetype → parents → extensions
  • New order: registry (combined with scope source & parents) → params → stack → extensions

Key improvements:

  • Direct scope + archetype check first
  • Inline scope source lookup (no separate method)
  • Flattened parent hierarchy (single pass with flatten())
  • Early exit when found

Performance Results

Benchmark Before (BETA1) After (BETA2) Improvement
fragmentScope_cascade_activity_root 1803 ns 1578 ns 12.5% faster 🚀
activityScope_cascade_root 882 ns 805 ns 8.8% faster
retrieveDependency 111 ns 104 ns 6.4% faster
start_get_lazy_module100 6.93 ms 4.25 ms 38.7% faster 🚀

Impact

  • Android apps: Faster navigation with deep Fragment/Activity hierarchies
  • Multi-threaded apps: Eliminates lock contention in concurrent access
  • Large apps: Significantly faster lazy module loading

Modified Files

  • core/koin-core/src/commonMain/kotlin/org/koin/core/instance/ScopedInstanceFactory.kt
  • core/koin-core/src/commonMain/kotlin/org/koin/core/instance/SingleInstanceFactory.kt
  • core/koin-core/src/commonMain/kotlin/org/koin/core/resolution/CoreResolverV2.kt (new)

Thread Safety

✅ Double-checked locking with @volatile ensures memory visibility
✅ Synchronized blocks prevent race conditions
✅ Uses safeHashMap (ConcurrentHashMap) for scope values


Breaking Changes: None (internal optimization only)

Fixes: #1990

@arnaudgiuliani arnaudgiuliani added this to the 4.2.0 milestone Dec 9, 2025
@arnaudgiuliani arnaudgiuliani merged commit 55a40e4 into 4.2.0 Dec 9, 2025
3 of 4 checks passed
@arnaudgiuliani arnaudgiuliani deleted the fix_scope_direct_resolution branch December 9, 2025 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants