diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index 38b66722b7..ca5838bd04 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -7846,7 +7846,7 @@ static void pso4_init(void) { const char *e = getenv("DM_PSO_ARCHIVE"); NSString *user = pso_user_path(); g_arch_dir = e && !strcmp(e, "off") ? nil : e && *e ? @(e) : user ? [user.stringByDeletingLastPathComponent stringByAppendingPathComponent:@"pso_archive"] : nil; NSMutableArray> *arch = [NSMutableArray array], *archp = [NSMutableArray array]; - pso_bundle_open(archp); // first: it holds every pair's base + arch_open_begin(); pso_bundle_open(archp); arch_open_end(); // first: it holds every pair's base. Mapped, like the device's archives below: on an external drive Foundation would read the whole file (GBs) into memory if (g_arch_dir) { g_arch_q = dispatch_queue_create("dm.nvn.pso-archive", dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0)); g_arch_pending = dispatch_group_create(); diff --git a/docs/reference/memory.md b/docs/reference/memory.md index f9cbc72693..4b562a64f2 100644 --- a/docs/reference/memory.md +++ b/docs/reference/memory.md @@ -454,6 +454,7 @@ The guest address space, heaps, pools, virtual address memory and the allocators - **Symptom:** `footprint` shows `Malloc Large` about the size of `pso_archive/` (the log's `pipeline archive …: N files, … MB`), resident or swapped for the whole run. `MallocStackLogging=1` with `malloc_history -allBySize` gives `newArchiveWithURL` → `MTL4ArchiveImpl::loadFromURL` → `LoaderGlobalState::loadFile` → `-[NSData initWithContentsOfFile:options:maxLength:error:]` → malloc. - **Cause:** where the file is. Metal loads each archive through `NSData` with `NSDataReadingMappedIfSafe`, and Foundation counts a file on an external APFS drive on USB (`Removable Media: Fixed`) as unsafe to map: it reads the whole file into allocated memory. On the internal disk it maps the file. `newArchiveWithURL:error:` takes only a URL. - **Fix:** `arch_open_begin` in `nvn_mtl.m`, for every game: while the host opens its archives, it replaces that `NSData` method's implementation, and a read of a `.mtl4a` asks for `NSDataReadingMappedAlways`. It is a private method, so a system without it reads the files as before. A workaround of the framework's choice, as no public call offers one. The game already runs from that drive, so a drive pulled out ends the run either way. Mapped files are clean pages the system can drop, and they do not count in the footprint. +- **The bundled archive too:** `pso_bundle_open` (the app's `Resources/pso/.mtl4a`) ran before that window opened, so an app on an external drive still had its whole bundled archive in memory. It opens inside the window now. Ocarina of Time, app on an external SSD, same save and scene: footprint 19.0 → 12.2 GB, `Malloc Large` 5,032 → 135 MB (one 4.8 GB `NSData`), frame rate unchanged. - **Check:** a 133 MB archive on the external drive: footprint +129 MB without it, +2 MB with it. Ocarina of Time on its walk with a 3.5 GB archive: footprint 15.0 → 11.8 GB, peak 16.7 → 12.8 GB, `Malloc Large` 3.5 GB → 59 MB, opening 4.6 → 3.0 s. Long frames did not change. - **Found on:** [Kirby Star Allies](../../dm/games/01007E3006DDA000/README.md): the same stage route gave a footprint of 5,109 MB with its 1.13 GB archive on the external drive and 4,013 MB on the internal disk. [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md#found-on-this-game). iOS was not measured; its files are on the internal disk.