Understanding the performance benefits of using ConstraintLayout


Figure 1. Example of how the measure section traverses a view tree

Each section inside the drawing procedure calls for a top-down traversal of the view
tree. Therefore, the extra perspectives you embed inside every different (or nest) into the
view hierarchy, the extra time and computation energy it takes for the software to
draw the perspectives. By maintaining a flat hierarchy on your Android app layouts, you’ll
create a quick and responsive person interface in your app.

The expense of a standard format hierarchy

With that rationalization in thoughts, let’s create a standard format hierarchy that
makes use of LinearLayout and RelativeLayout items.

Figure 2. Example format

Let’s say we need to construct a format like the symbol above. If you construct it with
conventional layouts, the XML record incorporates a component hierarchy very similar to the
following (for this situation, we have now ignored the attributes):

<RelativeLayout>
  <SymbolView />
  <SymbolView />
  <RelativeLayout>
    <Textual contentView />
    <LinearLayout>
      <Textual contentView />
      <RelativeLayout>
        <EditText />
      </RelativeLayout>
    </LinearLayout>
    <LinearLayout>
      <Textual contentView />
      <RelativeLayout>
        <EditText />
      </RelativeLayout>
    </LinearLayout>
    <Textual contentView />
  </RelativeLayout>
  <LinearLayout >
    <Button />
    <Button />
  </LinearLayout>
</RelativeLayout>

Although there is most often room for growth on this kind of view hierarchy,
you can virtually indisputably nonetheless want to create a hierarchy with some nested perspectives.

As mentioned prior to, nested hierarchies can adversely impact performance. Let’s
check out how the nested perspectives in reality impact the UI performance using
Android Studio’s Systrace
device. We referred to as the measure and format stages for every ViewStaff
(ConstraintLayout and RelativeLayout) programmatically
and prompted Systrace whilst the measure and format calls are executing. The
following command generates an summary record that incorporates key occasions, equivalent to
dear measure/format passes, that happen all through a 20-second period:

python $ANDROID_HOME/platform-tools/systrace/systrace.py --time=20 -o ~/hint.html gfx view res

For extra information about how you’ll use Systrace, see the Analyzing UI
Performance with Systrace
information.

Systrace robotically highlights the (a large number of) performance issues of this
format, in addition to ideas for solving them. By clicking the “Alerts” tab,
you’ll in finding that drawing this view hierarchy calls for 80 dear passes
via the measure and format stages!

Triggering that many dear measure and format stages is some distance from excellent; such
a great amount of drawing task may lead to skipped frames that customers
realize. We can conclude that the format has deficient performance because of the nested
hierarchy in addition to the function of RelativeLayout, which
measures every of its youngsters two times.

Figure three. Looking at the indicators from Systrace for the
format variant that makes use of RelativeLayout

You can take a look at the complete code on how we carried out those measurements in our GitHub
repository
.

The benefits of a ConstraintLayout object

If you create the identical format using ConstraintLayout, the XML record
incorporates a component hierarchy very similar to the following (attributes once more
ignored):

<android.beef up.constraint.ConstraintLayout>
  <SymbolView />
  <SymbolView />
  <Textual contentView />
  <EditText />
  <Textual contentView />
  <Textual contentView />
  <EditText />
  <Button />
  <Button />
  <Textual contentView />
</android.beef up.constraint.ConstraintLayout>

As this situation displays, the format now has a fully flat hierarchy. This is
as a result of ConstraintLayout lets you construct advanced layouts
with no need to nest View and ViewStaff components.

For instance, let’s take a look at the Textual contentView and EditText
in the heart of the format:

When using a RelativeLayout, you wish to have to create a brand new
ViewStaff to align the EditText vertically with the
Textual contentView:

<LinearLayout
    android:identity="@+id/camera_area"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@id/title" >

    <Textual contentView
        android:textual content="@string/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:identity="@+id/cameraLabel"
        android:labelFor="@+id/cameraType"
        android:layout_marginStart="16dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:identity="@+id/cameraType"
            android:ems="10"
            android:inputType="textPersonName"
            android:textual content="@string/camera_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginTop="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp" />
    </RelativeLayout>
</LinearLayout>

