Skip to content

Commit 391b56a

Browse files
perf(mobile): skip obsolete queued full token decodes (#25320)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 61c7601 commit 391b56a

7 files changed

Lines changed: 570 additions & 4 deletions

File tree

‎apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
4747
private var contentResetKey = ""
4848

4949
@Volatile private var rowsDecodeGeneration = 0
50-
private var tokensDecodeGeneration = 0
50+
51+
@Volatile private var tokensDecodeGeneration = 0
5152
private val payloadDecodeExecutor = Executors.newSingleThreadExecutor()
5253
private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
5354
private val minimumFlingVelocity = ViewConfiguration.get(context).scaledMinimumFlingVelocity
@@ -203,6 +204,11 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
203204
tokensDecodeGeneration += 1
204205
val generation = tokensDecodeGeneration
205206
payloadDecodeExecutor.execute {
207+
if (generation != tokensDecodeGeneration ||
208+
Thread.currentThread().isInterrupted
209+
) {
210+
return@execute
211+
}
206212
val decodedTokens = parseTokensObject(value)
207213
post {
208214
if (generation != tokensDecodeGeneration) return@post

‎apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffView.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ public final class T3ReviewDiffView: ExpoView, UIScrollViewDelegate {
342342
private var rowsDecodeGeneration = 0
343343
private var rowsDecodeCancellation: ReviewDiffRowPreparation?
344344
private var tokensDecodeGeneration = 0
345+
private var tokensDecodeCancellation: ReviewDiffRowPreparation?
345346

346347
let onDebug = EventDispatcher()
347348
let onVisibleFileChange = EventDispatcher()
@@ -390,6 +391,7 @@ public final class T3ReviewDiffView: ExpoView, UIScrollViewDelegate {
390391

391392
deinit {
392393
rowsDecodeCancellation?.cancel()
394+
tokensDecodeCancellation?.cancel()
393395
}
394396

395397
public override func layoutSubviews() {
@@ -485,10 +487,15 @@ public final class T3ReviewDiffView: ExpoView, UIScrollViewDelegate {
485487
}
486488

487489
func setTokensJson(_ tokensJson: String) {
490+
tokensDecodeCancellation?.cancel()
491+
let cancellation = ReviewDiffRowPreparation()
492+
tokensDecodeCancellation = cancellation
488493
tokensDecodeGeneration += 1
489494
let generation = tokensDecodeGeneration
490495

491496
payloadDecodeQueue.async { [weak self] in
497+
// Entered decoding may finish; only skip full payloads already obsolete in the queue.
498+
guard !cancellation.isCancelled, self != nil else { return }
492499
guard let data = tokensJson.data(using: .utf8) else {
493500
return
494501
}
@@ -615,6 +622,7 @@ public final class T3ReviewDiffView: ExpoView, UIScrollViewDelegate {
615622
rowsDecodeCancellation = nil
616623
rowsDecodeGeneration += 1
617624
tokensDecodeGeneration += 1
625+
tokensDecodeCancellation?.cancel()
618626
contentView.tokensByRowId = [:]
619627
rows = []
620628
contentView.rows = []

‎apps/mobile/modules/t3-review-diff/tests/android/build.gradle‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,39 @@ tasks.register('extractAndroidTestJars') {
4444
}
4545

4646
def moduleRoot = file('../..')
47+
// Count decoder entry without replacing the production executor, parser, or lifecycle.
48+
def instrumentedView = layout.buildDirectory.file('token-fixture/T3ReviewDiffView.kt')
49+
tasks.register('instrumentTokenFixture') {
50+
def original = file("${moduleRoot}/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt")
51+
inputs.file original
52+
outputs.file instrumentedView
53+
doLast {
54+
def source = original.text
55+
def decode = ' val decodedTokens = parseTokensObject(value)'
56+
def publish = ' canvasView.tokensByRowId = decodedTokens'
57+
assert source.count(decode) == 1
58+
assert source.count(publish) == 1
59+
source = source.replace(decode, ' TokenDecodeProbe.enter()\n' + decode)
60+
source = source.replace(publish, publish + '\n TokenDecodeProbe.publish()')
61+
def destination = instrumentedView.get().asFile
62+
destination.parentFile.mkdirs()
63+
destination.text = source
64+
}
65+
}
66+
4767
def sources = files(
68+
instrumentedView,
4869
fileTree('stubs') { include '**/*.kt' },
4970
fileTree('src/test/kotlin') { include '**/*.kt' },
5071
fileTree("${moduleRoot}/android/src/test/java") { include '**/*.kt' },
5172
fileTree("${moduleRoot}/android/src/main/java/expo/modules/t3reviewdiff") {
5273
include '**/*.kt'
53-
exclude '**/T3ReviewDiffModule.kt'
74+
exclude '**/T3ReviewDiffModule.kt', '**/T3ReviewDiffView.kt'
5475
},
5576
)
5677

5778
tasks.register('compileRegression', JavaExec) {
58-
dependsOn 'extractAndroidTestJars'
79+
dependsOn 'extractAndroidTestJars', 'instrumentTokenFixture'
5980
classpath = configurations.compiler
6081
mainClass = 'org.jetbrains.kotlin.cli.jvm.K2JVMCompiler'
6182
inputs.files sources, configurations.testCompileClasspath
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
package expo.modules.t3reviewdiff
2+
3+
import android.app.Activity
4+
import android.graphics.Color
5+
import android.os.Looper
6+
import expo.modules.kotlin.AppContext
7+
import java.util.concurrent.CountDownLatch
8+
import java.util.concurrent.ExecutorService
9+
import java.util.concurrent.Semaphore
10+
import java.util.concurrent.atomic.AtomicInteger
11+
import java.util.concurrent.atomic.AtomicReference
12+
import org.junit.Assert.assertEquals
13+
import org.junit.Assert.assertTrue
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import org.robolectric.Robolectric
17+
import org.robolectric.RobolectricTestRunner
18+
import org.robolectric.Shadows.shadowOf
19+
import org.robolectric.annotation.Config
20+
import org.robolectric.annotation.LooperMode
21+
22+
internal object TokenDecodeProbe {
23+
val entries = AtomicInteger()
24+
val publications = AtomicInteger()
25+
26+
@Volatile var onEntry: ((Int) -> Unit)? = null
27+
28+
fun reset() {
29+
entries.set(0)
30+
publications.set(0)
31+
onEntry = null
32+
}
33+
34+
fun enter() {
35+
val entry = entries.incrementAndGet()
36+
onEntry?.invoke(entry)
37+
}
38+
fun publish() {
39+
publications.incrementAndGet()
40+
}
41+
}
42+
43+
@RunWith(RobolectricTestRunner::class)
44+
@Config(sdk = [35], manifest = Config.NONE)
45+
@LooperMode(LooperMode.Mode.PAUSED)
46+
class ReviewDiffTokenDecodeTest {
47+
@Test
48+
fun queuedFullReplacementSkipsOnlyObsoleteWorkAndPreservesPatchOrdering() {
49+
for (case in queuedCases()) {
50+
fixture { view, executor ->
51+
TokenDecodeProbe.reset()
52+
view.setContentResetKey("content")
53+
view.setTokensResetKey("tokens")
54+
view.setTokensJson(payload("seed"))
55+
drain(executor)
56+
TokenDecodeProbe.reset()
57+
val entered = CountDownLatch(1)
58+
val release = Semaphore(0)
59+
executor.execute {
60+
entered.countDown()
61+
release.acquire()
62+
}
63+
entered.await()
64+
val acknowledgements = mutableListOf<Boolean>()
65+
try {
66+
case.enqueue(view) { id, chunk ->
67+
view.setTokensPatchJson(patchPayload(id, chunk)) { acknowledgements.add(it) }
68+
}
69+
} finally {
70+
release.release()
71+
}
72+
drain(executor)
73+
assertEquals("${case.name} decoder entry", case.entries, TokenDecodeProbe.entries.get())
74+
// Android's malformed full payload publishes its existing empty-map fallback.
75+
assertEquals(
76+
"${case.name} retained full publication",
77+
case.entries,
78+
TokenDecodeProbe.publications.get()
79+
)
80+
assertEquals("${case.name} token output", expected(case.ids), tokens(view))
81+
assertEquals("${case.name} patch acknowledgements", case.acks, acknowledgements)
82+
println(
83+
"Passed tokens: ${case.name}, decoder entries=${case.entries}, " +
84+
"retained full publications=${case.entries}"
85+
)
86+
}
87+
}
88+
}
89+
90+
private data class QueuedCase(
91+
val name: String,
92+
val ids: List<String> = listOf("b"),
93+
val entries: Int = 1,
94+
val acks: List<Boolean> = emptyList(),
95+
val enqueue: (T3ReviewDiffView, (String, Int) -> Unit) -> Unit
96+
)
97+
98+
private fun queuedCases() = listOf(
99+
QueuedCase("A B C burst", ids = listOf("c")) { view, _ ->
100+
listOf("a", "b", "c").forEach { view.setTokensJson(payload(it)) }
101+
},
102+
QueuedCase("content reset", ids = emptyList(), entries = 0) { view, _ ->
103+
view.setTokensJson(payload("a"))
104+
view.setContentResetKey("new")
105+
},
106+
QueuedCase("reset then B") { view, _ ->
107+
view.setTokensJson(payload("a"))
108+
view.setContentResetKey("new")
109+
view.setTokensJson(payload("b"))
110+
},
111+
QueuedCase("same content key") { view, _ ->
112+
view.setTokensJson(payload("b"))
113+
view.setContentResetKey("content")
114+
},
115+
QueuedCase("token reset preserves full") { view, _ ->
116+
view.setTokensJson(payload("b"))
117+
view.setTokensResetKey("new")
118+
},
119+
QueuedCase("same token key") { view, _ ->
120+
view.setTokensJson(payload("b"))
121+
view.setTokensResetKey("tokens")
122+
},
123+
QueuedCase("current full") { view, _ -> view.setTokensJson(payload("b")) },
124+
QueuedCase("current malformed", ids = emptyList()) { view, _ ->
125+
view.setTokensJson("invalid JSON")
126+
},
127+
QueuedCase("obsolete malformed") { view, _ ->
128+
view.setTokensJson("invalid JSON")
129+
view.setTokensJson(payload("b"))
130+
},
131+
QueuedCase("full patch full", acks = listOf(true)) { view, patch ->
132+
view.setTokensJson(payload("a"))
133+
patch("patch", 0)
134+
view.setTokensJson(payload("b"))
135+
},
136+
QueuedCase(
137+
"full then patches",
138+
ids = listOf("b", "one", "two"),
139+
acks = listOf(true, true)
140+
) { view, patch ->
141+
view.setTokensJson(payload("b"))
142+
patch("one", 0)
143+
patch("two", 1)
144+
},
145+
QueuedCase("patches then full", acks = listOf(true, true)) { view, patch ->
146+
patch("one", 0)
147+
patch("two", 1)
148+
view.setTokensJson(payload("b"))
149+
},
150+
QueuedCase(
151+
"additive patches",
152+
ids = listOf("seed", "one", "two"),
153+
entries = 0,
154+
acks = listOf(true, true)
155+
) { _, patch ->
156+
patch("one", 0)
157+
patch("two", 1)
158+
},
159+
QueuedCase("reset rejects old patch", acks = listOf(false)) { view, patch ->
160+
patch("old", 0)
161+
view.setContentResetKey("new")
162+
view.setTokensJson(payload("b"))
163+
},
164+
)
165+
166+
@Test
167+
fun enteredFullSuccessAndErrorFinishButOnlyTheReplacementPublishes() {
168+
for (invalid in listOf(false, true)) {
169+
fixture { view, executor ->
170+
TokenDecodeProbe.reset()
171+
val entered = CountDownLatch(1)
172+
val release = Semaphore(0)
173+
TokenDecodeProbe.onEntry = { entry ->
174+
if (entry == 1) {
175+
entered.countDown()
176+
release.acquire()
177+
}
178+
}
179+
view.setTokensJson(if (invalid) "invalid JSON" else payload("a"))
180+
entered.await()
181+
try {
182+
view.setContentResetKey("new")
183+
view.setTokensJson(payload("b"))
184+
} finally {
185+
release.release()
186+
}
187+
drain(executor)
188+
assertEquals(2, TokenDecodeProbe.entries.get())
189+
assertEquals(1, TokenDecodeProbe.publications.get())
190+
assertEquals(expected(listOf("b")), tokens(view))
191+
println(
192+
"Passed entered tokens invalid=$invalid, decoder entries=2, retained full publications=1"
193+
)
194+
}
195+
}
196+
}
197+
198+
@Test
199+
fun cleanupDiscardsQueuedFullWorkOnTheProductionExecutor() {
200+
fixture { view, executor ->
201+
TokenDecodeProbe.reset()
202+
val entered = CountDownLatch(1)
203+
val worker = AtomicReference<Thread>()
204+
val release = Semaphore(0)
205+
executor.execute {
206+
worker.set(Thread.currentThread())
207+
entered.countDown()
208+
try {
209+
release.acquire()
210+
} catch (
211+
_: InterruptedException
212+
) {
213+
Thread.currentThread().interrupt()
214+
}
215+
}
216+
entered.await()
217+
view.setTokensJson(payload("discarded"))
218+
view.cleanup()
219+
worker.get().join()
220+
shadowOf(Looper.getMainLooper()).idle()
221+
assertEquals(0, TokenDecodeProbe.entries.get())
222+
assertEquals(0, TokenDecodeProbe.publications.get())
223+
assertTrue(tokens(view).isEmpty())
224+
println("Passed cleanup before entry, decoder entries=0, retained full publications=0")
225+
}
226+
}
227+
228+
@Test
229+
fun cleanupAfterEntryKeepsTheExistingCurrentFullPublication() {
230+
fixture { view, _ ->
231+
TokenDecodeProbe.reset()
232+
val entered = CountDownLatch(1)
233+
val release = Semaphore(0)
234+
val worker = AtomicReference<Thread>()
235+
TokenDecodeProbe.onEntry = {
236+
worker.set(Thread.currentThread())
237+
entered.countDown()
238+
// shutdownNow interrupts this worker; entered decoding must still finish.
239+
release.acquireUninterruptibly()
240+
}
241+
view.setTokensJson(payload("entered"))
242+
entered.await()
243+
try {
244+
view.cleanup()
245+
} finally {
246+
release.release()
247+
}
248+
worker.get().join()
249+
shadowOf(Looper.getMainLooper()).idle()
250+
assertEquals(1, TokenDecodeProbe.entries.get())
251+
assertEquals(1, TokenDecodeProbe.publications.get())
252+
assertEquals(expected(listOf("entered")), tokens(view))
253+
println("Passed cleanup after entry, decoder entries=1, retained full publications=1")
254+
}
255+
}
256+
257+
private fun fixture(run: (T3ReviewDiffView, ExecutorService) -> Unit) {
258+
val controller = Robolectric.buildActivity(Activity::class.java).setup()
259+
val view = T3ReviewDiffView(controller.get(), AppContext())
260+
controller.get().setContentView(view)
261+
val executor = field(view, "payloadDecodeExecutor") as ExecutorService
262+
try {
263+
run(view, executor)
264+
} finally {
265+
view.cleanup()
266+
TokenDecodeProbe.reset()
267+
controller.pause().stop().destroy()
268+
}
269+
}
270+
271+
private fun drain(executor: ExecutorService) {
272+
executor.submit {}.get()
273+
shadowOf(Looper.getMainLooper()).idle()
274+
}
275+
276+
private fun field(target: Any, name: String): Any = target.javaClass.getDeclaredField(name)
277+
.apply { isAccessible = true }.get(target)
278+
279+
private fun tokens(view: T3ReviewDiffView) = field(
280+
view.getChildAt(0),
281+
"tokensByRowId"
282+
) as Map<*, *>
283+
284+
private fun expected(ids: List<String>) = ids.associateWith {
285+
listOf(DiffToken("漢字 👨‍👩‍👧‍👦 é", Color.RED, 3), DiffToken("tail", null, 0))
286+
}
287+
288+
private fun payload(id: String) =
289+
"""{"$id":[{"content":"漢字 👨‍👩‍👧‍👦 é","color":"#ff0000","fontStyle":3},{"content":"tail","fontStyle":0}]}"""
290+
291+
private fun patchPayload(id: String, chunk: Int) =
292+
"""{"resetKey":"tokens","chunkIndex":$chunk,"tokensByRowId":${payload(id)}}"""
293+
}

0 commit comments

Comments
 (0)