ScottRobbins / DeclarativeLayout

A declarative, expressive and efficient way to lay out your views.
MIT License
5 stars 0 forks source link

Research how much of an improvement constraint diffing is #32

Closed ScottRobbins closed 5 years ago

ScottRobbins commented 5 years ago

I know apple recommends not removing all constraints, which I agree with. So I want to make sure that the amount of time the framework takes to do diffing is less than the amount of time to do another layout pass as a sanity check

ScottRobbins commented 5 years ago

With my README example (which is a bad example but readily available to start with), the framework with diffing takes ~1.2ms to do the diffing, and about 0.2ms for the layout pass.

Total about 1.4ms

ScottRobbins commented 5 years ago

I changed the implementation to just deactivate all of the old constraints and activate the new ones

Without the layout pass included, the updateConstraints method now takes ~1.8ms The layout pass itself takes ~0.2ms

total, about 2ms

This, however, still has all of the constraints wrapped, so maybe the not-diffing part will be faster if i don't do that

ScottRobbins commented 5 years ago

It's not really any different when removing the constraint wrapping. the NSLayoutconstraint.activate method must be the biggest bottleneck here that the larger number of constraints it has to deal with takes longer than doing the diffing and updating properties on constraints already there.

Which is interesting, I thought the bottleneck would be during the layout pass, but really it's whatever work autolayout has to do when you activate/deactivate constraints.

ScottRobbins commented 5 years ago

aaand I've been benchmarking on a Debug build 🤦‍♂️

ScottRobbins commented 5 years ago

Well after running in release, it doesn't help the removing all constraints and re-adding all very much. That time stays pretty consistent. Though the total time for the implemented constraint diffing drops the diffing time to ~0.75ms, so a total of 1ms.

For this scenario, doing the constraint diffing seems to be about 100% faster

ScottRobbins commented 5 years ago

Increasing this now to 100 views instead of what's just in the READMe.

Total time current implementation: 1.7, 1.8ms, something like that Total time for removing all constraints and re-adding: similar, 1.6, 1.7ms.

So they kind of evened out

Let's do it for 20 views (and so 20 constraints), much more like reality Current: 1ms Removing all/re-adding: 1.6ms


It seems that the constraint diffing performs significantly better for normal usage. As your constraint number increases the time to diff them will eventually be surpassed by deactivating/activating constraints it seems.

I'm not trying to solve for outliers, so this doesn't really bother me. However, there's definitely room for improvement with the constraint diffing