By using ConstraintLayout as an alternative, you’ll reach the identical impact
simply by including a constraint from the baseline of the Textual contentView to
the baseline of the EditText with out growing some other
ViewStaff:

Figure four. Constraint between EditText and Textual contentView

<Textual contentView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_creator="1"
      app:layout_constraintBaseline_creator="1"
      app:layout_constraintLeft_toLeftOf="@+id/activity_main_done"
      app:layout_constraintBaseline_toBaselineOf="@+id/cameraType" />

When operating the Systrace device for the model of our format that makes use of
ConstraintLayout, you notice some distance fewer dear measure/format passes
all through the identical 20-second period. This growth in performance is sensible,
now that we are maintaining the view hierarchy flat!

Figure five. Looking at the indicators from Systrace for the
format variant that makes use of ConstraintLayout

On a comparable word, we constructed the ConstraintLayout variant of our
format using simply the layout
editor
as an alternative of enhancing the XML through hand. To reach the identical visible impact
using RelativeLayout, we more than likely would have had to edit the XML
through hand.

Measuring the performance distinction

We analyzed how lengthy each measure and format go took for 2 kind of layouts,
ConstraintLayout and RelativeLayout, through using
OnFrameMetricsAvailableListener,
which was once offered in Android 7.zero (API degree 24). This magnificence lets you
acquire frame-by-frame timing details about your app’s UI
rendering.

By calling the following code, you’ll get started recording per-frame UI movements:

window.uploadOnFrameMetricsAvailableListener(
        frameMetricsAvailableListener, frameMetricsHandler);

After timing data turns into to be had, the app triggers the
frameMetricsAvailableListener() callback. We are all for the
measure/format performance, so we name FrameMetrics.LAYOUT_MEASURE_DURATION
when retrieving the precise body length.

