Skip to content

Commit

Permalink
Optimize usage of ComponentsSystrace by assuming it is configured onc…
Browse files Browse the repository at this point in the history
…e at startup

Summary: We have seen from deep tracing that time spent in ComponentsSystrace.isTracing is non-neglibile, even while not tracing. Part of this is just due to the extra overhead of method calls, especially in interpretted mode. The optimization here is just to use the instance directly everywhere.

Reviewed By: muraziz

Differential Revision: D15963578

fbshipit-source-id: 2312c0a446aadfe5d5606045eb6a2a2e9c0e07b4
  • Loading branch information
astreet authored and facebook-github-bot committed Jun 24, 2019
1 parent 358db22 commit 457a20f
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions litho-core/src/main/java/com/facebook/litho/ComponentsSystrace.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ComponentsSystrace {
/** Convenience implementation of ArgsBuilder to use when we aren't tracing. */
public static final ArgsBuilder NO_OP_ARGS_BUILDER = new NoOpArgsBuilder();

private static volatile Systrace sInstance = null;
private static Systrace sInstance = new DefaultComponentsSystrace();

public interface Systrace {
void beginSection(String name);
Expand Down Expand Up @@ -93,6 +93,7 @@ public interface ArgsBuilder {

private ComponentsSystrace() {}

/** This should be called exactly once at app startup, before any Litho work happens. */
public static void provide(Systrace instance) {
sInstance = instance;
}
Expand All @@ -102,7 +103,7 @@ public static void provide(Systrace instance) {
* followed by a corresponding call to {@link #endSection()} on the same thread.
*/
public static void beginSection(String name) {
getInstance().beginSection(name);
sInstance.beginSection(name);
}

/**
Expand All @@ -115,7 +116,7 @@ public static void beginSection(String name) {
* behavior and in {@link DefaultComponentsSystrace} it is a no-op.
*/
public static void beginSectionAsync(String name) {
getInstance().beginSectionAsync(name);
sInstance.beginSectionAsync(name);
}

/**
Expand All @@ -128,11 +129,11 @@ public static void beginSectionAsync(String name) {
* behavior and in {@link DefaultComponentsSystrace} it is a no-op.
*/
public static void beginSectionAsync(String name, int cookie) {
getInstance().beginSectionAsync(name, cookie);
sInstance.beginSectionAsync(name, cookie);
}

public static ArgsBuilder beginSectionWithArgs(String name) {
return getInstance().beginSectionWithArgs(name);
return sInstance.beginSectionWithArgs(name);
}

/**
Expand All @@ -142,7 +143,7 @@ public static ArgsBuilder beginSectionWithArgs(String name) {
* beginSection / endSection pairs are properly nested and called from the same thread.
*/
public static void endSection() {
getInstance().endSection();
sInstance.endSection();
}

/**
Expand All @@ -153,7 +154,7 @@ public static void endSection() {
* behavior and in {@link DefaultComponentsSystrace} it is a no-op.
*/
public static void endSectionAsync(String name) {
getInstance().endSectionAsync(name);
sInstance.endSectionAsync(name);
}

/**
Expand All @@ -165,22 +166,11 @@ public static void endSectionAsync(String name) {
* behavior and in {@link DefaultComponentsSystrace} it is a no-op.
*/
public static void endSectionAsync(String name, int cookie) {
getInstance().endSectionAsync(name, cookie);
sInstance.endSectionAsync(name, cookie);
}

public static boolean isTracing() {
return getInstance().isTracing();
}

private static Systrace getInstance() {
if (sInstance == null) {
synchronized (ComponentsSystrace.class) {
if (sInstance == null) {
sInstance = new DefaultComponentsSystrace();
}
}
}
return sInstance;
return sInstance.isTracing();
}

private static final class NoOpArgsBuilder implements ArgsBuilder {
Expand Down

0 comments on commit 457a20f

Please sign in to comment.