Skip to content

ConstraintLayout Performance

John Hoford edited this page Aug 11, 2022 · 5 revisions

With any layout it is possible to have performance issues.

##Simplified Overview of ConstraintLayout

ConstraintLayout describes the position of Widgets through a series of "Constraints" connections between Anchors with a possible margin. There are 4 or 5 Anchors on each widget Top, Bottom, Left, Right and optionally BaseLine Besides Anchors there are several parameters that control the dimension of a widgets generally they can be

  • fixed - some hardcoded value
  • wrapped - The widget reports a desired size
  • min, max - limits on size
  • aspectRation - constraining the ratio of width to height

There are a few additional features chains - a specific pattern of Constraints that defines a behavior of a set of widgets in one dimensions
Guidelines - A vertical or horizontal line placed in a fixed location from the left right or percentage of the screen Barrier - A vertical or horizontal line that is set to the minimum or maximum of a collection of widgets referenced widgets

Beyond that there are a collect of "Helpers" which can effect a collection of referenced widgets.

Without optimization all Constraints, Dimensioned behavior and Helpers are turned into equations in a Numeric solver. Optimization directly solves simple cases greatly reducing the compute time.

Improving performance

ConstraintLayout has 2 major areas of performance concerns

  1. Inflation - when the xml is read
  2. Layout - when the position of the widgets is resolved.

Inflation

Generally speaking not much can be done about inflation time. More Views/Attributes means more to parse.

Layout

The way a layout is designed can greatly affect the performance.

There are some simple rules to guide performance.

* If you are laying out less than 6 widgets widgets layout performance is probably not your problem

The computation scales greater than linear so if it is small the performance is very fast.

* Avoid making the ConstraintLayout wrap content

Because of the complex relationship between the constrained widgets and the bounds of the ConstraintLayout it tends to push all equations into the solver

* Guidelines faster than chains.

If the layout is simple such as a left and right button. Connecting the buttons to a center guideline is more efficient.

Aspect ratio can be very expensive

Typically Horizontal and vertical constraints are independent (except in wrap/wrap text)

* Wrap/wrap

Views that are wrap in both horizontal and vertical can sometimes generate multiple passes

* Barriers can be expensive

* Simple constraints are best.

Simple constraints can be solved by the solver. All are fast This is roughly the order of performance

  1. Constraints to Parent
  2. Simple center constraint of a fixed with Widget
  3. Constraints to Guidelines
  4. Constraints to anchors constrained to parent
  5. Constraints to anchors a fixed with away from anchor constrained to parent

This assumes ConstraintLayout is not wrap content.