4

When I realize ConstraintLayout has:

  • better layout drag-drop

  • better view relative set-up with better naming "top-toBottomOf"

  • better layout construction with ratios and percentage-guidelines

  • and much more cant state here or I just didn't know

that I was always wanted to using it constantly because it is so comfortable.  

Just like the title, I curious about did ConstraintLayout will give big performance impact when using it constantly?

Statement your opinion and evidence will be appreciated.

Editing are welcome if you have good edition/grammar, and down-voting please leave your conclusion below comment. Thanks Dennis for grammar correction.

nyconing
  • 1,092
  • 1
  • 10
  • 33
  • 2
    I really, really suggest you stop using drag and drop. There's a reason why nobody uses it- its a slower, less accurate, and harder to maintain way of writing your layouts. There's a reason why every iOS engineer I know wishes they had Android layouts- its that they wish they didn't have to drag and drop. It's the same reason serious web devs don't use DreamWeaver anymore. – Gabe Sechan Oct 06 '17 at 04:05

2 Answers2

7

Are Constraint Layout expensive compared to others?

It Depends upon your usage. A ConstraintLayout is like the RelativeLayouton Steroids.

If you want a simple 2-3 ChildViews layout, then using ConstraintLayout won't be so efficient. Go for Linear Layout.

"The main purpose of the ConstraintLayout is to fix problems with the RelativeLayout, and it does it so well. You can do so many things that were impossible with a RelativeLayout. And you can simplify your layout like you never could before. Additionally, it fixes long-standing performance issues of the RelativeLayout. The double taxation during the measure/layout phase. So we get better performance, more versatility and much simpler layouts in one nice package."

Since performance improvements are among the main reasons to create this new layout, I did some performance tests to check if this goal is met.

I compared a layout used within a RecyclerView. Nothing too fancy, just some nested LinearLayout and RelativeLayout containers. I moved this layout manually over to ConstraintLayout and did some rather sloppy performance tests.

Comparing those two on an emulator as well as on a Nexus 5 yielded overall a performance penalty when using the new ConstraintLayout.

Performing worst is the measurement. The onMeasure() method takes roughly ten times as long as the one of the LinearLayout I used for comparison. And it uses up the bulk of the time with taking about 60 times as long as onLayout(). Thus the 30 percent hit of the onLayout() method of ConstraintLayout compared to the one of the LinearLayout in question is nearly irrelevant. Finally, layout inflation also took longer with ConstraintLayout than with LinearLayout.

The ConstraintLayout does lots of calculations to find out where and how to display each of its children. Internally (at least) three rather lengthy classes are working together to get the results: LinearSystem, ConstraintWidget, and ConstraintWidgetContainer. To fully understand the performance behavior I would have to dig into the depth of these classes (and for this, I prefer the commented classes when their sources are released by Google). But just by having a cursory look at the decompiled code it looks like those classes have to do a lot.

Some annoyances

Not all is working perfectly at the moment, though. Right now these things bother me the most:

  • There’s a noticeable lag between blueprint changes and design preview changes
  • The preview is not always correct
  • The blueprint view doesn’t heed text layout constraints
  • Constraints do not always work as expected
  • Undo works very unreliably

The editor updos stuff or changes stuff in unexpected ways

This post supports ConstraintLayout.

Personally, I only use ConstraintLayout, when I want to Align more than 3 views. It's easy to use but at the same time, annoying in the beginning.

tl:dr enter image description here

Daksh Gargas
  • 3,498
  • 2
  • 23
  • 37
  • 2
    It all depends on what you're comparing it to, and what you measure. If you're comparing it to LinearLayout, its still much more expensive. If you're comparing it in memory usage, its more expensive than RelativeLayout. I also wouldn't put much faith in random blog posts to be accurate about.. well anything. Especially that blog post- its all strawmen comparing it to badly implemented designs. – Gabe Sechan Oct 06 '17 at 03:50
  • @GabeSechan I recommend you post it (more detailed) as an answer to support users, that will come here by googling. – Northern Poet Oct 06 '17 at 03:52
  • If you are so concerned with memory usage, kindly let us know how much memory will it affect. I didn't read that random blog. I've myself used it and on the basis of that, I posted this answer. But I'll surely edit this answer. – Daksh Gargas Oct 06 '17 at 03:53
  • How about now @Miller – Daksh Gargas Oct 06 '17 at 03:57
  • @Dennis A little confused by your post. You say it's not expensive compared to others, then a long post saying its less performant. Then link to a post telling you to always use it and its more performant. – Gabe Sechan Oct 06 '17 at 04:06
  • Oops, I forgot to edit that part. Kindly have a look. Thanks! – Daksh Gargas Oct 06 '17 at 04:11
  • Ok, that makes more sense now :) – Gabe Sechan Oct 06 '17 at 04:25
  • Thanks! That was so mature of you. I'll take care of my Answer Writing technique from now on. :) – Daksh Gargas Oct 06 '17 at 04:27
  • @Dennis, much better! – Northern Poet Oct 06 '17 at 06:47
0

Constraint layout improves performance , read this article on android developer blog , it clearly states , ConstraintLayout performs about 40% better in the measure/layout phase than RelativeLayout

enter image description here

Manohar
  • 22,116
  • 9
  • 108
  • 144