Skip to content

Commit 5d580bb

Browse files
Andrew Wangfacebook-github-bot
authored andcommitted
Refactor the API of cotent pool
Summary: ## Context Now we have two interface methods, `createRecyclingPool` and `onCreateMountContentPool`, which is confusing and caused wrong usages. This diff is to remove one of which and make `onCreateMountContentPool` the single truth of providing content pool. Reviewed By: adityasharat Differential Revision: D60367366 fbshipit-source-id: 78e2a91c306edb2ec44f0eba7a925b9aa7447754
1 parent 9c83ea0 commit 5d580bb

File tree

15 files changed

+40
-40
lines changed

15 files changed

+40
-40
lines changed

litho-annotations/src/main/java/com/facebook/litho/annotations/OnCreateMountContentPool.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
* The annotated method will be called to create a custom MountContentPool for this mount spec. If a
2424
* method with this annotation isn't provided, a DefaultMountContentPool will be used to recycle the
2525
* mount content of this mount spec, which is almost always sufficient.
26+
*
27+
* <p>For example:
28+
*
29+
* <pre><code>
30+
*
31+
* {@literal @OnCreateMountContentPool}
32+
* static MountItemsPool.ItemPool OnCreateMountContentPool(int poolSizeOverride) {
33+
* return new MountItemsPool.ItemPool(poolSizeOverride);
34+
* }
35+
* </code></pre>
2636
*/
2737
@Retention(RetentionPolicy.SOURCE)
2838
public @interface OnCreateMountContentPool {}

litho-core/src/main/java/com/facebook/litho/HostComponent.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ protected HostComponent(
4646
mPoolingPolicy = poolingPolicy;
4747
}
4848

