From 89541a17f72878770064524b2a68c0e73900452b Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Fri, 30 Oct 2020 12:51:38 -0700 Subject: [PATCH] For #15279: LazyMonitored implement Lazy + update built-in API use. By having LazyMonitored implement Lazy, we can continue to pass these values directly into the ac APIs that require Lazy references. For some reason, implementing `Lazy.value` can replace `operator fun getValue` required for delegates. --- app/src/main/java/org/mozilla/fenix/components/Core.kt | 8 ++++---- app/src/main/java/org/mozilla/fenix/perf/LazyMonitored.kt | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/Core.kt b/app/src/main/java/org/mozilla/fenix/components/Core.kt index 65ccf94a7..687289338 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Core.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Core.kt @@ -293,14 +293,14 @@ class Core( // Use these for startup-path code, where we don't want to do any work that's not strictly necessary. // For example, this is how the GeckoEngine delegates (history, logins) are configured. // We can fully initialize GeckoEngine without initialized our storage. - val lazyHistoryStorage = lazy { PlacesHistoryStorage(context, crashReporter) } - val lazyBookmarksStorage = lazy { PlacesBookmarksStorage(context) } - val lazyPasswordsStorage = lazy { SyncableLoginsStorage(context, passwordsEncryptionKey) } + val lazyHistoryStorage = lazyMonitored { PlacesHistoryStorage(context, crashReporter) } + val lazyBookmarksStorage = lazyMonitored { PlacesBookmarksStorage(context) } + val lazyPasswordsStorage = lazyMonitored { SyncableLoginsStorage(context, passwordsEncryptionKey) } /** * The storage component to sync and persist tabs in a Firefox Sync account. */ - val lazyRemoteTabsStorage = lazy { RemoteTabsStorage() } + val lazyRemoteTabsStorage = lazyMonitored { RemoteTabsStorage() } // For most other application code (non-startup), these wrappers are perfectly fine and more ergonomic. val historyStorage by lazyMonitored { lazyHistoryStorage.value } diff --git a/app/src/main/java/org/mozilla/fenix/perf/LazyMonitored.kt b/app/src/main/java/org/mozilla/fenix/perf/LazyMonitored.kt index d8ec7a96d..d9efde5f7 100644 --- a/app/src/main/java/org/mozilla/fenix/perf/LazyMonitored.kt +++ b/app/src/main/java/org/mozilla/fenix/perf/LazyMonitored.kt @@ -27,7 +27,7 @@ fun lazyMonitored(initializer: () -> T) = LazyMonitored(initializer) * For example, we can count the number of components initialized to see how the number of * components initialized on start up impacts start up time. */ -class LazyMonitored(initializer: () -> T) { +class LazyMonitored(initializer: () -> T) : Lazy { // Lazy is thread safe. private val lazyValue = lazy { // We're unlikely to have 4 billion components so we don't handle overflow. @@ -40,5 +40,6 @@ class LazyMonitored(initializer: () -> T) { } } - operator fun getValue(thisRef: Any?, property: KProperty<*>): T = lazyValue.value + override val value: T get() = lazyValue.value + override fun isInitialized(): Boolean = lazyValue.isInitialized() }