diff --git a/litho-core/src/main/java/com/facebook/litho/ComponentsLogger.java b/litho-core/src/main/java/com/facebook/litho/ComponentsLogger.java index 927db2f251e..f50ad04222e 100644 --- a/litho-core/src/main/java/com/facebook/litho/ComponentsLogger.java +++ b/litho-core/src/main/java/com/facebook/litho/ComponentsLogger.java @@ -33,7 +33,11 @@ enum LogLevel { FATAL } - /** Create a new performance event with the given event id and start counting the time. */ + /** + * Create a new performance event with the given event id and start counting the time. If the + * logger doesn't care about this event id, it may return null. + */ + @Nullable PerfEvent newPerformanceEvent(ComponentContext c, @FrameworkLogEvents.LogEventId int eventId); /** Write a {@link PerfEvent} to storage. This also marks the end of the event. */ diff --git a/litho-core/src/main/java/com/facebook/litho/LogTreePopulator.java b/litho-core/src/main/java/com/facebook/litho/LogTreePopulator.java index b3964124f2f..c5b41154ec9 100644 --- a/litho-core/src/main/java/com/facebook/litho/LogTreePopulator.java +++ b/litho-core/src/main/java/com/facebook/litho/LogTreePopulator.java @@ -37,7 +37,11 @@ private LogTreePopulator() {} @Nullable @CheckReturnValue public static PerfEvent populatePerfEventFromLogger( - ComponentContext c, ComponentsLogger logger, PerfEvent perfEvent) { + ComponentContext c, ComponentsLogger logger, @Nullable PerfEvent perfEvent) { + if (perfEvent == null) { + return null; + } + final String logTag = c.getLogTag(); if (logTag == null) { logger.cancelPerfEvent(perfEvent); diff --git a/litho-it/src/test/java/com/facebook/litho/LayoutStateCalculateTest.java b/litho-it/src/test/java/com/facebook/litho/LayoutStateCalculateTest.java index 3a27bcfbf2c..44906bc3a2b 100644 --- a/litho-it/src/test/java/com/facebook/litho/LayoutStateCalculateTest.java +++ b/litho-it/src/test/java/com/facebook/litho/LayoutStateCalculateTest.java @@ -59,6 +59,7 @@ import android.graphics.Rect; import android.util.SparseArray; import android.view.accessibility.AccessibilityManager; +import androidx.annotation.Nullable; import com.facebook.litho.config.ComponentsConfiguration; import com.facebook.litho.testing.TestComponent; import com.facebook.litho.testing.TestDrawableComponent; @@ -67,6 +68,7 @@ import com.facebook.litho.testing.TestSizeDependentComponent; import com.facebook.litho.testing.TestViewComponent; import com.facebook.litho.testing.Whitebox; +import com.facebook.litho.testing.logging.TestComponentsLogger; import com.facebook.litho.testing.testrunner.ComponentsTestRunner; import com.facebook.litho.testing.util.InlineLayoutSpec; import com.facebook.litho.widget.Text; @@ -2765,6 +2767,36 @@ protected Component onCreateLayout(final ComponentContext c) { ComponentsConfiguration.createPhantomLayoutOutputsForTransitions = false; } + @Test + public void testComponentsLoggerCanReturnNullPerfEventsDuringLayout() { + final Component component = + new InlineLayoutSpec() { + @Override + protected Component onCreateLayout(final ComponentContext c) { + return create(c).child(TestDrawableComponent.create(c)).wrapInView().build(); + } + }; + + final ComponentsLogger logger = + new TestComponentsLogger() { + @Override + public @Nullable PerfEvent newPerformanceEvent(ComponentContext c, int eventId) { + return null; + } + }; + + final LayoutState layoutState = + LayoutState.calculate( + new ComponentContext(application, "test", logger), + component, + -1, + makeSizeSpec(100, EXACTLY), + makeSizeSpec(100, EXACTLY), + LayoutState.CalculateLayoutSource.TEST); + + assertThat(layoutState.getMountableOutputCount()).isEqualTo(2); + } + private void enableAccessibility() { final ShadowAccessibilityManager manager = Shadows.shadowOf( (AccessibilityManager) diff --git a/litho-it/src/test/java/com/facebook/litho/LogTreePopulatorTest.java b/litho-it/src/test/java/com/facebook/litho/LogTreePopulatorTest.java index 3c7bdadc0bc..541133d014a 100644 --- a/litho-it/src/test/java/com/facebook/litho/LogTreePopulatorTest.java +++ b/litho-it/src/test/java/com/facebook/litho/LogTreePopulatorTest.java @@ -150,5 +150,19 @@ public Map getExtraAnnotations(TreeProps treeProps) { assertThat(res).isEqualTo("my_key:1337:other_key:value:"); } + @Test + public void testSkipNullPerfEvent() { + final ComponentsLogger logger = + new TestComponentsLogger() { + @Nullable + @Override + public Map getExtraAnnotations(TreeProps treeProps) { + return null; + } + }; + + assertThat(LogTreePopulator.populatePerfEventFromLogger(mContext, logger, null)).isNull(); + } + private static class MyKey {} }