what is the difference between lazy and lazyFast in kotlin?
Asked Answered
M

1

5

what is the difference between Kotlin's lazy delegate property lazy and lazyFast?.because it looks like same code.

  private val key: String by lazy {
        if (arguments != null && arguments?.getString(Constants.KEY) != null) {
            return@lazy arguments?.getString(Constants.KEY).toString()
        } else {
            return@lazy ""
        }
    }

 private val key: String by lazyFast {
        if (arguments != null && arguments?.getString(Constants.KEY) != null) {
            return@lazyFast arguments?.getString(Constants.KEY).toString()
        } else {
            return@lazyFast ""
        }
    }
Melise answered 4/3, 2020 at 8:0 Comment(1)
lazy is a function from the standard library that instantiates a Lazy object. lazyFast is not, so it can be virtually anything.Perlite
M
7

lazy:-

  • It represents a value with lazy initialization.
  • Gets the lazily initialized value of the current Lazy instance. Once the value was initialized it must not change during the rest of the lifetime of this Lazy instance.
  • Creates a new instance of the Lazy that uses the specified initialization function and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.
  • If the initialization of a value throws an exception, it will attempt to reinitialize the value at the next access.

lazy returns a Lazy object that handles the lambda function(initializer) performing the initialization in a slightly different way according to the thread execution mode(LazyThreadSafetyMode).

public actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: ()
        -> T): Lazy<T> =
                when (mode) {
                    LazyThreadSafetyMode.SYNCHRONIZED -> SynchronizedLazyImpl(initializer)
                    LazyThreadSafetyMode.PUBLICATION -> SafePublicationLazyImpl(initializer)
                    LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer) 
}

lazyFast:-

  • Implementation of lazy that is not thread-safe. Useful when you know what thread you will be executing on and are not worried about synchronization.

lazyFast also returns a Lazy object with mode LazyThreadSafetyMode.NONE

fun <T> lazyFast(operation: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE) {
    operation()
}

LazyThreadSafetyMode.SYNCHRONIZED:-

  • Locks are used to ensure that only a single thread can initialize the Lazy instance.

LazyThreadSafetyMode.PUBLICATION:-

  • Initializer function can be called several times on concurrent access to uninitialized Lazy instance value, but only the first returned value will be used as the value of Lazy instance.

LazyThreadSafetyMode.NONE :-

  • No locks are used to synchronize access to the Lazy instance value; if the instance is accessed from multiple threads, its behavior is undefined. This mode should not be used unless the Lazy instance is guaranteed never to be initialized from more than one thread.
Melise answered 4/3, 2020 at 8:0 Comment(1)
I'm wondering about real world scneraios, if I understand it correctly all of my Lazy properties so far are being called in some fragment or activity, not seeing any case where multiple threads would access/init it at the same time.Carnauba

© 2022 - 2024 — McMap. All rights reserved.