Skip to content

Commit dac4a43

Browse files
seeddscapibawa
andcommitted
The shader split reads each SARC member's name up to its own NUL (#968)
sarc_members took each member's name by splitting the whole rest of the archive at every NUL and keeping the first piece, so the work grew with the archive's size times its member count. Fire Emblem Fortune's Weave's split took 12 minutes, 6 of them in one 9.8 MB SARC (Layout/Common.arc.zst). It now finds the name's own NUL and decodes only that, and only for a member that has a name. The author reports that game's split at 1:51 and the same 23,638 shaders on Donkey Kong Bananza. Checked: test_split_nesting.py's new test reads an 800-member archive and compares the names and data. The old and new sarc_members gave the same results on crafted archives (nameless members, unterminated names, offsets past the end, bad UTF-8, damaged archives) and on 20,000 random ones; an 800-member synthetic archive takes 1.2 ms against 47 s. No regression games were run: the output of this one function is unchanged. Co-authored-by: capibawa <[email protected]>
1 parent b1dee89 commit dac4a43

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎dm/gfx/tools/shader_split.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,10 @@ def sarc_members(d, label='SARC'):
21172117
for i in range(n):
21182118
h, attr, s, e = struct.unpack_from('<IIII', d, hl + 12 + 16 * i)
21192119
if not s <= e <= len(d) - doff: red(f'{label}: SARC member {i} at {s:#x}..{e:#x} of the data lies outside its {len(d) - doff:#x} bytes')
2120-
nm = d[names + (attr & 0xffffff) * 4:].split(b'\0')[0].decode('utf-8', 'replace') if attr >> 24 else f'#{i}'
2120+
if attr >> 24: # up to the name's own NUL: splitting the rest of the archive per member was quadratic (Fire Emblem Fortune's Weave's Layout/Common.arc.zst: 6 minutes)
2121+
o = names + (attr & 0xffffff) * 4; z = d.find(b'\0', o)
2122+
nm = d[o:z if z >= 0 else len(d)].decode('utf-8', 'replace')
2123+
else: nm = f'#{i}'
21212124
yield nm, d[doff + s:doff + e]
21222125

21232126
NV_STAGES = ['vs', 'ps', 'gs', 'hs', 'ds', 'cs'] # NVN stage order

‎dm/tools/tests/test_split_nesting.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
one layout archive that expands 9 times, was left out), while members that overlap and Yaz0 members that expand without end are still stopped early. No game files needed."""
33
import struct
44
import sys
5+
import time
56
import unittest
67
from pathlib import Path
78
from unittest import mock
@@ -74,5 +75,14 @@ def test_yaz0_expansion_is_charged_before_it_is_decoded(self):
7475
self.assertEqual(dec.call_count, 0)
7576

7677

78+
class MemberNames(unittest.TestCase):
79+
def test_names_and_time(self): # each name ends at its own NUL; splitting the rest of the archive per member made Fire Emblem Fortune's Weave's 9.8 MB layout SARC take 6 minutes
80+
members = [(b'tex%04d.bntx' % i, bytes(0x4000)) for i in range(800)]
81+
archive = fz.sarc(members) # built outside the timing: only the member reading is measured (about 1 ms here, 47 s with the per-member split)
82+
t = time.monotonic(); got = list(ss.sarc_members(archive)); took = time.monotonic() - t
83+
self.assertEqual(got, [(n.decode(), m) for n, m in members])
84+
self.assertLess(took, 1.0)
85+
86+
7787
if __name__ == '__main__':
7888
unittest.main()

0 commit comments

Comments
 (0)