From ff7c1fd25ae8a2ddc99caeef6eb524468aef7c90 Mon Sep 17 00:00:00 2001 From: Michal Zielinski Date: Tue, 1 Oct 2024 03:53:33 -0700 Subject: [PATCH] Rename getPoolableContentType API to getPoolKey Summary: As per title, this diff renames `ContentAllocator.getPoolableContentType` to `ContentAllocator.getPoolKey`. Reviewed By: adityasharat Differential Revision: D63682116 fbshipit-source-id: 54e5b5ef1d42f915a024f2a540edeb9f4a38ed1e --- .../litho/MountSpecLithoRenderUnit.kt | 2 +- .../litho/SpecGeneratedComponent.java | 4 +-- .../com/facebook/litho/HostRecyclingTest.kt | 4 +-- .../rendercore/primitives/ContentAllocator.kt | 6 ++--- .../rendercore/primitives/MountBehavior.kt | 2 +- .../testing/DrawableWrapperUnit.java | 2 +- .../rendercore/testing/ViewWrapperUnit.java | 2 +- .../facebook/rendercore/ContentAllocator.kt | 4 +-- .../com/facebook/rendercore/MountItemsPool.kt | 25 ++++++++----------- .../com/facebook/rendercore/MountState.kt | 4 +-- .../java/com/facebook/rendercore/Reducer.kt | 2 +- .../facebook/rendercore/utils/CommonUtils.kt | 6 ++--- .../com/facebook/rendercore/MountStateTest.kt | 4 +-- 13 files changed, 32 insertions(+), 35 deletions(-) diff --git a/litho-core/src/main/java/com/facebook/litho/MountSpecLithoRenderUnit.kt b/litho-core/src/main/java/com/facebook/litho/MountSpecLithoRenderUnit.kt index 57ce951ad7..ce60010f3f 100644 --- a/litho-core/src/main/java/com/facebook/litho/MountSpecLithoRenderUnit.kt +++ b/litho-core/src/main/java/com/facebook/litho/MountSpecLithoRenderUnit.kt @@ -95,7 +95,7 @@ private constructor( return component.createMountContent(c) } - override fun getPoolableContentType(): Any { + override fun getPoolKey(): Any { return component.javaClass } diff --git a/litho-core/src/main/java/com/facebook/litho/SpecGeneratedComponent.java b/litho-core/src/main/java/com/facebook/litho/SpecGeneratedComponent.java index 66cc36cbb7..3518084af7 100644 --- a/litho-core/src/main/java/com/facebook/litho/SpecGeneratedComponent.java +++ b/litho-core/src/main/java/com/facebook/litho/SpecGeneratedComponent.java @@ -808,7 +808,7 @@ public RenderUnit.RenderType getRenderType() { } @Override - public Object getPoolableContentType() { + public Object getPoolKey() { return getClass(); } @@ -825,7 +825,7 @@ public boolean canPreallocate() { */ @Override public MountItemsPool.ItemPool onCreateMountContentPool(int poolSizeOverride) { - return new MountItemsPool.DefaultItemPool(getPoolableContentType(), poolSizeOverride); + return new MountItemsPool.DefaultItemPool(getPoolKey(), poolSizeOverride); } @ThreadSafe diff --git a/litho-it/src/test/java/com/facebook/litho/HostRecyclingTest.kt b/litho-it/src/test/java/com/facebook/litho/HostRecyclingTest.kt index 3dcaa90bf9..8d0dad4621 100644 --- a/litho-it/src/test/java/com/facebook/litho/HostRecyclingTest.kt +++ b/litho-it/src/test/java/com/facebook/litho/HostRecyclingTest.kt @@ -151,8 +151,8 @@ class HostRecyclingTest { private val itemPool = MountItemsPool.DefaultItemPool(HostComponent::class.java, 5) override fun acquire(contentAllocator: ContentAllocator<*>): Any? { - val poolableContentType = contentAllocator.getPoolableContentType() - if (poolableContentType == HostComponent::class.java) { + val poolKey = contentAllocator.getPoolKey() + if (poolKey == HostComponent::class.java) { _numComponentHostAcquireRequests++ val poolContent = itemPool.acquire(contentAllocator) if (poolContent != null) { diff --git a/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/ContentAllocator.kt b/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/ContentAllocator.kt index 083bd65ec8..5203f620d5 100644 --- a/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/ContentAllocator.kt +++ b/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/ContentAllocator.kt @@ -47,7 +47,7 @@ class ViewAllocator( override fun canPreallocate(): Boolean = canPreallocate - override fun getPoolableContentType(): Any = allocator.javaClass + override fun getPoolKey(): Any = allocator.javaClass } /** @@ -73,7 +73,7 @@ class DrawableAllocator( override fun canPreallocate(): Boolean = canPreallocate - override fun getPoolableContentType(): Any = allocator.javaClass + override fun getPoolKey(): Any = allocator.javaClass } /** @@ -91,7 +91,7 @@ fun ContentAllocator.withContentType( contentType: Any ): ContentAllocator { return object : ContentAllocator by this { - override fun getPoolableContentType(): Any { + override fun getPoolKey(): Any { return contentType } } diff --git a/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/MountBehavior.kt b/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/MountBehavior.kt index 272a90c5e6..d3d9678027 100644 --- a/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/MountBehavior.kt +++ b/litho-rendercore-primitives/src/main/java/com/facebook/rendercore/primitives/MountBehavior.kt @@ -64,7 +64,7 @@ class MountBehavior( override val description: String get() = this@MountBehavior.description?.invoke()?.take(MAX_DESCRIPTION_LENGTH) - ?: getSectionNameForTracing(contentAllocator.getPoolableContentType()) + ?: getSectionNameForTracing(contentAllocator.getPoolKey()) } } } diff --git a/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/DrawableWrapperUnit.java b/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/DrawableWrapperUnit.java index 37e0ec9111..01efa63c58 100644 --- a/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/DrawableWrapperUnit.java +++ b/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/DrawableWrapperUnit.java @@ -42,7 +42,7 @@ public Drawable createContent(Context c) { } @Override - public Class getPoolableContentType() { + public Class getPoolKey() { return drawable.getClass(); } diff --git a/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/ViewWrapperUnit.java b/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/ViewWrapperUnit.java index da1a258c9d..eb077b1e51 100644 --- a/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/ViewWrapperUnit.java +++ b/litho-rendercore-testing/src/main/java/com/facebook/rendercore/testing/ViewWrapperUnit.java @@ -55,7 +55,7 @@ public View createContent(Context c) { } @Override - public Object getPoolableContentType() { + public Object getPoolKey() { return view.getClass(); } diff --git a/litho-rendercore/src/main/java/com/facebook/rendercore/ContentAllocator.kt b/litho-rendercore/src/main/java/com/facebook/rendercore/ContentAllocator.kt index af2013e522..731f0b5f19 100644 --- a/litho-rendercore/src/main/java/com/facebook/rendercore/ContentAllocator.kt +++ b/litho-rendercore/src/main/java/com/facebook/rendercore/ContentAllocator.kt @@ -35,8 +35,8 @@ interface ContentAllocator { /** Returns the [RenderUnit.RenderType] of the mountable content. */ val renderType: RenderType - /** Returns the class of the content of the mountable content. */ - fun getPoolableContentType(): Any { + /** Returns the key that should be used for the pool for the mountable content. */ + fun getPoolKey(): Any { return javaClass } diff --git a/litho-rendercore/src/main/java/com/facebook/rendercore/MountItemsPool.kt b/litho-rendercore/src/main/java/com/facebook/rendercore/MountItemsPool.kt index d33ea379d2..8f49dcbabb 100644 --- a/litho-rendercore/src/main/java/com/facebook/rendercore/MountItemsPool.kt +++ b/litho-rendercore/src/main/java/com/facebook/rendercore/MountItemsPool.kt @@ -99,7 +99,7 @@ object MountItemsPool { val isTracing = RenderCoreSystrace.isTracing() if (isTracing) { RenderCoreSystrace.beginSection( - "MountItemsPool:createMountContent ${poolableMountContent.poolableContentTypeName}") + "MountItemsPool:createMountContent ${poolableMountContent.poolKeyTypeName}") } val content = poolableMountContent.createContent(context) if (isTracing) { @@ -135,7 +135,7 @@ object MountItemsPool { if (mountItemPoolsReleaseValidator != null && mountContent is View) { mountItemPoolsReleaseValidator?.assertValidRelease( - mountContent, listOf(poolableMountContent.poolableContentTypeName)) + mountContent, listOf(poolableMountContent.poolKeyTypeName)) } pool.release(mountContent) @@ -202,20 +202,18 @@ object MountItemsPool { mountContentPoolsByContext[context] = poolsMap } - val poolableContentType = allocator.getPoolableContentType() - var pool = poolsMap[poolableContentType] + val poolKey = allocator.getPoolKey() + var pool = poolsMap[poolKey] if (pool == null && hasMountContentPoolFactory) { pool = mountContentPoolFactory.get()?.createMountContentPool() } if (pool == null) { - pool = - allocator.onCreateMountContentPool(poolSize) - ?: DefaultItemPool(poolableContentType, poolSize) + pool = allocator.onCreateMountContentPool(poolSize) ?: DefaultItemPool(poolKey, poolSize) } - poolsMap[poolableContentType] = pool + poolsMap[poolKey] = pool return pool } } @@ -333,10 +331,10 @@ object MountItemsPool { return currentContext === baseCtx } - private val ContentAllocator<*>.poolableContentTypeName: String + private val ContentAllocator<*>.poolKeyTypeName: String get() { - val poolableContentType: Any = getPoolableContentType() - return (poolableContentType as? Class<*>)?.simpleName ?: poolableContentType.toString() + val poolKey: Any = getPoolKey() + return (poolKey as? Class<*>)?.simpleName ?: poolKey.toString() } /** Empty implementation of the [Application.ActivityLifecycleCallbacks] interface */ @@ -440,12 +438,11 @@ object MountItemsPool { fun maybePreallocateContent(c: Context, contentAllocator: ContentAllocator<*>): Boolean } - open class DefaultItemPool(poolableContentType: Any, private val maxPoolSize: Int) : ItemPool { + open class DefaultItemPool(poolKey: Any, private val maxPoolSize: Int) : ItemPool { private val pool: Pools.SynchronizedPool = Pools.SynchronizedPool(maxPoolSize) - private val debugIdentifier: String = - (poolableContentType as? Class<*>)?.name ?: poolableContentType.toString() + private val debugIdentifier: String = (poolKey as? Class<*>)?.name ?: poolKey.toString() private val currentPoolSize: AtomicInteger = AtomicInteger(0) diff --git a/litho-rendercore/src/main/java/com/facebook/rendercore/MountState.kt b/litho-rendercore/src/main/java/com/facebook/rendercore/MountState.kt index febda2185f..ef80c79d32 100644 --- a/litho-rendercore/src/main/java/com/facebook/rendercore/MountState.kt +++ b/litho-rendercore/src/main/java/com/facebook/rendercore/MountState.kt @@ -945,8 +945,8 @@ constructor( throw RuntimeException( """ Trying to mount a RenderTreeNode, its parent should be a Host, but was '${parentContent.javaClass.simpleName}'. - Parent RenderUnit: id=${parentRenderUnit.id}; contentType='${parentRenderUnit.contentAllocator.getPoolableContentType()}'. - Child RenderUnit: id=${renderUnit.id}; contentType='${renderUnit.contentAllocator.getPoolableContentType()}'. + Parent RenderUnit: id=${parentRenderUnit.id}; poolKey='${parentRenderUnit.contentAllocator.getPoolKey()}'. + Child RenderUnit: id=${renderUnit.id}; poolKey='${renderUnit.contentAllocator.getPoolKey()}'. """ .trimIndent()) } diff --git a/litho-rendercore/src/main/java/com/facebook/rendercore/Reducer.kt b/litho-rendercore/src/main/java/com/facebook/rendercore/Reducer.kt index c7b810654c..7cbdcae176 100644 --- a/litho-rendercore/src/main/java/com/facebook/rendercore/Reducer.kt +++ b/litho-rendercore/src/main/java/com/facebook/rendercore/Reducer.kt @@ -194,7 +194,7 @@ object Reducer { private class RootHostRenderUnit : RenderUnit(RenderType.VIEW), ContentAllocator { override fun createContent(context: Context): Any = Unit - override fun getPoolableContentType(): Any = javaClass + override fun getPoolKey(): Any = javaClass override val contentAllocator: ContentAllocator = this diff --git a/litho-rendercore/src/main/java/com/facebook/rendercore/utils/CommonUtils.kt b/litho-rendercore/src/main/java/com/facebook/rendercore/utils/CommonUtils.kt index 6d09456853..47b5e9e417 100644 --- a/litho-rendercore/src/main/java/com/facebook/rendercore/utils/CommonUtils.kt +++ b/litho-rendercore/src/main/java/com/facebook/rendercore/utils/CommonUtils.kt @@ -32,11 +32,11 @@ object CommonUtils { /** * This API is used for tracing. It's the same as getSectionNameForTracing(Class), but it uses - * [contentType] instead of the class name. + * [poolKey] instead of the class name. */ @JvmStatic - fun getSectionNameForTracing(contentType: Any): String { - return "contentType:$contentType".take(127) + fun getSectionNameForTracing(poolKey: Any): String { + return "poolKey:$poolKey".take(127) } /** Utility to re-throw exceptions. */ diff --git a/litho-rendercore/src/test/java/com/facebook/rendercore/MountStateTest.kt b/litho-rendercore/src/test/java/com/facebook/rendercore/MountStateTest.kt index 22fe7dc6cb..6d33228c97 100644 --- a/litho-rendercore/src/test/java/com/facebook/rendercore/MountStateTest.kt +++ b/litho-rendercore/src/test/java/com/facebook/rendercore/MountStateTest.kt @@ -756,8 +756,8 @@ class MountStateTest { expectedException.expectMessage( """ Trying to mount a RenderTreeNode, its parent should be a Host, but was 'LinearLayout'. - Parent RenderUnit: id=1; contentType='class android.widget.LinearLayout'. - Child RenderUnit: id=2; contentType='class android.view.View'. + Parent RenderUnit: id=1; poolKey='class android.widget.LinearLayout'. + Child RenderUnit: id=2; poolKey='class android.view.View'. """ .trimIndent()) val c = renderCoreTestRule.context