49-
@Override
50-
public MountItemsPool.ItemPool onCreateMountContentPool() {
51-
return new HostMountContentPool(
52-
ComponentsConfiguration.hostComponentPoolSize,
53-
mPoolingPolicy.canAcquireContent || mPoolingPolicy.canReleaseContent);
54-
}
55-
5649
@Override
5750
public PoolingPolicy getPoolingPolicy() {
5851
return mPoolingPolicy;
@@ -68,6 +61,12 @@ protected Object onCreateMountContent(Context c) {
6861
return new ComponentHost(c, mUnsafeModificationPolicy);
6962
}
7063

64+
@Override
65+
public MountItemsPool.ItemPool onCreateMountContentPool(int poolSizeOverride) {
66+
return new HostMountContentPool(
67+
poolSizeOverride, mPoolingPolicy.canAcquireContent || mPoolingPolicy.canReleaseContent);
68+
}
69+
7170
@Override
7271
protected void onMount(
7372
final @Nullable ComponentContext c,
@@ -134,7 +133,7 @@ public boolean isEquivalentProps(@Nullable Component other, boolean shouldCompar
134133

135134
@Override
136135
public int poolSize() {
137-
return 45;
136+
return ComponentsConfiguration.hostComponentPoolSize;
138137
}
139138

140139
@Override

litho-core/src/main/java/com/facebook/litho/HostMountContentPool.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class HostMountContentPool(maxSize: Int, isEnabled: Boolean) : ItemPool {
4747
return false
4848
}
4949

50-
return pool?.release(item) ?: false
50+
return pool.release(item)
5151
}
5252

5353
override fun maybePreallocateContent(c: Context, contentAllocator: ContentAllocator<*>): Boolean =

litho-core/src/main/java/com/facebook/litho/MountSpecLithoRenderUnit.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ private constructor(
7676
isShouldUpdateResultCached = false
7777
}
7878

79-
override fun createRecyclingPool(poolSizeOverride: Int): ItemPool? {
79+
override fun onCreateMountContentPool(poolSizeOverride: Int): ItemPool? {
8080
return try {
8181
if (component is SpecGeneratedComponent) {
82-
component.createRecyclingPool()
82+
component.onCreateMountContentPool(poolSizeOverride)
8383
} else {
8484
null
8585
}

litho-core/src/main/java/com/facebook/litho/SpecGeneratedComponent.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public abstract class SpecGeneratedComponent extends Component
5858
EventTriggerTarget,
5959
HasEventTrigger {
6060

61-
private static final int DEFAULT_MAX_PREALLOCATION = 3;
6261
private static final DynamicValue[] sEmptyArray = new DynamicValue[0];
6362

6463
private final String mSimpleName;
@@ -817,12 +816,6 @@ public Class<?> getPoolableContentType() {
817816
return getClass();
818817
}
819818

820-
@Nullable
821-
@Override
822-
public MountItemsPool.ItemPool createRecyclingPool(int poolSize) {
823-
return onCreateMountContentPool();
824-
}
825-
826819
/**
827820
* @return true if this component can be preallocated.
828821
*/
@@ -835,8 +828,8 @@ public boolean canPreallocate() {
835828
* @return the MountContentPool that should be used to recycle mount content for this mount spec.
836829
*/
837830
@Override
838-
public MountItemsPool.ItemPool onCreateMountContentPool() {
839-
return new MountItemsPool.DefaultItemPool(getPoolableContentType(), poolSize());
831+
public MountItemsPool.ItemPool onCreateMountContentPool(int poolSizeOverride) {
832+
return new MountItemsPool.DefaultItemPool(getPoolableContentType(), poolSizeOverride);
840833
}
841834

842835
@ThreadSafe

litho-it/src/main/java/com/facebook/litho/widget/CrashingMountableSpec.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ object CrashingMountableSpec {
134134

135135
@JvmStatic
136136
@OnCreateMountContentPool
137-
fun onCreateMountContentPool(@InjectProp lifecycle: LifecycleStep): MountItemsPool.ItemPool {
137+
fun onCreateMountContentPool(
138+
poolSize: Int,
139+
@InjectProp lifecycle: LifecycleStep
140+
): MountItemsPool.ItemPool {
138141
if (lifecycle == LifecycleStep.ON_CREATE_MOUNT_CONTENT_POOL) {
139142
throw MountPhaseException(LifecycleStep.ON_CREATE_MOUNT_CONTENT_POOL)
140143
}

litho-it/src/main/java/com/facebook/litho/widget/MountSpecLifecycleTesterDrawableSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ object MountSpecLifecycleTesterDrawableSpec {
115115

116116
@JvmStatic
117117
@OnCreateMountContentPool
118-
fun onCreateMountContentPool(): MountItemsPool.ItemPool = TrackingMountContentPool(1)
118+
fun onCreateMountContentPool(poolSize: Int): MountItemsPool.ItemPool = TrackingMountContentPool(1)
119119

120120
@JvmStatic
121121
@UiThread

litho-it/src/main/java/com/facebook/litho/widget/MountSpecLifecycleTesterSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ object MountSpecLifecycleTesterSpec {
122122

123123
@JvmStatic
124124
@OnCreateMountContentPool
125-
fun onCreateMountContentPool(): MountItemsPool.ItemPool = TrackingMountContentPool(1)
125+
fun onCreateMountContentPool(poolSize: Int): MountItemsPool.ItemPool = TrackingMountContentPool(1)
126126

127127
@JvmStatic
128128
@UiThread

litho-it/src/main/java/com/facebook/litho/widget/MountSpecPureRenderLifecycleTesterSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ object MountSpecPureRenderLifecycleTesterSpec {
120120

121121
@JvmStatic
122122
@OnCreateMountContentPool
123-
fun onCreateMountContentPool(): MountItemsPool.ItemPool = TrackingMountContentPool(1)
123+
fun onCreateMountContentPool(poolSize: Int): MountItemsPool.ItemPool = TrackingMountContentPool(1)
124124

125125
@JvmStatic
126126
@UiThread

litho-it/src/main/java/com/facebook/litho/widget/PreallocatedMountSpecLifecycleTesterSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ object PreallocatedMountSpecLifecycleTesterSpec {
8585
@JvmStatic
8686
@UiThread
8787
@OnCreateMountContentPool
88-
fun onCreateMountContentPool(): MountItemsPool.ItemPool = TrackingMountContentPool(1)
88+
fun onCreateMountContentPool(poolSize: Int): MountItemsPool.ItemPool = TrackingMountContentPool(1)
8989

9090
@JvmStatic
9191
@UiThread

litho-it/src/test/resources/processor/TestMount.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,9 @@ public boolean implementsShouldUpdate() {
477477
}
478478

479479
@Override
480-
public MountItemsPool.ItemPool onCreateMountContentPool() {
480+
public MountItemsPool.ItemPool onCreateMountContentPool(int size) {
481481
MountItemsPool.ItemPool _result;
482-
_result = (MountItemsPool.ItemPool) TestMountSpec.onCreateMountContentPool();
482+
_result = (MountItemsPool.ItemPool) TestMountSpec.onCreateMountContentPool(size);
483483
return _result;
484484
}
485485

litho-it/src/test/resources/processor/TestMountSpec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ static boolean shouldUpdate(@Prop Diff<Integer> prop1) {
216216
}
217217

218218
@OnCreateMountContentPool
219-
static MountItemsPool.ItemPool onCreateMountContentPool() {
220-
return new MountItemsPool.DefaultItemPool(TestMountSpec.class, 3);
219+
static MountItemsPool.ItemPool onCreateMountContentPool(int size) {
220+
return new MountItemsPool.DefaultItemPool(TestMountSpec.class, size);
221221
}
222222

223223
@OnCalculateCachedValue(name = "cached")

litho-processor/src/main/java/com/facebook/litho/specmodels/model/DelegateMethodDescriptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public final class DelegateMethodDescriptions {
258258
.accessType(Modifier.PUBLIC)
259259
.returnType(ClassNames.MOUNT_CONTENT_POOL)
260260
.name("onCreateMountContentPool")
261-
.definedParameterTypes(ImmutableList.<TypeName>of())
261+
.definedParameterTypes(ImmutableList.<TypeName>of(TypeName.INT))
262262
.optionalParameterTypes(ImmutableList.of(INJECT_PROP))
263263
.build();
264264

litho-rendercore/src/main/java/com/facebook/rendercore/ContentAllocator.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ interface ContentAllocator<Content : Any> {
5050
val poolingPolicy: PoolingPolicy
5151
get() = PoolingPolicy.Default
5252

53-
fun createRecyclingPool(poolSizeOverride: Int = UNSET_POOL_SIZE): ItemPool? {
54-
return if (poolSizeOverride > UNSET_POOL_SIZE) {
55-
DefaultItemPool(javaClass, poolSizeOverride)
56-
} else {
57-
onCreateMountContentPool()
58-
}
59-
}
60-
6153
/**
6254
* This API informs the framework to fill the content pool for this Mountable ahead of time. The
6355
* default value is `false`, i.e. content is not pre-allocated. Pre-allocation of the content can
@@ -71,7 +63,10 @@ interface ContentAllocator<Content : Any> {
7163
fun poolSize(): Int = DEFAULT_MAX_PREALLOCATION
7264

7365
/** Creates the content pool the framework should use for this [ContentAllocator] */
74-
fun onCreateMountContentPool(): ItemPool = DefaultItemPool(javaClass, poolSize())
66+
fun onCreateMountContentPool(poolSizeOverride: Int = UNSET_POOL_SIZE): ItemPool? {
67+
val size = if (poolSizeOverride > UNSET_POOL_SIZE) poolSizeOverride else poolSize()
68+
return DefaultItemPool(javaClass, size)
69+
}
7570

7671
companion object {
7772
/** Default size of the content pool. */

litho-rendercore/src/main/java/com/facebook/rendercore/MountItemsPool.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ object MountItemsPool {
203203

204204
if (pool == null) {
205205
pool =
206-
allocator.createRecyclingPool(poolSize)
206+
allocator.onCreateMountContentPool(poolSize)
207207
?: DefaultItemPool(poolableContentType, poolSize)
208208
}
209209

0 commit comments

Comments
 (0)