perf(xtream): Decode the channel list from the stream into typed rows #34

Merged
namillis merged 2 commits from perf/typed-stream-decode into main 2026-11-09 14:43:57 +00:00
Owner

What was wrong

XtreamClient.liveChannels read the whole get_live_streams body into one String (7.9 MB for the 23,960 channels of the pool, our IPTV pool server). It then called parseToJsonElement, which builds a tree of every row and all 15 of its fields, before mapping the 8 fields the app reads. On a low-RAM TV that String and the tree are alive at the same time.

What changed

  • XtreamRows.kt adds XtreamStream with only the fields the app reads (stream_id, name, stream_icon, epg_channel_id, category_id, tv_archive, tv_archive_duration, num) plus pool_event, the live game a Live Now channel carries. XtreamClient.kt decodes the list straight from the response stream into it with decodeFromStream, skipping other fields, then maps each row to LiveChannel as before.
  • LenientText reads those fields as text whether the provider sends "187" or 187, matching the old JsonElement?.str() helper. An object or array in one of them reads as null. pool_event stays a JsonElement, so PoolSignals.parseEvent is unchanged.
  • liveCategories decodes into XtreamCategory the same way.
  • Errors match the old parse. An HTTP error, or valid JSON that isn't an array (the object a panel sends for a bad login), gives an empty list. A body that isn't JSON, such as most error pages, or an array cut off part way, throws, so Home still shows "Could not load channels". null items are skipped and a UTF-8 byte order mark is ignored. One difference: an array item that is a number or a string now fails the whole list, where the old parse skipped it. No panel I know of sends one.

Verified

  • testDebugUnitTest (574 tests, 9 new in XtreamListDecodeTest.kt: strings versus numbers, odd values, unknown and nested fields, pool events, bodies that give an empty list, bodies that throw, null items and a byte order mark, escapes and non-ASCII names, categories), lintDebug and assembleDebug pass. LiveNowEventsTest passes unchanged. I checked the error cases against the old parse with a throwaway test.

  • The real list for the tv3 test login (23,960 channels, saved locally and deleted afterwards), parsed by the old code and the new code in a throwaway JVM test with no network: the two lists were equal field for field, with 34 pool events. Median of 5 runs after warm-up: old 29 ms and 123 MB allocated, new 27 ms and 53 MB.

  • LiveWireTV_API34 emulator with the tv3 login, with temporary logging (removed before commit), 5 cold starts each. Time and runtime allocation for the full list call, download included:

    Median time Allocated
    main 1,291 ms 100 MB
    This branch 1,048 ms 64 MB

    Home's lists and now-playing, and the Guide's channel count and Live Now game blocks, looked the same as on main, including after the error-handling fix. I haven't measured on a TV.

## What was wrong `XtreamClient.liveChannels` read the whole `get_live_streams` body into one String (7.9 MB for the 23,960 channels of the pool, our IPTV pool server). It then called `parseToJsonElement`, which builds a tree of every row and all 15 of its fields, before mapping the 8 fields the app reads. On a low-RAM TV that String and the tree are alive at the same time. ## What changed - `XtreamRows.kt` adds `XtreamStream` with only the fields the app reads (`stream_id`, `name`, `stream_icon`, `epg_channel_id`, `category_id`, `tv_archive`, `tv_archive_duration`, `num`) plus `pool_event`, the live game a Live Now channel carries. `XtreamClient.kt` decodes the list straight from the response stream into it with `decodeFromStream`, skipping other fields, then maps each row to `LiveChannel` as before. - `LenientText` reads those fields as text whether the provider sends `"187"` or `187`, matching the old `JsonElement?.str()` helper. An object or array in one of them reads as null. `pool_event` stays a `JsonElement`, so `PoolSignals.parseEvent` is unchanged. - `liveCategories` decodes into `XtreamCategory` the same way. - Errors match the old parse. An HTTP error, or valid JSON that isn't an array (the object a panel sends for a bad login), gives an empty list. A body that isn't JSON, such as most error pages, or an array cut off part way, throws, so Home still shows "Could not load channels". `null` items are skipped and a UTF-8 byte order mark is ignored. One difference: an array item that is a number or a string now fails the whole list, where the old parse skipped it. No panel I know of sends one. ## Verified - `testDebugUnitTest` (574 tests, 9 new in `XtreamListDecodeTest.kt`: strings versus numbers, odd values, unknown and nested fields, pool events, bodies that give an empty list, bodies that throw, null items and a byte order mark, escapes and non-ASCII names, categories), `lintDebug` and `assembleDebug` pass. `LiveNowEventsTest` passes unchanged. I checked the error cases against the old parse with a throwaway test. - The real list for the tv3 test login (23,960 channels, saved locally and deleted afterwards), parsed by the old code and the new code in a throwaway JVM test with no network: the two lists were equal field for field, with 34 pool events. Median of 5 runs after warm-up: old 29 ms and 123 MB allocated, new 27 ms and 53 MB. - LiveWireTV_API34 emulator with the tv3 login, with temporary logging (removed before commit), 5 cold starts each. Time and runtime allocation for the full list call, download included: | | Median time | Allocated | |---|---|---| | `main` | 1,291 ms | 100 MB | | This branch | 1,048 ms | 64 MB | Home's lists and now-playing, and the Guide's channel count and Live Now game blocks, looked the same as on `main`, including after the error-handling fix. I haven't measured on a TV.
perf(xtream): Decode the channel list from the stream into typed rows
All checks were successful
CI / test (pull_request) Successful in 7m14s
0eb8d60841
XtreamClient.liveChannels read the whole get_live_streams body into a
String (7.9 MB for the pool's 23,960 channels) and then built a JSON
tree of every row and all 15 of its fields before mapping the 8 the app
uses.

It now decodes straight from the response stream into XtreamStream,
which has only those fields plus pool_event. Unknown fields are skipped.
LenientText reads ids, numbers and flags as text whether the provider
sends them as strings or numbers, the way str() did, and reads an
object or array in those fields as null. pool_event stays a JsonElement
so PoolSignals.parseEvent is unchanged. liveCategories decodes into
XtreamCategory the same way.

A body that isn't an array of rows (a bad-login object, an error page)
still gives an empty list, and an HTTP error still does too.
fix(xtream): Keep the old parse's errors for bodies that aren't a list
All checks were successful
CI / test (pull_request) Successful in 6m9s
31dabbe8a2
The first version returned an empty list for any body it couldn't
decode. The tree parse it replaced threw for a body that isn't JSON or
an array cut off part way, so Home showed its 'Could not load channels'
error. An empty category list instead loaded Home with no rails and no
message.

getList now looks at the first character. A body that doesn't start
with [ is small, so it is parsed as text as before: valid JSON gives an
empty list, anything else throws. An array is decoded from the stream
and throws if it's cut off. null items are skipped, and a UTF-8 byte
order mark is ignored, as OkHttp's body.string() did.
namillis deleted branch perf/typed-stream-decode 2026-11-09 14:43:57 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
namillis/LiveWireTV!34
No description provided.