From 86f2fa41d4f24c9042159fafa2f6d61c8d1c2e81 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 4 Dec 2026 10:44:12 -0800 Subject: [PATCH] Reuse BorderDrawable's paths and dash effects across draws MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `BorderDrawable` allocated a `Path` on every draw of a single-color rectangular border and a `DashPathEffect` for every edge of a dashed or dotted border on every draw. Each time it rebuilt the paths of a rounded border, it also built an outline path that nothing reads. It now keeps the single-border path, replaces the dash effect only when the border style or width changes, and no longer builds the outline path. `BorderDrawableTest` passes, including new checks that an unchanged dashed border draws with the same path and dash effect and that a width change replaces the effect. Both checks fail without this change. On an arm64 Android 16 emulator, a benchmark in RNTester's Release build recorded one border into a `RenderNode` per draw, with both versions of `BorderDrawable` in one process, alternating which ran first, over three rounds of 21 samples: | Border | Before | After | | --- | --- | --- | | 1px, one color | 595–721 ns, 164 B | 405–555 ns, 66 B | | 2px, dashed | 1,480–1,594 ns, 606 B | 425–528 ns, 66 B | | 1px, rounded, after `invalidateSelf()` | 1,448–1,685 ns, 623 B | 1,177–1,320 ns, 442 B | The bytes are Java heap allocated per draw, including the `RenderNode` recording. A rounded border drawn again without invalidation takes the same time and allocation in both versions. Signed-off-by: James Ide --- .../uimanager/drawable/BorderDrawable.kt | 89 +++++++------------ .../uimanager/drawable/BorderDrawableTest.kt | 25 ++++-- 2 files changed, 52 insertions(+), 62 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt index b962b2f5c28e..5c184c739a75 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt @@ -87,11 +87,12 @@ internal class BorderDrawable( private var pathForBorder: Path? = null private val borderPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) + private var pathEffectStyle: BorderStyle? = null + private var pathEffectWidth: Float = 0f private var needUpdatePath: Boolean = true private var pathForSingleBorder: Path? = null - private var pathForOutline: Path? = null private var centerDrawPath: Path? = null private var outerClipPathForBorderRadius: Path? = null @@ -174,7 +175,6 @@ internal class BorderDrawable( } override fun draw(canvas: Canvas) { - updatePathEffect() computedBorderColors = borderColors?.resolve(layoutDirection, context) ?: computedBorderColors if (borderRadius?.hasRoundedBorders() == true) { drawRoundedBorders(canvas) @@ -266,42 +266,42 @@ internal class BorderDrawable( val bottom = bounds.bottom borderPaint.color = multiplyColorAlpha(fastBorderColor, borderAlpha) borderPaint.style = Paint.Style.STROKE - pathForSingleBorder = Path() + val path = pathForSingleBorder ?: Path().also { pathForSingleBorder = it } if (borderLeft > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.left.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo((left + width / 2).toFloat(), top.toFloat()) - pathForSingleBorder?.lineTo((left + width / 2).toFloat(), bottom.toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo((left + width / 2).toFloat(), top.toFloat()) + path.lineTo((left + width / 2).toFloat(), bottom.toFloat()) + canvas.drawPath(path, borderPaint) } if (borderTop > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.top.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo(left.toFloat(), (top + width / 2).toFloat()) - pathForSingleBorder?.lineTo(right.toFloat(), (top + width / 2).toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo(left.toFloat(), (top + width / 2).toFloat()) + path.lineTo(right.toFloat(), (top + width / 2).toFloat()) + canvas.drawPath(path, borderPaint) } if (borderRight > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.right.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo((right - width / 2).toFloat(), top.toFloat()) - pathForSingleBorder?.lineTo((right - width / 2).toFloat(), bottom.toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo((right - width / 2).toFloat(), top.toFloat()) + path.lineTo((right - width / 2).toFloat(), bottom.toFloat()) + canvas.drawPath(path, borderPaint) } if (borderBottom > 0) { - pathForSingleBorder?.reset() + path.reset() val width = borderWidth.bottom.roundToInt() - updatePathEffect(width) + updatePathEffect(width.toFloat()) borderPaint.strokeWidth = width.toFloat() - pathForSingleBorder?.moveTo(left.toFloat(), (bottom - width / 2).toFloat()) - pathForSingleBorder?.lineTo(right.toFloat(), (bottom - width / 2).toFloat()) - pathForSingleBorder?.let { canvas.drawPath(it, borderPaint) } + path.moveTo(left.toFloat(), (bottom - width / 2).toFloat()) + path.lineTo(right.toFloat(), (bottom - width / 2).toFloat()) + canvas.drawPath(path, borderPaint) } } } else { @@ -312,6 +312,7 @@ internal class BorderDrawable( */ borderPaint.isAntiAlias = false borderPaint.style = Paint.Style.FILL + updatePathEffect(getFullBorderWidth()) val width = bounds.width() val height = bounds.height() if (borderLeft > 0) { @@ -366,6 +367,7 @@ internal class BorderDrawable( } private fun drawRoundedBorders(canvas: Canvas) { + updatePathEffect(getFullBorderWidth()) updatePath() canvas.save() @@ -570,17 +572,14 @@ internal class BorderDrawable( return if (!borderWidth.isNaN()) borderWidth else 0f } - private fun updatePathEffect() { - /** Used for rounded border and rounded background. */ - val pathEffectForBorderStyle = - this.borderStyle?.let { style -> getPathEffect(style, getFullBorderWidth()) } - borderPaint.setPathEffect(pathEffectForBorderStyle) - } - - private fun updatePathEffect(borderWidth: Int) { - val pathEffectForBorderStyle = - this.borderStyle?.let { style -> getPathEffect(style, borderWidth.toFloat()) } - borderPaint.setPathEffect(pathEffectForBorderStyle) + private fun updatePathEffect(borderWidth: Float) { + val style = borderStyle + if (style == pathEffectStyle && borderWidth == pathEffectWidth) { + return + } + pathEffectStyle = style + pathEffectWidth = borderWidth + borderPaint.setPathEffect(style?.let { getPathEffect(it, borderWidth) }) } private fun getPathEffect(style: BorderStyle, borderWidth: Float): PathEffect? { @@ -676,7 +675,6 @@ internal class BorderDrawable( // Path innerClipPathForBorderRadius = innerClipPathForBorderRadius ?: Path() outerClipPathForBorderRadius = outerClipPathForBorderRadius ?: Path() - pathForOutline = Path() // RectF innerClipTempRectForBorderRadius = innerClipTempRectForBorderRadius ?: RectF() @@ -781,27 +779,6 @@ internal class BorderDrawable( ) } - var extraRadiusForOutline = 0f - - if (this.borderWidth != null) { - extraRadiusForOutline = this.borderWidth[Spacing.ALL] / 2f - } - - pathForOutline?.addRoundRect( - RectF(bounds), - floatArrayOf( - topLeftRadius.horizontal + extraRadiusForOutline, - topLeftRadius.vertical + extraRadiusForOutline, - topRightRadius.horizontal + extraRadiusForOutline, - topRightRadius.vertical + extraRadiusForOutline, - bottomRightRadius.horizontal + extraRadiusForOutline, - bottomRightRadius.vertical + extraRadiusForOutline, - bottomLeftRadius.horizontal + extraRadiusForOutline, - bottomLeftRadius.vertical + extraRadiusForOutline, - ), - Path.Direction.CW, - ) - if (computedBorderRadius?.isUniform() != true) { centerDrawPath = centerDrawPath ?: Path() centerDrawPath?.reset() diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt index f7ae7d1e5638..35f8be51202d 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BorderDrawableTest.kt @@ -23,7 +23,6 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.mockito.kotlin.any import org.mockito.kotlin.argumentCaptor import org.mockito.kotlin.atLeastOnce import org.mockito.kotlin.mock @@ -45,7 +44,7 @@ class BorderDrawableTest { } @Test - fun removingBorderStyleClearsDashedPathEffect() { + fun dashedBorderChangesItsPathEffectOnlyWithItsStyleOrWidth() { val borderInsets = BorderInsets().apply { setBorderWidth(LogicalEdge.ALL, 8f) } val drawable = BorderDrawable( @@ -59,7 +58,18 @@ class BorderDrawableTest { drawable.setBorderWidth(Spacing.ALL, 8f) drawable.setBorderColor(LogicalEdge.ALL, Color.RED) - assertThat(drawBorderPaint(drawable).pathEffect).isInstanceOf(DashPathEffect::class.java) + val (firstPath, firstPaint) = drawBorder(drawable) + val dashedPathEffect = firstPaint.pathEffect + assertThat(dashedPathEffect).isInstanceOf(DashPathEffect::class.java) + val (secondPath, secondPaint) = drawBorder(drawable) + assertThat(secondPath).isSameAs(firstPath) + assertThat(secondPaint.pathEffect).isSameAs(dashedPathEffect) + + borderInsets.setBorderWidth(LogicalEdge.ALL, 12f) + + assertThat(drawBorderPaint(drawable).pathEffect) + .isInstanceOf(DashPathEffect::class.java) + .isNotSameAs(dashedPathEffect) drawable.borderStyle = null @@ -94,11 +104,14 @@ class BorderDrawableTest { } } - private fun drawBorderPaint(drawable: BorderDrawable): Paint { + private fun drawBorderPaint(drawable: BorderDrawable): Paint = drawBorder(drawable).second + + private fun drawBorder(drawable: BorderDrawable): Pair { val canvas = mock() drawable.draw(canvas) + val path = argumentCaptor() val paint = argumentCaptor() - verify(canvas, atLeastOnce()).drawPath(any(), paint.capture()) - return paint.lastValue + verify(canvas, atLeastOnce()).drawPath(path.capture(), paint.capture()) + return path.lastValue to paint.lastValue } }