Skip to content

Commit

Permalink
Rename getPoolableContentType API to getPoolKey
Browse files Browse the repository at this point in the history
Summary: As per title, this diff renames `ContentAllocator.getPoolableContentType` to `ContentAllocator.getPoolKey`.

Reviewed By: adityasharat

Differential Revision: D63682116

fbshipit-source-id: 54e5b5ef1d42f915a024f2a540edeb9f4a38ed1e
  • Loading branch information
zielinskimz authored and facebook-github-bot committed Oct 1, 2024
1 parent 2cd664e commit ff7c1fd
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private constructor(
return component.createMountContent(c)
}

override fun getPoolableContentType(): Any {
override fun getPoolKey(): Any {
return component.javaClass
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public RenderUnit.RenderType getRenderType() {
}

@Override
public Object getPoolableContentType() {
public Object getPoolKey() {
return getClass();
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ViewAllocator<Content : View>(

override fun canPreallocate(): Boolean = canPreallocate

override fun getPoolableContentType(): Any = allocator.javaClass
override fun getPoolKey(): Any = allocator.javaClass
}

/**
Expand All @@ -73,7 +73,7 @@ class DrawableAllocator<Content : Drawable>(

override fun canPreallocate(): Boolean = canPreallocate

override fun getPoolableContentType(): Any = allocator.javaClass
override fun getPoolKey(): Any = allocator.javaClass
}

/**
Expand All @@ -91,7 +91,7 @@ fun <Content : Any> ContentAllocator<Content>.withContentType(
contentType: Any
): ContentAllocator<Content> {
return object : ContentAllocator<Content> by this {
override fun getPoolableContentType(): Any {
override fun getPoolKey(): Any {
return contentType
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MountBehavior<ContentType : Any>(
override val description: String
get() =
this@MountBehavior.description?.invoke()?.take(MAX_DESCRIPTION_LENGTH)
?: getSectionNameForTracing(contentAllocator.getPoolableContentType())
?: getSectionNameForTracing(contentAllocator.getPoolKey())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Drawable createContent(Context c) {
}

@Override
public Class<?> getPoolableContentType() {
public Class<?> getPoolKey() {
return drawable.getClass();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public View createContent(Context c) {
}

@Override
public Object getPoolableContentType() {
public Object getPoolKey() {
return view.getClass();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ interface ContentAllocator<Content : Any> {
/** 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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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<Any> = 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ object Reducer {
private class RootHostRenderUnit : RenderUnit<Any>(RenderType.VIEW), ContentAllocator<Any> {
override fun createContent(context: Context): Any = Unit

override fun getPoolableContentType(): Any = javaClass
override fun getPoolKey(): Any = javaClass

override val contentAllocator: ContentAllocator<Any> = this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff7c1fd

Please sign in to comment.