mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
perf(bridge): avoid redundant download and small job codecs
Keep download snapshots as JSON across the Android channel and decode large progress payloads off the UI isolate while retaining event order and permissive empty-result behavior. Encode bounded small native data requests inline, with the existing pre-dispatch cancellation yield and background fallback for larger graphs. Host tests verify zero encoder isolates for small requests, background decoding of large snapshots, cursor resets, stream cancellation, exact JSON and separate binary arguments. Restoring the old codec paths fails the corresponding isolate and event-loop probes.
This commit is contained in:
1 parent
d125c50fac
commit
e28fa2aeaa
7 files changed
+338
-22
No files matched your search
@@ -9,6 +9,8 @@ import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import org.json.JSONObject
|
||||
import org.json.JSONTokener
|
||||
|
||||
private val DOWNLOAD_PROGRESS_STREAM_CHANNEL =
|
||||
"com.zarz.mornye/download_progress_stream"
|
||||
@@ -54,12 +56,15 @@ internal fun MainActivity.registerProgressStreams(messenger: BinaryMessenger) {
|
||||
)
|
||||
}
|
||||
|
||||
private fun MainActivity.updateDownloadProgressSeq(progress: Any?) {
|
||||
val objectValue = progress as? Map<*, *> ?: return
|
||||
val seq = (objectValue["seq"] as? Number)?.toLong() ?: lastDownloadProgressSeq
|
||||
if (objectValue["reset"] == true || seq > lastDownloadProgressSeq) {
|
||||
lastDownloadProgressSeq = seq
|
||||
internal fun downloadProgressSequence(payload: String, previous: Long): Long {
|
||||
if (payload.isEmpty()) return previous
|
||||
val progress = try {
|
||||
JSONTokener(payload).nextValue() as? JSONObject ?: return previous
|
||||
} catch (_: Exception) {
|
||||
return previous
|
||||
}
|
||||
val seq = (progress.opt("seq") as? Number)?.toLong() ?: previous
|
||||
return if (progress.opt("reset") == true || seq > previous) seq else previous
|
||||
}
|
||||
|
||||
private fun MainActivity.startDownloadProgressStream(sink: EventChannel.EventSink) {
|
||||
@@ -73,19 +78,17 @@ private fun MainActivity.startDownloadProgressStream(sink: EventChannel.EventSin
|
||||
try {
|
||||
while (isActive && downloadProgressConnection === connection) {
|
||||
try {
|
||||
// Decode once on IO. Reset snapshots of large queues
|
||||
// previously parsed twice on the UI thread.
|
||||
val (payload, progress) = withContext(Dispatchers.IO) {
|
||||
val (payload, sequence) = withContext(Dispatchers.IO) {
|
||||
val reader = connection.get() ?: coreBackend.openDownloadProgress().also { connection.set(it) }
|
||||
ensureActive()
|
||||
val payload = reader.waitDelta(lastDownloadProgressSeq, 15_000L)
|
||||
payload to if (payload.isEmpty()) null else parseJsonPayload(payload)
|
||||
payload to downloadProgressSequence(payload, lastDownloadProgressSeq)
|
||||
}
|
||||
if (!isActive || downloadProgressConnection !== connection) break
|
||||
if (payload.isNotEmpty() && payload != lastDownloadProgressPayload) {
|
||||
updateDownloadProgressSeq(progress)
|
||||
lastDownloadProgressSeq = sequence
|
||||
lastDownloadProgressPayload = payload
|
||||
sink.success(progress)
|
||||
sink.success(payload)
|
||||
delay(250L)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
|
||||
@@ -110,14 +110,11 @@ internal fun MainActivity.handleAcknowledgeNativeDownloadWorkerItems(call: Metho
|
||||
internal suspend fun MainActivity.handleGetNativeDownloadWorkerSnapshot(call: MethodCall, result: MethodChannel.Result) {
|
||||
val sinceStateSerial =
|
||||
(call.argument<Number>("since_state_serial") ?: 0L).toLong()
|
||||
// The snapshot can be megabytes late in a large
|
||||
// batch; read and parse it off the main thread.
|
||||
// Keep large snapshots as JSON rather than re-encoding a map on Main.
|
||||
val payload = withContext(Dispatchers.IO) {
|
||||
parseJsonPayload(
|
||||
DownloadService.getNativeWorkerSnapshot(
|
||||
this@handleGetNativeDownloadWorkerSnapshot,
|
||||
sinceStateSerial
|
||||
)
|
||||
DownloadService.getNativeWorkerSnapshot(
|
||||
this@handleGetNativeDownloadWorkerSnapshot,
|
||||
sinceStateSerial
|
||||
)
|
||||
}
|
||||
result.success(payload)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.zarz.mornye
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class DownloadProgressSequenceTest {
|
||||
@Test
|
||||
fun advancesOnlyForNewEventsOrAnExplicitReset() {
|
||||
assertEquals(5L, downloadProgressSequence("{\"seq\":5}", 4L))
|
||||
assertEquals(4L, downloadProgressSequence("{\"seq\":3}", 4L))
|
||||
assertEquals(3L, downloadProgressSequence("{\"seq\":3,\"reset\":true}", 4L))
|
||||
assertEquals(4L, downloadProgressSequence("{\"seq\":3,\"reset\":\"true\"}", 4L))
|
||||
assertEquals(4L, downloadProgressSequence("{\"reset\":true}", 4L))
|
||||
assertEquals(4L, downloadProgressSequence("{\"seq\":\"5\"}", 4L))
|
||||
assertEquals(5L, downloadProgressSequence("{\"seq\":5.9}", 4L))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun missingOrInvalidObjectsKeepTheCursorWithoutChangingThePayload() {
|
||||
for (payload in listOf("", "null", "[]", "1", "{broken", "{}")) {
|
||||
assertEquals(9L, downloadProgressSequence(payload, 9L))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,7 @@ class PlatformBridge {
|
||||
final states = _nativeDataJobs.putIfAbsent(id, () => {});
|
||||
states.add(state);
|
||||
try {
|
||||
final encoded = await compute(_encodeJsonInBackground, request);
|
||||
final encoded = await _encodeNativeDataJobRequest(request);
|
||||
if (state.cancelled) {
|
||||
throw PlatformException(
|
||||
code: 'CANCELLED',
|
||||
@@ -993,10 +993,12 @@ class PlatformBridge {
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> getAllDownloadProgress() async =>
|
||||
_decodeMapResult(await _channel.invokeMethod('getAllDownloadProgress'));
|
||||
_decodeProgressMapAsync(
|
||||
await _channel.invokeMethod('getAllDownloadProgress'),
|
||||
);
|
||||
|
||||
static Stream<Map<String, dynamic>> downloadProgressStream() {
|
||||
return _channel.downloadProgressEvents().map(_decodeMapResult);
|
||||
return _channel.downloadProgressEvents().asyncMap(_decodeProgressMapAsync);
|
||||
}
|
||||
|
||||
static Future<void> exitApp() => _invokeVoid('exitApp');
|
||||
@@ -1839,7 +1841,7 @@ class PlatformBridge {
|
||||
/// only compact progress fields are returned.
|
||||
static Future<Map<String, dynamic>> getNativeDownloadWorkerSnapshot({
|
||||
int sinceStateSerial = 0,
|
||||
}) async => _decodeMapResult(
|
||||
}) async => _decodeProgressMapAsync(
|
||||
await _channel.invokeMethod('getNativeDownloadWorkerSnapshot', {
|
||||
'since_state_serial': sinceStateSerial,
|
||||
}),
|
||||
@@ -2807,6 +2809,16 @@ class PlatformBridge {
|
||||
return const <String, dynamic>{};
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> _decodeProgressMapAsync(
|
||||
dynamic result,
|
||||
) async {
|
||||
if (result is! String || result.isEmpty) return _decodeMapResult(result);
|
||||
final decoded = await _decodeJsonStringAsync(result);
|
||||
return decoded is Map
|
||||
? decoded.cast<String, dynamic>()
|
||||
: const <String, dynamic>{};
|
||||
}
|
||||
|
||||
/// Present the native iOS folder picker. The security-scoped bookmark is
|
||||
/// created inside the picker callback while its access grant is still
|
||||
/// active; creating it later from a bare path loses the grant.
|
||||
|
||||
@@ -3,6 +3,36 @@ part of '../platform_bridge.dart';
|
||||
Object? _decodeJsonInBackground(String json) => jsonDecode(json);
|
||||
String _encodeJsonInBackground(Object? value) => jsonEncode(value);
|
||||
|
||||
Future<String> _encodeNativeDataJobRequest(Map<String, dynamic> request) async {
|
||||
var nodes = 0;
|
||||
var characters = 0;
|
||||
bool small(Object? value, int depth) {
|
||||
if (++nodes > 256 || depth > 8) return false;
|
||||
if (value == null || value is bool || value is num) return true;
|
||||
if (value is String) {
|
||||
characters += value.length;
|
||||
return characters <= 16 * 1024;
|
||||
}
|
||||
if (value is List<Object?>) {
|
||||
if (value.length > 64) return false;
|
||||
return value.every((item) => small(item, depth + 1));
|
||||
}
|
||||
if (value is Map<Object?, Object?>) {
|
||||
if (value.length > 64) return false;
|
||||
return value.entries.every(
|
||||
(entry) =>
|
||||
entry.key is String &&
|
||||
small(entry.key, depth + 1) &&
|
||||
small(entry.value, depth + 1),
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (small(request, 0)) return jsonEncode(request);
|
||||
return compute(_encodeJsonInBackground, request);
|
||||
}
|
||||
|
||||
String _encodeBoundedLookupCacheInBackground(Map<String, dynamic> entries) {
|
||||
final result = StringBuffer('{');
|
||||
var bytes = 2; // Opening and closing braces.
|
||||
|
||||
@@ -1,9 +1,35 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
import 'dart:convert';
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mornye/services/platform_bridge.dart';
|
||||
|
||||
class _EncodingLocationMap extends MapBase<String, dynamic> {
|
||||
_EncodingLocationMap(this._values);
|
||||
|
||||
final Map<String, dynamic> _values;
|
||||
|
||||
@override
|
||||
Iterable<String> get keys => [..._values.keys, 'encoding_isolate'];
|
||||
|
||||
@override
|
||||
dynamic operator [](Object? key) =>
|
||||
key == 'encoding_isolate' ? Isolate.current.debugName : _values[key];
|
||||
|
||||
@override
|
||||
void operator []=(String key, dynamic value) =>
|
||||
throw UnsupportedError('read only');
|
||||
|
||||
@override
|
||||
void clear() => throw UnsupportedError('read only');
|
||||
|
||||
@override
|
||||
dynamic remove(Object? key) => throw UnsupportedError('read only');
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
const channel = MethodChannel('com.zarz.mornye/backend');
|
||||
@@ -21,6 +47,89 @@ void main() {
|
||||
|
||||
tearDown(() => messenger.setMockMethodCallHandler(channel, null));
|
||||
|
||||
for (final scenario in [
|
||||
(
|
||||
name: 'small scalars',
|
||||
values: <String, dynamic>{'path': '/song.flac'},
|
||||
background: false,
|
||||
),
|
||||
(
|
||||
name: 'bounded list',
|
||||
values: <String, dynamic>{'items': List.filled(64, 1)},
|
||||
background: false,
|
||||
),
|
||||
(
|
||||
name: 'long list',
|
||||
values: <String, dynamic>{'items': List.filled(65, 1)},
|
||||
background: true,
|
||||
),
|
||||
(
|
||||
name: 'long string',
|
||||
values: <String, dynamic>{'text': 'x' * (16 * 1024)},
|
||||
background: true,
|
||||
),
|
||||
(
|
||||
name: 'wide map',
|
||||
values: <String, dynamic>{for (var i = 0; i < 65; i++) '$i': i},
|
||||
background: true,
|
||||
),
|
||||
(
|
||||
name: 'aggregate nodes',
|
||||
values: <String, dynamic>{
|
||||
'items': List.generate(8, (_) => List.filled(64, 1)),
|
||||
},
|
||||
background: true,
|
||||
),
|
||||
(
|
||||
name: 'aggregate text',
|
||||
values: <String, dynamic>{'items': List.filled(32, 'x' * 1024)},
|
||||
background: true,
|
||||
),
|
||||
]) {
|
||||
test('encodes ${scenario.name} on the expected isolate', () async {
|
||||
late Map<String, dynamic> encoded;
|
||||
messenger.setMockMethodCallHandler(channel, (call) async {
|
||||
encoded =
|
||||
jsonDecode((call.arguments as Map)['request_json'] as String)
|
||||
as Map<String, dynamic>;
|
||||
return '{}';
|
||||
});
|
||||
await PlatformBridge.runNativeDataJob(
|
||||
_EncodingLocationMap(scenario.values),
|
||||
);
|
||||
expect(
|
||||
encoded.remove('encoding_isolate'),
|
||||
scenario.background
|
||||
? isNot(Isolate.current.debugName)
|
||||
: Isolate.current.debugName,
|
||||
);
|
||||
expect(encoded, scenario.values);
|
||||
});
|
||||
}
|
||||
|
||||
test(
|
||||
'inline encoding preserves JSON and separate binary arguments',
|
||||
() async {
|
||||
final request = <String, dynamic>{
|
||||
'operation': 'palette',
|
||||
'path': '/Music/Example ♫/song.flac',
|
||||
'options': {
|
||||
'enabled': true,
|
||||
'missing': null,
|
||||
'values': [1, 2.5, 'a\nb'],
|
||||
},
|
||||
};
|
||||
final bytes = Uint8List.fromList([0, 127, 255]);
|
||||
messenger.setMockMethodCallHandler(channel, (call) async {
|
||||
final args = call.arguments as Map;
|
||||
expect(args['request_json'], jsonEncode(request));
|
||||
expect(args['bytes'], bytes);
|
||||
return '{}';
|
||||
});
|
||||
await PlatformBridge.runNativeDataJob(request, bytes: bytes);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'cancel before encoding completes never dispatches an idle job',
|
||||
() async {
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mornye/services/platform_bridge.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
const channel = MethodChannel('com.zarz.mornye/backend');
|
||||
const events = MethodChannel('com.zarz.mornye/download_progress_stream');
|
||||
const codec = StandardMethodCodec();
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
|
||||
tearDown(() {
|
||||
messenger.setMockMethodCallHandler(channel, null);
|
||||
messenger.setMockMethodCallHandler(events, null);
|
||||
});
|
||||
|
||||
final snapshot = <String, dynamic>{
|
||||
'seq': 19,
|
||||
'reset': true,
|
||||
'run_id': 'example-run',
|
||||
'state_serial': 8,
|
||||
'compact_items': [
|
||||
for (var i = 0; i < 2000; i++)
|
||||
{
|
||||
'item_id': '$i',
|
||||
'name': 'Example song $i',
|
||||
'progress': 0.5,
|
||||
'error': null,
|
||||
},
|
||||
],
|
||||
'item_ids': [for (var i = 0; i < 2000; i++) '$i'],
|
||||
};
|
||||
final largeJson = jsonEncode(snapshot);
|
||||
|
||||
test('large snapshots yield while decoding and retain all fields', () async {
|
||||
expect(largeJson.length, greaterThanOrEqualTo(128 * 1024));
|
||||
final timer = Completer<void>();
|
||||
messenger.setMockMethodCallHandler(channel, (call) async {
|
||||
expect(call.method, 'getNativeDownloadWorkerSnapshot');
|
||||
expect(call.arguments, {'since_state_serial': 7});
|
||||
Timer.run(timer.complete);
|
||||
return largeJson;
|
||||
});
|
||||
final result = await PlatformBridge.getNativeDownloadWorkerSnapshot(
|
||||
sinceStateSerial: 7,
|
||||
);
|
||||
expect(timer.isCompleted, isTrue);
|
||||
expect(result, snapshot);
|
||||
await timer.future;
|
||||
});
|
||||
|
||||
test(
|
||||
'snapshot maps, empty payloads and decode errors keep their contract',
|
||||
() async {
|
||||
for (final value in [snapshot, '', null, '[]', 'null', '{}']) {
|
||||
messenger.setMockMethodCallHandler(channel, (_) async => value);
|
||||
expect(
|
||||
await PlatformBridge.getNativeDownloadWorkerSnapshot(),
|
||||
value is Map ? snapshot : isEmpty,
|
||||
);
|
||||
}
|
||||
messenger.setMockMethodCallHandler(channel, (_) async => '{broken');
|
||||
await expectLater(
|
||||
PlatformBridge.getNativeDownloadWorkerSnapshot(),
|
||||
throwsA(isA<FormatException>()),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'progress JSON stays ordered across large payloads and errors',
|
||||
() async {
|
||||
final listening = Completer<void>();
|
||||
final cancelled = Completer<void>();
|
||||
messenger.setMockMethodCallHandler(events, (call) async {
|
||||
if (call.method == 'listen') listening.complete();
|
||||
if (call.method == 'cancel') cancelled.complete();
|
||||
return null;
|
||||
});
|
||||
final seen = <Object>[];
|
||||
final done = Completer<void>();
|
||||
final subscription = PlatformBridge.downloadProgressStream().listen((
|
||||
value,
|
||||
) {
|
||||
seen.add(value);
|
||||
if (seen.length == 4) done.complete();
|
||||
}, onError: (Object error) => seen.add(error));
|
||||
await listening.future;
|
||||
for (final value in [
|
||||
largeJson,
|
||||
{'seq': 20},
|
||||
'{broken',
|
||||
'{"seq":21}',
|
||||
]) {
|
||||
await messenger.handlePlatformMessage(
|
||||
events.name,
|
||||
codec.encodeSuccessEnvelope(value),
|
||||
(_) {},
|
||||
);
|
||||
}
|
||||
await done.future;
|
||||
expect(seen[0], snapshot);
|
||||
expect(seen[1], {'seq': 20});
|
||||
expect(seen[2], isA<FormatException>());
|
||||
expect(seen[3], {'seq': 21});
|
||||
await subscription.cancel();
|
||||
await cancelled.future;
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'cancelling a large progress decode retires the native stream',
|
||||
() async {
|
||||
final listening = Completer<void>();
|
||||
final cancelled = Completer<void>();
|
||||
messenger.setMockMethodCallHandler(events, (call) async {
|
||||
if (call.method == 'listen') listening.complete();
|
||||
if (call.method == 'cancel') cancelled.complete();
|
||||
return null;
|
||||
});
|
||||
final seen = <Map<String, dynamic>>[];
|
||||
final subscription = PlatformBridge.downloadProgressStream().listen(
|
||||
seen.add,
|
||||
);
|
||||
await listening.future;
|
||||
await messenger.handlePlatformMessage(
|
||||
events.name,
|
||||
codec.encodeSuccessEnvelope(largeJson),
|
||||
(_) {},
|
||||
);
|
||||
await subscription.cancel();
|
||||
await cancelled.future;
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(seen, isEmpty);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user