Skip to content

Commit

Permalink
Create LithoReducer.reduce() with immutable inputs and output
Browse files Browse the repository at this point in the history
Summary:
Following [1/2]:
> Create LithoReducer.reduce() and pull out reduce code from LithoTreeFuture.layout() into it.
LithoReducer.reduce() will take multiple params including the immediateLayoutState and return a LayoutState.

This diff makes a deeper refactor:
- Replace ```layoutState.xx``` in ```LithoReducer``` with ```reductionState.xx``` so that ```layoutState``` won't be mutated
- ```setSizeAfterMeasureAndCollectResults()``` now takes in ```reductionState``` instead of ```layoutState``` as parameter
- ```addRootHostRenderTreeNode()``` is partially migrated to using ```reductionState``` as parameter instead of ```layoutState```

Next Steps:
- Add more test against this change

Reviewed By: pentiumao

Differential Revision: D52832575

fbshipit-source-id: 556599f8e6a1af75f91e31b22f5cd6e4bda8330a
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Jan 23, 2024
1 parent 40389d1 commit 786afac
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 153 deletions.
7 changes: 6 additions & 1 deletion litho-core/src/main/java/com/facebook/litho/LayoutState.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public static boolean isFromSyncLayout(@RenderSource int source) {
private boolean mShouldProcessVisibilityOutputs;

LayoutState(
int id,
ResolveResult resolveResult,
SizeConstraints sizeConstraints,
int rootX,
Expand All @@ -170,7 +171,7 @@ public static boolean isFromSyncLayout(@RenderSource int source) {
@Nullable LayoutState current,
@Nullable LayoutCache.CachedData layoutCacheData,
ReductionState reductionState) {
mId = sIdGenerator.getAndIncrement();
mId = id;
mResolveResult = resolveResult;
mSizeConstraints = sizeConstraints;
mRootX = rootX;
Expand Down Expand Up @@ -313,6 +314,10 @@ static String layoutSourceToString(@RenderSource int source) {
}
}

static AtomicInteger getIdGenerator() {
return sIdGenerator;
}

@ThreadSafe(enableChecks = false)
void preAllocateMountContent(boolean shouldPreallocatePerMountSpec) {
if (!shouldPreallocatePerMountSpec) {
Expand Down
20 changes: 9 additions & 11 deletions litho-core/src/main/java/com/facebook/litho/LayoutTreeFuture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ class LayoutTreeFuture(
scopedComponentInfosNeedingPreviousRenderData =
resolveResult.outputs?.let {
ArrayList(it.componentsThatNeedPreviousRenderData)
})
},
c = c,
sizeConstraints = sizeConstraints,
currentLayoutState = currentLayoutState,
root = root,
offsetRootX = lsc.rootOffset.x,
offsetRootY = lsc.rootOffset.y,
)
if (root != null) {
measurePendingSubtrees(
parentContext = c,
Expand All @@ -168,16 +175,7 @@ class LayoutTreeFuture(

perfEvent?.markerPoint("start_collect_results")
val layoutState =
LithoReducer.reduce(
lsc,
c,
resolveResult,
sizeConstraints,
treeId,
currentLayoutState,
layoutCache,
root,
reductionState)
LithoReducer.reduce(lsc, resolveResult, treeId, layoutCache, reductionState)
perfEvent?.markerPoint("end_collect_results")

root?.releaseLayoutPhaseData()
Expand Down
Loading

0 comments on commit 786afac

Please sign in to comment.