fix(addons): read only the needed .toc fields instead of the whole file (#3056)

Some addons keep their data as metadata lines in the .toc (one has a
57 MB .toc with 400k "## X-..." lines). Opening the Addons tab read the
whole file into one string and kept every metadata line in a map, so each
opening allocated 220-270 MB for a moment and could push the app past its
heap limit. Stream the .toc line by line, keep only Version and
X-WoWI-ID, and stop once both are found.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-23 04:00:04 -05:00
1 parent 84c877813f
commit 43b17483fb
2 files changed
+36 -6

No files matched your search

@@ -45,6 +45,9 @@ object WowAddons {
class Origin(val id: String, val version: String)
private const val ORIGINS = ".droiddeck-sources"
private const val TOC_VERSION = "Version"
private const val TOC_WOWI_ID = "X-WoWI-ID"
private val TOC_KEYS = setOf(TOC_VERSION, TOC_WOWI_ID)
internal const val MAX_EXPANDED_BYTES = 4L * 1024 * 1024 * 1024
@@ -81,18 +84,20 @@ object WowAddons {
return installed(target).map { name ->
val toc = toc(File(target.addOns, name))
val origin = origins[name]
Installed(name, origin?.version ?: toc["Version"], origin?.id ?: toc["X-WoWI-ID"])
Installed(name, origin?.version ?: toc[TOC_VERSION], origin?.id ?: toc[TOC_WOWI_ID])
}
}
internal fun tocFields(text: String): Map<String, String> {
internal fun tocFields(lines: Sequence<String>, keys: Set<String> = TOC_KEYS): Map<String, String> {
val out = LinkedHashMap<String, String>()
for (raw in text.lineSequence()) {
for (raw in lines) {
val line = raw.trim().removePrefix("\uFEFF")
if (!line.startsWith("##") || ':' !in line) continue
val key = line.removePrefix("##").substringBefore(':').trim()
if (key !in keys || key in out) continue
val value = line.substringAfter(':').trim()
if (key.isNotEmpty() && value.isNotEmpty() && '@' !in value) out.putIfAbsent(key, value)
if (value.isNotEmpty() && '@' !in value) out[key] = value
if (out.size == keys.size) break
}
return out
}
@@ -102,7 +107,7 @@ object WowAddons {
val main = tocs.firstOrNull { it.name.dropLast(4).equals(folder.name, ignoreCase = true) }
?: tocs.firstOrNull { it.name.dropLast(4).replace(SUFFIX, "").equals(folder.name, ignoreCase = true) }
?: tocs.minByOrNull { it.name.length } ?: return emptyMap()
return runCatching { tocFields(main.readText()) }.getOrDefault(emptyMap())
return runCatching { main.bufferedReader().useLines { tocFields(it) } }.getOrDefault(emptyMap())
}
internal fun parseOrigins(text: String): Map<String, Origin> = text.lineSequence().mapNotNull { line ->
@@ -220,12 +220,37 @@ class WowAddonsTest {
}
@Test fun tocFieldsReadVersionAndSourceIds() {
val fields = WowAddons.tocFields("\uFEFF## Interface: 110002\n## Title: Foo\n## Version: 1.4.2\n## X-WoWI-ID: 1234\n## Version: 9\n## X-Curse-Project-ID: @curse-id@\nFoo.lua\n")
val fields = WowAddons.tocFields("\uFEFF## Interface: 110002\n## Title: Foo\n## Version: 1.4.2\n## X-WoWI-ID: 1234\n## Version: 9\n## X-Curse-Project-ID: @curse-id@\nFoo.lua\n".lineSequence(), setOf("Version", "X-WoWI-ID", "X-Curse-Project-ID"))
assertEquals("1.4.2", fields["Version"])
assertEquals("1234", fields["X-WoWI-ID"])
assertEquals(null, fields["X-Curse-Project-ID"])
}
@Test fun tocFieldsKeepOnlyTheRequestedKeysAndStopOnceFound() {
var read = 0
val lines = sequence {
yield("## Version: 2.0")
yield("## X-WoWI-ID: 5")
repeat(100_000) { read++; yield("## X-Data-$it: payload") }
}
val fields = WowAddons.tocFields(lines)
assertEquals(mapOf("Version" to "2.0", "X-WoWI-ID" to "5"), fields)
assertEquals(0, read)
}
@Test fun detailsSkipDataRowsInLargeTocs() {
val target = target()
val db = File(target.addOns, "BigDB").apply { mkdirs() }
File(db, "BigDB_Mists.toc").bufferedWriter().use { w ->
w.write("## Title: BigDB\nsrc\\main.lua\n")
repeat(50_000) { w.write("## X-Data-$it: ${"x".repeat(200)}\n") }
w.write("## Version: 3.1\n")
}
val details = WowAddons.details(target).single()
assertEquals("3.1", details.version)
assertEquals(null, details.wowiId)
}
@Test fun detailsUseTheTocAndTheRecordedSource() {
val target = target()
val foo = File(target.addOns, "Foo").apply { mkdirs() }