You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ScopedInstanceFactory and SingleInstanceFactory both lock on every call to get. This adds unnecessary overhead since locking is only required during the first initialization of an object.
Koin should add double-checked locking so that we can avoid locking every time we inject a single or scoped instance.
// In SingleInstanceFactory
@Volatile
privatevar value:T?=nulloverridefunget(context:InstanceContext): T {
// Fast path to avoid locking if this instance is already initializedif (!isCreated(context)) {
KoinPlatformTools.synchronized(this) {
if (!isCreated(context)) {
value = create(context)
}
}
}
return getValue()
}
Note that the multiplatform @Volatile is necessary for this to be correct on JVM and native platforms.
The text was updated successfully, but these errors were encountered:
ScopedInstanceFactory and SingleInstanceFactory both lock on every call to
get
. This adds unnecessary overhead since locking is only required during the first initialization of an object.Koin should add double-checked locking so that we can avoid locking every time we inject a single or scoped instance.
See Dagger's
DoubleCheck
for an example in Java that is widely employed: https://github.com/google/dagger/blob/f8a09b29683687720ae3e0e5ac241f3a4a054d6e/java/dagger/internal/DoubleCheck.java#L29Example implementation
Note that the multiplatform @Volatile is necessary for this to be correct on JVM and native platforms.
The text was updated successfully, but these errors were encountered: