Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Modifier.Nodeに移行してパフォーマンスを比べてみた
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
usuiat
December 13, 2023
Programming
550
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Modifier.Nodeに移行してパフォーマンスを比べてみた
YUMEMI.grow Mobile #9
https://yumemi.connpass.com/event/302462/
usuiat
December 13, 2023
More Decks by usuiat
See All by usuiat
Google I/O 2025 報告LT会 Small language models with Google AI Edge
usuiat
0
500
発信から広がった地方エンジニアのキャリア
usuiat
1
70
タッチイベントの仕組みを理解してジェスチャーを使いこなそう
usuiat
1
1.7k
Google I/O 2024 報告LT会(Androidのオンデバイス生成AI)
usuiat
0
980
reifiedって何ですか?
usuiat
2
1.3k
Other Decks in Programming
See All in Programming
どこまでゆるくて許されるのか
tk3fftk
0
430
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
570
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
210
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
350
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
140
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
150
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
1
120
Webフレームワークの ベンチマークについて
yusukebe
0
200
Performance Engineering for Everyone
elenatanasoiu
0
260
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
7
2.9k
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
290
act1-costs.pdf
sumedhbala
0
170
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
1.1k
Amusing Abliteration
ianozsvald
1
220
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
From π to Pie charts
rasagy
0
230
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
200
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
67
56k
Building Applications with DynamoDB
mza
96
7.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Statistics for Hackers
jakevdp
799
230k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
320
Transcript
Modifier.Nodeに移行して パフォーマンスを比べてみた 2023.12.13 YUMEMI.grow Mobile #9 usuiat
usuiat 自己紹介 Atsushi Usui / うっすぃ~ / usuiat Androidエンジニア 静岡県在住
X/GitHub : usuiat Blog : https://engawapg.net/
usuiat 本日の発表内容 ZoomableというJetpack Composeのライブラリを Modifier.Nodeに移行したので 移行方法の概要と 移行前後のパフォーマンスの比較結果を 共有します Modifier.Node移行の具体例はまだまだ情報が少ないので、これから移行する人の参考になれば幸いです
usuiat Zoomable Jetpack Composeライブラリ 最短1行で画像などをズーム可能にする ピンチジェスチャー、ダブルタップ、タップ&ドラッグ対応 HorizontalPagerと組み合わせ可 https://github.com/usuiat/Zoomable
usuiat Modifier.Node Modifier: Composeの見た目や振る舞いを変更するためのインターフェース 状態を持つModifier: これまではModifier.composedで実装されてきた 現在はパフォーマンスに優れるModifier.Nodeへの移行が進みつつある (参考) Modifier.Nodeを使いましょう Compose
Modifiers deep dive 実践 脱Modifier.composed / Let's Modifier.Node Create custom modifiers
usuiat ZoomableのModifier.Node移行 ZoomableもModifier.composedを使っていたので、 Modifier.Nodeに移行しました。 本日は実装の概要を紹介します。 ソースコード詳しく見たい方はGitHubのPRをご覧ください。 https://github.com/usuiat/Zoomable/pull/131/files
usuiat fun Modifier.zoomable(): Modifier = composed { Modifier .onSizeChanged {
size -> zoomState.setLayoutSize(size.toSize()) } .nestedScroll(connection, dispatcher) .pointerInput(zoomState) { detectTransformGestures( onGesture = { centroid, pan, zoom -> scope.launch { zoomState.applyGesture( pan = pan, zoom = zoom, position = centroid, ) } }, ) } .graphicsLayer { scaleX = zoomState.scale scaleY = zoomState.scale translationX = zoomState.offsetX translationY = zoomState.offsetY } } Modifier.zoomable (composed版)の構造 1) pointerInput()でジェスチャーを検出し、ZoomStateを変更 2) graphicsLayer()でZoomStateの状態をレイアウトに反映 4) nestedScroll()で上位のPagerなどにスクロールイベントを伝達 3) onSizeChanged()でコンポーネントのサイズを取得 (ドラッグ可能な範囲の算出に使う) Modifier.composed()を使用
usuiat ZoomableNodeの実装方針 1) pointerInput()でジェスチャーを検出し、ZoomStateを変更 2) graphicsLayer()でZoomStateの状態をレイアウトに反映 4) nestedScroll()で上位のPagerなどにスクロールイベントを伝達 3) onSizeChanged()でコンポーネントのサイズを取得
(ドラッグ可能な範囲の算出に使う) PointerInputModifierNodeを実装 LayoutModifierNodeを実装 nestedScrollModifierNodeに委任 private class ZoomableNode(): PointerInputModifierNode, LayoutModifierNode, DelegatingNode() { ... }
usuiat private class ZoomableNode(): PointerInputModifierNode, DelegatingNode() { override fun onPointerEvent(
pointerEvent: PointerEvent, pass: PointerEventPass, bounds: IntSize ) { pointerInputNode.onPointerEvent(pointerEvent, pass, bounds) } override fun onCancelPointerInput() { pointerInputNode.onCancelPointerInput() } val pointerInputNode = delegate(SuspendingPointerInputModifierNode { detectTransformGestures( onGesture = { centroid, pan, zoom -> coroutineScope.launch { zoomState.applyGesture(pan, zoom, centroid) } }, ) }) } 1) ジェスチャーを検出し、ZoomStateを変更 PointerInputModifierNodeのonPointerEvent()と onCancelPointerInput()をoverride pointerInputNodeの処理を呼び出す SuspendingPointerInputModifierNodeにはPointerInputScopeの 処理を渡せるので、Modifier.pointerInputに書いていた処理を ほぼそのまま書ける。 pointerInputNodeはSuspendingPointerInputModifierNode 実際の処理はSuspendingPointerInputModifierNodeに 委任する PointerInputModifierNodeを実装
usuiat private class ZoomableNode(): LayoutModifierNode, DelegatingNode() { var measuredSize =
Size.Zero override fun MeasureScope.measure( measurable: Measurable, constraints: Constraints ): MeasureResult { val placeable = measurable.measure(constraints) measuredSize = IntSize(placeable.measuredWidth, placeable.measuredHeight).toSize() zoomState.setLayoutSize(measuredSize) return layout(placeable.width, placeable.height) { placeable.placeWithLayer(x = 0, y = 0) { scaleX = zoomState.scale scaleY = zoomState.scale translationX = zoomState.offsetX translationY = zoomState.offsetY } } } } 2) ZoomStateの状態をレイアウトに反映 3) コンポーネントのサイズを取得 MeasureScope.measure()をoverride placeableからコンポーネントのサイズを取得できる placeWithLayer()に、Modifier.graphicsLayer()の処理を そのまま書ける LayoutModifierNodeを実装
usuiat private class ZoomableNode(): DelegatingNode() { val connection = object
: NestedScrollConnection{} val dispatcher = NestedScrollDispatcher() init { delegate(nestedScrollModifierNode(connection, dispatcher)) } } 4) スクロールイベントを伝達 initブロックでnestedScrollModifierNodeを作成し、 delegateで委任する DelegatingNodeを継承
usuiat このほか、ModifierNodeElementの実装などが必要ですが、 今日は省略します。 ソースコード詳しく見たい方はGitHubのPRをご覧ください。 https://github.com/usuiat/Zoomable/pull/131/files
usuiat パフォーマンスは良くなったのか???
usuiat 見た目には何も変わらなかった もともとパフォーマンス悪かったわけではないので、それはそう🙄
usuiat 見た目で分からないなら計測しよう! Android Studio Flamingo以降ではProfilerでComposable関数をトレースできる😎
usuiat 再コンポーズの処理時間を計測 @Composable fun Sample() { var offset by remember
{ mutableStateOf(0.dp) } Image( painter = painter, contentDescription = null, contentScale = ContentScale.Fit, modifier = Modifier .fillMaxSize() .offset(y = offset) .zoomable( zoomState = rememberZoomState(), onTap = { offset += 10.dp } ), ) } Modifier.Nodeでは、再コンポーズ時のパフォーマンスが改善しているはず! onTapでoffsetを変更することによって再コンポーズを発生させて、 Image()の実行時間を測定する。
usuiat トレース結果はこんな感じ 指が触れ た 指が離れ た ダブルタップかどうかの 判定のための待ち時間 再コンポー ズ
このあたりにImage()がある
usuiat Modifier.composed版 10回平均 8.2ms 最大 20.78ms 最小 4.16ms materializeModifierの処理時間が Image()全体の半分程度を占めて
いる。 materializeがいかに大変かとい う話は、この動画で説明されて います。 https://www.youtube.com/watch? v=BjGX2RftXsU Image()の処理時間
usuiat Modifier.Node版 10回平均 3.72ms 最大 5.24ms 最小 1.98ms Image()の処理時間 materialize処理が
消えている
usuiat 速くなった! Modifier.composed Modifier.Node 10回平均 8.2ms 3.72ms 最大 20.78ms 5.24ms
最小 4.16ms 1.98ms 平均処理時間が約半分になった
usuiat ベータ版提供中 Zoomable v1.6.0-beta2提供中です。 不具合等あればご連絡お願いします https://github.com/usuiat/Zoomable/releases/tag/v1.6.0-beta2