From 686137f65967b526486eff38c694d5be282596cf Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Fri, 20 Nov 2026 17:43:04 -0800 Subject: [PATCH] Keep Marker, Polyline and Polygon state in one snapshot record Each mutable field was its own mutableStateOf behind a NotifyingState: four objects per field, 7 to 13 fields per state. On the JVM a marker was 1,340 B, a polyline 942 B and a polygon 1,152 B before any points. CircleState already kept its fields in one record. The three now do the same: a private Values data class held in a single mutableStateOf (marker 293 B, polyline 261 B, polygon 269 B). A write compares only the field it writes, as before, and is serialized so two threads writing different fields both land. The marker's iconRevision moves into the record too. Street Trees (144,183 markers) on a Pixel 5a: app heap 334 MB -> 146 MB. --- api/android-sdk-core.api | 80 ++++++++++++ .../com/mapconductor/core/marker/Marker.kt | 114 ++++++++++++------ .../core/marker/MarkerIconRevision.kt | 10 +- .../com/mapconductor/core/polygon/Polygon.kt | 68 +++++++++-- .../mapconductor/core/polyline/Polyline.kt | 58 +++++++-- 5 files changed, 270 insertions(+), 60 deletions(-) diff --git a/api/android-sdk-core.api b/api/android-sdk-core.api index 11d117c..a6d3822 100644 --- a/api/android-sdk-core.api +++ b/api/android-sdk-core.api @@ -2068,6 +2068,38 @@ public final class com.mapconductor.core.marker.MarkerState implements com.mapco public int hashCode(); public final com.mapconductor.core.marker.MarkerFingerPrint fingerPrint(); } +final class com.mapconductor.core.marker.MarkerState$Values { + public final com.mapconductor.core.features.GeoPointInterface getPosition(); + public final com.mapconductor.core.marker.MarkerIconInterface getIcon(); + public final com.mapconductor.core.marker.MarkerAnimation getAnimation(); + public final java.lang.Integer getZIndex(); + public final boolean getClickable(); + public final boolean getDraggable(); + public final kotlin.jvm.functions.Function1 getOnClick(); + public final kotlin.jvm.functions.Function1 getOnDragStart(); + public final kotlin.jvm.functions.Function1 getOnDrag(); + public final kotlin.jvm.functions.Function1 getOnDragEnd(); + public final kotlin.jvm.functions.Function1 getOnAnimateStart(); + public final kotlin.jvm.functions.Function1 getOnAnimateEnd(); + public final int getIconRevision(); + public final com.mapconductor.core.features.GeoPointInterface component1(); + public final com.mapconductor.core.marker.MarkerIconInterface component2(); + public final com.mapconductor.core.marker.MarkerAnimation component3(); + public final java.lang.Integer component4(); + public final boolean component5(); + public final boolean component6(); + public final kotlin.jvm.functions.Function1 component7(); + public final kotlin.jvm.functions.Function1 component8(); + public final kotlin.jvm.functions.Function1 component9(); + public final kotlin.jvm.functions.Function1 component10(); + public final kotlin.jvm.functions.Function1 component11(); + public final kotlin.jvm.functions.Function1 component12(); + public final int component13(); + public final com.mapconductor.core.marker.MarkerState$Values copy(com.mapconductor.core.features.GeoPointInterface, com.mapconductor.core.marker.MarkerIconInterface, com.mapconductor.core.marker.MarkerAnimation, java.lang.Integer, boolean, boolean, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, int); + public java.lang.String toString(); + public int hashCode(); + public boolean equals(java.lang.Object); +} public interface com.mapconductor.core.marker.MarkerTileRasterLayerCallback { public abstract java.lang.Object onRasterLayerUpdate(com.mapconductor.core.raster.RasterLayerState, kotlin.coroutines.Continuation); } @@ -2498,6 +2530,32 @@ public final class com.mapconductor.core.polygon.PolygonState implements com.map public com.mapconductor.core.polygon.PolygonState(java.util.List, java.util.List, java.lang.String, long, float, long, boolean, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, kotlin.jvm.internal.DefaultConstructorMarker); public com.mapconductor.core.polygon.PolygonState(java.util.List, java.util.List, java.lang.String, long, float, long, boolean, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1, kotlin.jvm.internal.DefaultConstructorMarker); } +final class com.mapconductor.core.polygon.PolygonState$Values { + public final java.util.List getPoints(); + public final java.util.List> getHoles(); + public final long getStrokeColor-0d7_KjU(); + public final float getStrokeWidth-D9Ej5fM(); + public final long getFillColor-0d7_KjU(); + public final boolean getGeodesic(); + public final boolean getClickable(); + public final int getZIndex(); + public final java.io.Serializable getExtra(); + public final kotlin.jvm.functions.Function1 getOnClick(); + public final java.util.List component1(); + public final java.util.List> component2(); + public final long component3-0d7_KjU(); + public final float component4-D9Ej5fM(); + public final long component5-0d7_KjU(); + public final boolean component6(); + public final boolean component7(); + public final int component8(); + public final java.io.Serializable component9(); + public final kotlin.jvm.functions.Function1 component10(); + public final com.mapconductor.core.polygon.PolygonState$Values copy-cWpAVu8(java.util.List, java.util.List>, long, float, long, boolean, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1); + public java.lang.String toString(); + public int hashCode(); + public boolean equals(java.lang.Object); +} public final class com.mapconductor.core.polygon.PolygonTileGeometryKt { public static final double POLYGON_CELL; public static final int POLYGON_MAX_CELLS; @@ -2831,6 +2889,28 @@ public final class com.mapconductor.core.polyline.PolylineState implements com.m public com.mapconductor.core.polyline.PolylineState(java.util.List, java.lang.String, long, float, boolean, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1, kotlin.jvm.internal.DefaultConstructorMarker); public com.mapconductor.core.polyline.PolylineState(java.util.List, java.lang.String, long, float, boolean, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1, kotlin.jvm.internal.DefaultConstructorMarker); } +final class com.mapconductor.core.polyline.PolylineState$Values { + public final java.util.List getPoints(); + public final long getStrokeColor-0d7_KjU(); + public final float getStrokeWidth-D9Ej5fM(); + public final boolean getGeodesic(); + public final boolean getClickable(); + public final int getZIndex(); + public final java.io.Serializable getExtra(); + public final kotlin.jvm.functions.Function1 getOnClick(); + public final java.util.List component1(); + public final long component2-0d7_KjU(); + public final float component3-D9Ej5fM(); + public final boolean component4(); + public final boolean component5(); + public final int component6(); + public final java.io.Serializable component7(); + public final kotlin.jvm.functions.Function1 component8(); + public final com.mapconductor.core.polyline.PolylineState$Values copy-3MZ6nm0(java.util.List, long, float, boolean, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1); + public java.lang.String toString(); + public int hashCode(); + public boolean equals(java.lang.Object); +} public final class com.mapconductor.core.polyline.PolylineTileSet { public static final com.mapconductor.core.polyline.PolylineTileSet$Companion Companion; public com.mapconductor.core.polyline.PolylineTileSet(com.mapconductor.core.tileserver.LocalTileServer, kotlinx.coroutines.CoroutineScope); diff --git a/src/main/java/com/mapconductor/core/marker/Marker.kt b/src/main/java/com/mapconductor/core/marker/Marker.kt index 69e6370..b99d52e 100644 --- a/src/main/java/com/mapconductor/core/marker/Marker.kt +++ b/src/main/java/com/mapconductor/core/marker/Marker.kt @@ -1,7 +1,7 @@ package com.mapconductor.core.marker import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size @@ -55,26 +55,73 @@ class MarkerState( */ override val mutations = StateMutationSignal() - private val currentIcon = mutations.notifying(icon) + // One Compose snapshot record per marker rather than one per attribute, as + // in CircleState: a mutableStateOf is three objects, and there were thirteen. + private data class Values( + val position: GeoPointInterface, + val icon: MarkerIconInterface?, + val animation: MarkerAnimation?, + val zIndex: Int?, + val clickable: Boolean, + val draggable: Boolean, + val onClick: OnMarkerEventHandler?, + val onDragStart: OnMarkerEventHandler?, + val onDrag: OnMarkerEventHandler?, + val onDragEnd: OnMarkerEventHandler?, + val onAnimateStart: OnMarkerEventHandler?, + val onAnimateEnd: OnMarkerEventHandler?, + /** [iconRevision] の中身。URL の画像が届くたびに増える。 */ + val iconRevision: Int = 0, + ) + + private var values by mutableStateOf( + Values( + position, icon, animation, zIndex, clickable, draggable, + onClick, onDragStart, onDrag, onDragEnd, onAnimateStart, onAnimateEnd, + ), + ) + + // A write compares only the field written, as the per-attribute states did, + // and is serialized so writes to two fields from two threads both land (an + // animation moving the marker while the app swaps its icon, say). + private inline fun write(next: (Values) -> Values?) { + synchronized(this) { values = next(values) ?: return } + mutations.notifyMutated() + } - /** [iconRevision] の中身。URL の画像が届くたびに増える。 */ - internal val iconRevisionState = mutableIntStateOf(0) + internal val iconRevisionValue: Int get() = values.iconRevision var icon: MarkerIconInterface? - get() = currentIcon.value + get() = values.icon set(value) { - val changed = currentIcon.value != value - currentIcon.value = value + val changed = values.icon != value + write { if (it.icon == value) null else it.copy(icon = value) } if (changed) redrawWhenImageLoaded(value) } - var clickable by mutations.notifying(clickable) - var draggable by mutations.notifying(draggable) - var onClick by mutations.notifying(onClick) - var onDragStart by mutations.notifying(onDragStart) - var onDrag by mutations.notifying(onDrag) - var onDragEnd by mutations.notifying(onDragEnd) - var onAnimateStart by mutations.notifying(onAnimateStart) - var onAnimateEnd by mutations.notifying(onAnimateEnd) + var clickable: Boolean + get() = values.clickable + set(value) = write { if (it.clickable == value) null else it.copy(clickable = value) } + var draggable: Boolean + get() = values.draggable + set(value) = write { if (it.draggable == value) null else it.copy(draggable = value) } + var onClick: OnMarkerEventHandler? + get() = values.onClick + set(value) = write { if (it.onClick == value) null else it.copy(onClick = value) } + var onDragStart: OnMarkerEventHandler? + get() = values.onDragStart + set(value) = write { if (it.onDragStart == value) null else it.copy(onDragStart = value) } + var onDrag: OnMarkerEventHandler? + get() = values.onDrag + set(value) = write { if (it.onDrag == value) null else it.copy(onDrag = value) } + var onDragEnd: OnMarkerEventHandler? + get() = values.onDragEnd + set(value) = write { if (it.onDragEnd == value) null else it.copy(onDragEnd = value) } + var onAnimateStart: OnMarkerEventHandler? + get() = values.onAnimateStart + set(value) = write { if (it.onAnimateStart == value) null else it.copy(onAnimateStart = value) } + var onAnimateEnd: OnMarkerEventHandler? + get() = values.onAnimateEnd + set(value) = write { if (it.onAnimateEnd == value) null else it.copy(onAnimateEnd = value) } /** * Called once each time this marker goes from not being on the map to being on it: @@ -92,9 +139,9 @@ class MarkerState( * it redraws nothing. If the same state is shown on two maps, each map calls it. */ var onAdded: OnMarkerEventHandler? = onAdded - var zIndex by mutations.notifying(zIndex) - - private var internalAnimation by mutations.notifying(animation) + var zIndex: Int? + get() = values.zIndex + set(value) = write { if (it.zIndex == value) null else it.copy(zIndex = value) } init { redrawWhenImageLoaded(icon) @@ -110,10 +157,7 @@ class MarkerState( private fun redrawWhenImageLoaded(icon: MarkerIconInterface?) { val image = icon as? ImageIcon ?: return image.whenImageLoaded { - if (currentIcon.value === image) { - iconRevisionState.intValue++ - mutations.notifyMutated() - } + write { if (it.icon !== image) null else it.copy(iconRevision = it.iconRevision + 1) } } } @@ -152,19 +196,14 @@ class MarkerState( ) fun animate(animation: MarkerAnimation?) { - internalAnimation = animation + write { if (it.animation == animation) null else it.copy(animation = animation) } } - fun getAnimation(): MarkerAnimation? = internalAnimation + fun getAnimation(): MarkerAnimation? = values.animation - private val currentPosition = mutations.notifying(position) var position: GeoPointInterface - get() { - return currentPosition.value - } - set(value) { - currentPosition.value = value - } + get() = values.position + set(value) = write { if (it.position == value) null else it.copy(position = value) } /** * Copies this state, carrying every property over unless overridden. @@ -254,9 +293,10 @@ class MarkerState( var result = extra?.hashCode() ?: 0 result = 31 * result + clickable.hashCode() result = 31 * result + draggable.hashCode() - result = 31 * result + currentPosition.value.latitude.hashCode() - result = 31 * result + currentPosition.value.longitude.hashCode() - result = 31 * result + currentPosition.value.altitude.hashCode() + val position = values.position + result = 31 * result + position.latitude.hashCode() + result = 31 * result + position.longitude.hashCode() + result = 31 * result + position.altitude.hashCode() result = 31 * result + (icon?.hashCode() ?: 0) result = 31 * result + zIndex.hashCode() return result @@ -268,9 +308,9 @@ class MarkerState( icon.hashCode(), clickable.hashCode(), draggable.hashCode(), - currentPosition.value.latitude.hashCode(), - currentPosition.value.longitude.hashCode(), - internalAnimation?.hashCode() ?: 1, + values.position.latitude.hashCode(), + values.position.longitude.hashCode(), + values.animation?.hashCode() ?: 1, zIndex.hashCode(), ) } diff --git a/src/main/java/com/mapconductor/core/marker/MarkerIconRevision.kt b/src/main/java/com/mapconductor/core/marker/MarkerIconRevision.kt index dab03a5..ce6e240 100644 --- a/src/main/java/com/mapconductor/core/marker/MarkerIconRevision.kt +++ b/src/main/java/com/mapconductor/core/marker/MarkerIconRevision.kt @@ -6,12 +6,12 @@ import com.mapconductor.core.InternalMapConductorApi * [MarkerState.icon] の URL の画像が届くたびに増える数。Compose から読むと、そのとき描き直される。 * * URL の画像が届いても、アイコンは同じインスタンスのまま `hashCode` だけが変わるので、 - * `marker.icon` を読んでいるだけのコンポジションは走り直さない。マーカーをコンポーズの - * オーバーレイとして描くプロバイダは、これも読んで `remember` のキーにする。 + * `remember(marker.icon)` は作り直されない。マーカーをコンポーズのオーバーレイとして描く + * プロバイダは、これも `remember` のキーにする。 * - * [MarkerState.icon] の読み出し自体は Compose の状態を増やさない。タイルの描画スレッドや - * コントローラが 14 万回読むので、そこへ余計な読み取りを足さないため。 + * マーカーの値は 1 つの Compose 状態にまとめてあるので、これを読んでも状態の読み取りは + * 増えない(タイルの描画スレッドやコントローラが 14 万回読むので、そこは増やさない)。 */ @InternalMapConductorApi val MarkerState.iconRevision: Int - get() = iconRevisionState.intValue + get() = iconRevisionValue diff --git a/src/main/java/com/mapconductor/core/polygon/Polygon.kt b/src/main/java/com/mapconductor/core/polygon/Polygon.kt index 2c2cd7c..c51720d 100644 --- a/src/main/java/com/mapconductor/core/polygon/Polygon.kt +++ b/src/main/java/com/mapconductor/core/polygon/Polygon.kt @@ -1,6 +1,7 @@ package com.mapconductor.core.polygon import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.Dp @@ -46,10 +47,45 @@ class PolygonState( */ override val mutations = StateMutationSignal() - var strokeColor by mutations.notifying(strokeColor) - var strokeWidth by mutations.notifying(strokeWidth) - var fillColor by mutations.notifying(fillColor) - var geodesic by mutations.notifying(geodesic) + // One Compose snapshot record per polygon rather than one per attribute, as + // in CircleState: a mutableStateOf is three objects, and there were nine. + private data class Values( + val points: List, + val holes: List>, + val strokeColor: Color, + val strokeWidth: Dp, + val fillColor: Color, + val geodesic: Boolean, + val clickable: Boolean, + val zIndex: Int, + val extra: Serializable?, + val onClick: OnPolygonEventHandler?, + ) + + private var values by mutableStateOf( + Values(points, holes, strokeColor, strokeWidth, fillColor, geodesic, clickable, zIndex, extra, onClick), + ) + + // A write compares only the field written, as the per-attribute states did + // (not the whole record, which would compare the points to change a colour), + // and is serialized so writes to two fields from two threads both land. + private inline fun write(next: (Values) -> Values?) { + synchronized(this) { values = next(values) ?: return } + mutations.notifyMutated() + } + + var strokeColor: Color + get() = values.strokeColor + set(value) = write { if (it.strokeColor == value) null else it.copy(strokeColor = value) } + var strokeWidth: Dp + get() = values.strokeWidth + set(value) = write { if (it.strokeWidth == value) null else it.copy(strokeWidth = value) } + var fillColor: Color + get() = values.fillColor + set(value) = write { if (it.fillColor == value) null else it.copy(fillColor = value) } + var geodesic: Boolean + get() = values.geodesic + set(value) = write { if (it.geodesic == value) null else it.copy(geodesic = value) } /** * タップを受け取るか。`false` ならこのポリゴンはタップに対して透過し、 @@ -59,8 +95,12 @@ class PolygonState( * どのプロバイダでも同じ挙動になる。描画には影響しないので [fingerPrint] には * 含めない(含めると値を変えるたびにオーバーレイが作り直される)。 */ - var clickable by mutations.notifying(clickable) - var zIndex by mutations.notifying(zIndex) + var clickable: Boolean + get() = values.clickable + set(value) = write { if (it.clickable == value) null else it.copy(clickable = value) } + var zIndex: Int + get() = values.zIndex + set(value) = write { if (it.zIndex == value) null else it.copy(zIndex = value) } /** * 外周の頂点。 @@ -76,15 +116,23 @@ class PolygonState( * 内容の等しいリストの代入は無視される。その場で書き換えた同じリストを代入し直しても、 * 前の値と同じものなので何も起きない。 */ - var points by mutations.notifying>(points) + var points: List + get() = values.points + set(value) = write { if (it.points == value) null else it.copy(points = value) } /** * 穴(内周)の頂点。[points] と同じく、変えるときは新しいリストを代入する。 * 内側のリストをその場で書き換えても描き直されない。 */ - var holes by mutations.notifying>>(holes) - var extra by mutations.notifying(extra) - var onClick by mutations.notifying(onClick) + var holes: List> + get() = values.holes + set(value) = write { if (it.holes == value) null else it.copy(holes = value) } + var extra: Serializable? + get() = values.extra + set(value) = write { if (it.extra == value) null else it.copy(extra = value) } + var onClick: OnPolygonEventHandler? + get() = values.onClick + set(value) = write { if (it.onClick == value) null else it.copy(onClick = value) } /** * Called once each time this polygon goes from not being on the map to being on it: diff --git a/src/main/java/com/mapconductor/core/polyline/Polyline.kt b/src/main/java/com/mapconductor/core/polyline/Polyline.kt index 12440fd..fc2816d 100644 --- a/src/main/java/com/mapconductor/core/polyline/Polyline.kt +++ b/src/main/java/com/mapconductor/core/polyline/Polyline.kt @@ -1,6 +1,7 @@ package com.mapconductor.core.polyline import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.Dp @@ -42,9 +43,40 @@ class PolylineState( */ override val mutations = StateMutationSignal() - var strokeColor by mutations.notifying(strokeColor) - var strokeWidth by mutations.notifying(strokeWidth) - var geodesic by mutations.notifying(geodesic) + // One Compose snapshot record per polyline rather than one per attribute, as + // in CircleState: a mutableStateOf is three objects, and there were seven. + private data class Values( + val points: List, + val strokeColor: Color, + val strokeWidth: Dp, + val geodesic: Boolean, + val clickable: Boolean, + val zIndex: Int, + val extra: Serializable?, + val onClick: OnPolylineEventHandler?, + ) + + private var values by mutableStateOf( + Values(points, strokeColor, strokeWidth, geodesic, clickable, zIndex, extra, onClick), + ) + + // A write compares only the field written, as the per-attribute states did + // (not the whole record, which would compare the points to change a colour), + // and is serialized so writes to two fields from two threads both land. + private inline fun write(next: (Values) -> Values?) { + synchronized(this) { values = next(values) ?: return } + mutations.notifyMutated() + } + + var strokeColor: Color + get() = values.strokeColor + set(value) = write { if (it.strokeColor == value) null else it.copy(strokeColor = value) } + var strokeWidth: Dp + get() = values.strokeWidth + set(value) = write { if (it.strokeWidth == value) null else it.copy(strokeWidth = value) } + var geodesic: Boolean + get() = values.geodesic + set(value) = write { if (it.geodesic == value) null else it.copy(geodesic = value) } /** * タップを受け取るか。`false` ならこのポリラインはタップに対して透過し、 @@ -54,8 +86,12 @@ class PolylineState( * どのプロバイダでも同じ挙動になる。描画には影響しないので [fingerPrint] には * 含めない(含めると値を変えるたびにオーバーレイが作り直される)。 */ - var clickable by mutations.notifying(clickable) - var zIndex by mutations.notifying(zIndex) + var clickable: Boolean + get() = values.clickable + set(value) = write { if (it.clickable == value) null else it.copy(clickable = value) } + var zIndex: Int + get() = values.zIndex + set(value) = write { if (it.zIndex == value) null else it.copy(zIndex = value) } /** * 線の頂点。 @@ -71,9 +107,15 @@ class PolylineState( * 内容の等しいリストの代入は無視される。その場で書き換えた同じリストを代入し直しても、 * 前の値と同じものなので何も起きない。 */ - var points by mutations.notifying>(points) - var extra by mutations.notifying(extra) - var onClick by mutations.notifying(onClick) + var points: List + get() = values.points + set(value) = write { if (it.points == value) null else it.copy(points = value) } + var extra: Serializable? + get() = values.extra + set(value) = write { if (it.extra == value) null else it.copy(extra = value) } + var onClick: OnPolylineEventHandler? + get() = values.onClick + set(value) = write { if (it.onClick == value) null else it.copy(onClick = value) } /** * Called once each time this polyline goes from not being on the map to being on it: