For #21183: remove lazyMonitored wrapper functionality.

We're not using the functionality so there's no sense in doing the work.
However, we keep the wrapper: see the code comments for details.
upstream-sync
Michael Comella 3 years ago committed by mergify[bot]
parent 09fe569b30
commit 4fe6a4506d

@ -4,42 +4,12 @@
package org.mozilla.fenix.perf
import mozilla.components.support.base.log.logger.Logger
import java.util.concurrent.atomic.AtomicInteger
private val logger = Logger("LazyMonitored")
/**
* A container for the number of components initialized.
*/
object ComponentInitCount {
val count = AtomicInteger(0)
}
/**
* A convenience function for setting the [LazyMonitored] property delegate, which wraps
* [lazy] to add performance monitoring.
* A function which wraps [lazy].
*
* This functionality was previously used to add performance monitoring. This
* wrapper could be useful in the future to add more monitoring. Even though
* this method is unused, we keep the code because re-adding this wrapper to
* every component is non-trivial.
*/
fun <T> lazyMonitored(initializer: () -> T): Lazy<T> = LazyMonitored(initializer)
/**
* A wrapper around the [lazy] property delegate to monitor for performance related issues.
* 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.
*/
private class LazyMonitored<T>(initializer: () -> T) : Lazy<T> {
// Lazy is thread safe.
private val lazyValue = lazy {
// We're unlikely to have 4 billion components so we don't handle overflow.
val componentInitCount = ComponentInitCount.count.incrementAndGet()
initializer().also {
@Suppress("UNNECESSARY_NOT_NULL_ASSERTION") // the compiler fails with !! but warns with !!.
val className = if (it == null) "null" else it!!::class.java.canonicalName
logger.debug("Init component #$componentInitCount: $className")
}
}
override val value: T get() = lazyValue.value
override fun isInitialized(): Boolean = lazyValue.isInitialized()
}
fun <T> lazyMonitored(initializer: () -> T): Lazy<T> = lazy(initializer)

@ -1,36 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.perf
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
class LazyMonitoredTest {
private val componentInitCount get() = ComponentInitCount.count.get()
@Before
fun setUp() {
ComponentInitCount.count.set(0)
}
@Test
fun `WHEN accessing a lazy monitored THEN it returns the initializer value`() {
val actual by lazyMonitored { 4 }
assertEquals(4, actual)
}
@Test
fun `WHEN accessing a lazy monitored THEN the component init count is incremented`() {
assertEquals(0, componentInitCount)
val monitored by lazyMonitored { }
// We must access the value to trigger init.
@Suppress("UNUSED_EXPRESSION") monitored
assertEquals(1, componentInitCount)
}
}
Loading…
Cancel
Save