Skip to content

Commit

Permalink
[Compose] Minor Perf fix on JsonConstraintSet
Browse files Browse the repository at this point in the history
Properly tag when a JSONConstraintSet may have changed to update
ConstraintLayout's core State.

Previously, it would always cause the State to re-initialize which
implies parsing the string into a Json object and then iterating over
it. Causing a significant overhead when ConstraintLayout is part of an
animation.
  • Loading branch information
oscar-ad committed May 19, 2022
1 parent 47292e3 commit b26e4ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ internal abstract class EditableJSONLayout(@Language("json5") content: String) :
}

fun setCurrentContent(content: String) {
currentContent = content
onNewContent(content)
}

fun getCurrentContent() : String{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package androidx.constraintlayout.compose

import androidx.compose.ui.layout.Measurable
import androidx.constraintlayout.core.parser.CLKey
import androidx.constraintlayout.core.parser.CLParser
import androidx.constraintlayout.core.parser.CLParsingException
Expand All @@ -31,6 +32,7 @@ internal class JSONConstraintSet(
) : EditableJSONLayout(content), DerivedConstraintSet {
private val overridedVariables = HashMap<String, Float>()
private val overrideVariables = overrideVariables
private var _isDirty = true

init {
initialization()
Expand All @@ -43,6 +45,10 @@ internal class JSONConstraintSet(
return false
}

override fun isDirty(measurables: List<Measurable>): Boolean {
return _isDirty
}

// Only called by MotionLayout in MotionMeasurer
override fun applyTo(transition: Transition, type: Int) {
val layoutVariables = LayoutVariables()
Expand All @@ -66,11 +72,18 @@ internal class JSONConstraintSet(
// TODO: Need to better handle half parsed JSON and/or incorrect states.
try {
parseJSON(getCurrentContent(), state, layoutVariables)
_isDirty = false
} catch (e : Exception) {
// nothing (content might be invalid, sent by live edit)
_isDirty = true
}
}

override fun onNewContent(content: String) {
super.onNewContent(content)
_isDirty = true
}

override fun override(name: String, value: Float): ConstraintSet {
overridedVariables[name] = value
return this
Expand Down

0 comments on commit b26e4ed

Please sign in to comment.