Window.OnFrameMetricsAvailableListener {
        _, frameMetrics, _ ->
        val frameMetricsCopy = FrameMetrics(frameMetrics);
        // Layout measure length in nanoseconds
        val formatMeasureDurationNs = 
                frameMetricsCopy.getMetric(FrameMetrics.LAYOUT_MEASURE_DURATION);

To be informed extra about the different varieties of length data that
FrameMetrics can obtain, see the FrameMetrics
API reference.

Measurement effects: ConstraintLayout is quicker

Our performance comparability displays that ConstraintLayout plays
about 40% higher in the measure/format section than RelativeLayout:

Figure 6. Measure / Layout (unit: ms, moderate of 100
frames)

As those effects display, ConstraintLayout is perhaps extra
performant than conventional layouts. Moreover, ConstraintLayout has
different options that can help you construct advanced and performant layouts, as mentioned
in the benefits of a ConstraintLayout
object
segment. For main points, see the Build
a Responsive UI with ConstraintLayout
information. We counsel that you just use
ConstraintLayout when designing your app’s layouts. In virtually all
instances whilst you would have in the past want a deeply-nested format,
ConstraintLayout will have to be your go-to format for optimum
performance and straightforwardness of use.

Appendix: Measurement surroundings

All the measurements above have been carried out in the following surroundings.

Device Nexus 5X
Android Version eight.zero
ConstraintLayout model 1.zero.2

What’s subsequent

Check out the developer
guide
, the API
reference documentation
, and the article
on Medium
to completely perceive what ConstraintLayout may give
for you. And as soon as once more, thanks to all who submitted comments and problems over
the months since our alpha free up of ConstraintLayout. We’re in point of fact
thankful that we have been ready to free up the production-ready 1.0
version
of ConstraintLayout previous this 12 months.
As we proceed to toughen ConstraintLayout, please proceed to ship
us comments using the Android factor tracker.

Since saying ConstraintLayout
at Google I/O ultimate 12 months, we have now persisted to toughen the format’s steadiness and
format editor beef up. We’ve additionally added new options particular to
ConstraintLayout that can help you construct more than a few kind of layouts, such
as introducing
chains
and setting
size as a ratio
. In addition to those options, there’s a notable
performance receive advantages through using ConstraintLayout. In this put up, we will
stroll via how you’ll get pleasure from those performance enhancements.

How Android attracts perspectives?

To higher perceive the performance of ConstraintLayout, let’s
take a step again and spot how Android attracts perspectives.

When a person brings an Android view into focal point, the Android framework directs the
view to attract itself. This drawing procedure incorporates three stages:

  1. Measure

    The machine completes a top-down traversal of the view tree to resolve how
    massive every ViewStaff and View part will have to be. When a
    ViewStaff is measured, it additionally measures its youngsters.

  2. Layout

    Another top-down traversal happens, with every ViewStaff figuring out
    the positions of its youngsters using the sizes decided in the measure section.

  3. Draw

    The machine plays but some other top-down traversal. For every object in the view
    tree, a Canvas object is created to ship a listing of drawing instructions
    to the GPU. These instructions come with the ViewStaff and
    View items’ sizes and positions, which the machine decided
    all through the earlier 2 stages.

Figure 1. Example of how the measure section traverses a view tree

Each section inside the drawing procedure calls for a top-down traversal of the view
tree. Therefore, the extra perspectives you embed inside every different (or nest) into the
view hierarchy, the extra time and computation energy it takes for the software to
draw the perspectives. By maintaining a flat hierarchy on your Android app layouts, you’ll
create a quick and responsive person interface in your app.

The expense of a standard format hierarchy

With that rationalization in thoughts, let’s create a standard format hierarchy that
makes use of LinearLayout and RelativeLayout items.

Figure 2. Example format

Let’s say we need to construct a format like the symbol above. If you construct it with
conventional layouts, the XML record incorporates a component hierarchy very similar to the
following (for this situation, we have now ignored the attributes):

<RelativeLayout>
  <SymbolView />
  <SymbolView />
  <RelativeLayout>
    <Textual contentView />
    <LinearLayout>
      <Textual contentView />
      <RelativeLayout>
        <EditText />
      </RelativeLayout>
    </LinearLayout>
    <LinearLayout>
      <Textual contentView />
      <RelativeLayout>
        <EditText />
      </RelativeLayout>
    </LinearLayout>
    <Textual contentView />
  </RelativeLayout>
  <LinearLayout >
    <Button />
    <Button />
  </LinearLayout>
</RelativeLayout>

Although there is most often room for growth on this kind of view hierarchy,
you can virtually indisputably nonetheless want to create a hierarchy with some nested perspectives.

As mentioned prior to, nested hierarchies can adversely impact performance. Let’s
check out how the nested perspectives in reality impact the UI performance using
Android Studio’s Systrace
device. We referred to as the measure and format stages for every ViewStaff
(ConstraintLayout and RelativeLayout) programmatically
and prompted Systrace whilst the measure and format calls are executing. The
following command generates an summary record that incorporates key occasions, equivalent to
dear measure/format passes, that happen all through a 20-second period:

python $ANDROID_HOME/platform-tools/systrace/systrace.py --time=20 -o ~/hint.html gfx view res

For extra information about how you’ll use Systrace, see the Analyzing UI
Performance with Systrace
information.

Systrace robotically highlights the (a large number of) performance issues of this
format, in addition to ideas for solving them. By clicking the “Alerts” tab,
you’ll in finding that drawing this view hierarchy calls for 80 dear passes
via the measure and format stages!

Triggering that many dear measure and format stages is some distance from excellent; such
a great amount of drawing task may lead to skipped frames that customers
realize. We can conclude that the format has deficient performance because of the nested
hierarchy in addition to the function of RelativeLayout, which
measures every of its youngsters two times.

Figure three. Looking at the indicators from Systrace for the
format variant that makes use of RelativeLayout

You can take a look at the complete code on how we carried out those measurements in our GitHub
repository
.

The benefits of a ConstraintLayout object

If you create the identical format using ConstraintLayout, the XML record
incorporates a component hierarchy very similar to the following (attributes once more
ignored):

<android.beef up.constraint.ConstraintLayout>
  <SymbolView />
  <SymbolView />
  <Textual contentView />
  <EditText />
  <Textual contentView />
  <Textual contentView />
  <EditText />
  <Button />
  <Button />
  <Textual contentView />
</android.beef up.constraint.ConstraintLayout>

As this situation displays, the format now has a fully flat hierarchy. This is
as a result of ConstraintLayout lets you construct advanced layouts
with no need to nest View and ViewStaff components.

For instance, let’s take a look at the Textual contentView and EditText
in the heart of the format:

When using a RelativeLayout, you wish to have to create a brand new
ViewStaff to align the EditText vertically with the
Textual contentView:

<LinearLayout
    android:identity="@+id/camera_area"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@id/title" >

    <Textual contentView
        android:textual content="@string/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:identity="@+id/cameraLabel"
        android:labelFor="@+id/cameraType"
        android:layout_marginStart="16dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:identity="@+id/cameraType"
            android:ems="10"
            android:inputType="textPersonName"
            android:textual content="@string/camera_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginTop="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp" />
    </RelativeLayout>
</LinearLayout>

By using ConstraintLayout as an alternative, you’ll reach the identical impact
simply by including a constraint from the baseline of the Textual contentView to
the baseline of the EditText with out growing some other
ViewStaff:

Figure four. Constraint between EditText and Textual contentView

<Textual contentView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_creator="1"
      app:layout_constraintBaseline_creator="1"
      app:layout_constraintLeft_toLeftOf="@+id/activity_main_done"
      app:layout_constraintBaseline_toBaselineOf="@+id/cameraType" />

When operating the Systrace device for the model of our format that makes use of
ConstraintLayout, you notice some distance fewer dear measure/format passes
all through the identical 20-second period. This growth in performance is sensible,
now that we are maintaining the view hierarchy flat!

Figure five. Looking at the indicators from Systrace for the
format variant that makes use of ConstraintLayout

On a comparable word, we constructed the ConstraintLayout variant of our
format using simply the layout
editor
as an alternative of enhancing the XML through hand. To reach the identical visible impact
using RelativeLayout, we more than likely would have had to edit the XML
through hand.

Measuring the performance distinction

We analyzed how lengthy each measure and format go took for 2 kind of layouts,
ConstraintLayout and RelativeLayout, through using
OnFrameMetricsAvailableListener,
which was once offered in Android 7.zero (API degree 24). This magnificence lets you
acquire frame-by-frame timing details about your app’s UI
rendering.

By calling the following code, you’ll get started recording per-frame UI movements:

window.uploadOnFrameMetricsAvailableListener(
        frameMetricsAvailableListener, frameMetricsHandler);

After timing data turns into to be had, the app triggers the
frameMetricsAvailableListener() callback. We are all for the
measure/format performance, so we name FrameMetrics.LAYOUT_MEASURE_DURATION
when retrieving the precise body length.

Window.OnFrameMetricsAvailableListener {
        _, frameMetrics, _ ->
        val frameMetricsCopy = FrameMetrics(frameMetrics);
        // Layout measure length in nanoseconds
        val formatMeasureDurationNs = 
                frameMetricsCopy.getMetric(FrameMetrics.LAYOUT_MEASURE_DURATION);

To be informed extra about the different varieties of length data that
FrameMetrics can obtain, see the FrameMetrics
API reference.

Measurement effects: ConstraintLayout is quicker

Our performance comparability displays that ConstraintLayout plays
about 40% higher in the measure/format section than RelativeLayout:

Figure 6. Measure / Layout (unit: ms, moderate of 100
frames)

As those effects display, ConstraintLayout is perhaps extra
performant than conventional layouts. Moreover, ConstraintLayout has
different options that can help you construct advanced and performant layouts, as mentioned
in the benefits of a ConstraintLayout
object
segment. For main points, see the Build
a Responsive UI with ConstraintLayout
information. We counsel that you just use
ConstraintLayout when designing your app’s layouts. In virtually all
instances whilst you would have in the past want a deeply-nested format,
ConstraintLayout will have to be your go-to format for optimum
performance and straightforwardness of use.

Appendix: Measurement surroundings

All the measurements above have been carried out in the following surroundings.

Device Nexus 5X
Android Version eight.zero
ConstraintLayout model 1.zero.2

What’s subsequent

Check out the developer
guide
, the API
reference documentation
, and the article
on Medium
to completely perceive what ConstraintLayout may give
for you. And as soon as once more, thanks to all who submitted comments and problems over
the months since our alpha free up of ConstraintLayout. We’re in point of fact
thankful that we have been ready to free up the production-ready 1.0
version
of ConstraintLayout previous this 12 months.
As we proceed to toughen ConstraintLayout, please proceed to ship
us comments using the Android factor tracker.



Source link

Reply