Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: the light voice model loads with less free memory, and a model switch or removal leaves nothing behind
My-Name-Is-Jeffa193d6720f3b
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex bec2cfe2..2c8a0ee6 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -299,10 +299,10 @@ Shared:(SGSingRemoveOldModel, which logs the space freed), Sing moving to the new one at its next load;loaded only while Spotifyis active (SGSingLoader.m), on any iPhone with iOS 18, a CPU copy first and warmed, only with 1 GB left to the- process (no floor on the iPhone's memory: both copies warm add 0.1 GB of footprint), then on Automatic, where the iPhone has a+ process (0.4 GB for Light, and 1.6 or 0.65 GB beside a load still out; no floor on the iPhone's memory: both copies warm add 0.1 GB of footprint), then on Automatic, where the iPhone has aNeural Engine, a Neural Engine copy of the same model beside it, which takes every window once it isin, in the background too, each load with a deadline and kept a minute after the mic goes off; the- Neural Engine copy loads only with 0.5 GB left to the process, its first load after every install+ Neural Engine copy loads only with 0.5 GB left to the process (0.25 GB for Light), its first load after every installcompiling it for up to ten minutes while Karaoke runs on the CPU (while it loads, the CPU's fallingbehind spends none of the 8 s after which Karaoke gives a song up, nor counts toward its three in arow), unless Prepare after updates (on by default) compiled it already: with the model in and the mic off, 15 s afterdiff --git a/harness/sing/README.md b/harness/sing/README.mdindex c3bf723e..b8079052 100644--- a/harness/sing/README.md+++ b/harness/sing/README.md@@ -253,7 +253,10 @@ from the old model (`separator.mlmodelc`, stood in by sparse files of its sizes)finds both. `dev` checks the FLEX build's dev folder. `light` chooses the light model with the full one and the oldone stood in: it reads missing (the full one does not stand in for it), downloads from `separator-light.mlmodelc`, isleft alone by the launch's cleanup, switches to the full model and back with no download, and the other model's-removal and its own each leave the other alone. Every mode starts on the full model: the defaults outlive the+removal and its own each leave the other alone. Before that it sets the key directly, as a settings import would+(Light must read missing, not its folder unchecked), and stops Light's download mid-weights by switching to the full+model and removes Light at once (the stop's late resume data must not bring a file of it back); built on the code+before these fixes (a663fcf8), both fail. Every mode starts on the full model: the defaults outlive the`CFFIXED_USER_HOME` folder.download/build.sh && CFFIXED_USER_HOME="$(mktemp -d)" build/download 0.2diff --git a/harness/sing/download/main.m b/harness/sing/download/main.mindex 7ebead3e..3a82e3e0 100644--- a/harness/sing/download/main.m+++ b/harness/sing/download/main.m@@ -111,7 +111,26 @@ static int checkLight(NSURL *sing) {@"weights/weight.bin": @209077208});standIn(old, @{@"model.mil": @669061});CHECK(SGSingChosenModel() == SGSingModelFull && [SGSingModelURL().path isEqualToString:full.path], @"the full model is the default");+ // The key changed another way (a settings import), with no generation bump: Light, not downloaded, is checked anyway.+ [NSUserDefaults.standardUserDefaults setInteger:SGSingModelLight forKey:SGKeySingModel];+ CHECK(!SGSingModelURL(), @"a key changed by another way is checked again: Light reads missing, not its folder unchecked (%@)", SGSingModelURL().path);+ [NSUserDefaults.standardUserDefaults removeObjectForKey:SGKeySingModel];+ CHECK([SGSingModelURL().path isEqualToString:full.path], @"and the full model again once the key goes back");++ // Light's download stopped by a switch and Light removed at once: the stop's resume data, written later, does not+ // bring a file of it back.SGSetSingChosenModel(SGSingModelLight);+ SGSingDownloadModelOverCellular();+ runUntil(^BOOL { return SGSingModelProgress() >= 0.3 || SGSingModelCurrentState() != SGSingModelDownloading; }, 600);+ CHECK(SGSingModelCurrentState() == SGSingModelDownloading, @"Light's download runs into its weights (%.0f%%)", SGSingModelProgress() * 100);+ SGSetSingChosenModel(SGSingModelFull);+ SGSingDeleteOtherModel();+ runUntil(^BOOL { return NO; }, 5);+ NSArray *after = [NSFileManager.defaultManager contentsOfDirectoryAtPath:sing.path error:nil];+ CHECK(SGSingOtherModelBytes() == 0 && ![[after componentsJoinedByString:@" "] containsString:@"light"],+ @"switched and removed mid-download, nothing of Light is left (%@)", [after componentsJoinedByString:@", "]);+ SGSetSingChosenModel(SGSingModelLight);+ CHECK(SGSingModelPausedBytes() == 0, @"and Light has no paused bytes");CHECK(SGSingModelCurrentState() == SGSingModelMissing && !SGSingModelURL(), @"chosen, Light reads missing: the full model does not stand in for it");CHECK(SGSingOtherModelBytes() > 209077208, @"the other model's bytes count the full model and the old one (%lld)", SGSingOtherModelBytes());printf("Light's size reads %s, the full model's %s\n", SGSingModelSizeText().UTF8String, SGSingModelSizeTextOf(SGSingModelFull).UTF8String);diff --git a/tweak/Sources/Shared/Sing/SGSingLoader.m b/tweak/Sources/Shared/Sing/SGSingLoader.mindex a7fedab5..9d65b9b2 100644--- a/tweak/Sources/Shared/Sing/SGSingLoader.m+++ b/tweak/Sources/Shared/Sing/SGSingLoader.m@@ -20,8 +20,33 @@ static const unsigned long long kFirstRoom = 1000ull * 1000 * 1000;// footprint (resident 1.06 to 1.13 GB); 0.5 GB is that five times over, for its first compile, which was not measured// apart, and for Spotify's own memory to grow without iOS closing it for a copy Sing can do without.static const unsigned long long kNeuralRoom = 500ull * 1000 * 1000;+// The light model's (separator-light.mlmodelc, 31 MB) rooms. Measured in harness/sing on an M4 with both models, the+// copies' footprint barely differs (a warm CPU copy 0.13 GB light, 0.12 GB full) and resident memory follows the+// model's size: two CPU copies warm 0.29 GB light against 0.72 GB full, a ratio of 0.40, which scales the first and+// second copies' rooms: 0.4 GB and 0.65 GB. Its Neural Engine copy added 0.03-0.05 GB of footprint against the full+// one's 0.08 GB; 0.25 GB is five times that, as the full one's is. An earlier export took 0.3 GB of footprint on an+// iPhone 15 Pro with the CPU copy warm, so 0.4 GB still covers it, and leaves the Neural Engine copy its room after.+static const unsigned long long kLightFirstRoom = 400ull * 1000 * 1000, kLightCopyRoom = 650ull * 1000 * 1000,+ kLightNeuralRoom = 250ull * 1000 * 1000;static const int kStillEvery = 30;+// The rooms of the model at `url`: the light model's by its folder's name, which its dev copy shares.+static BOOL isLight(NSURL *url) {+ return [url.lastPathComponent isEqualToString:@"separator-light.mlmodelc"];+}++static unsigned long long firstRoom(NSURL *url) {+ return isLight(url) ? kLightFirstRoom : kFirstRoom;+}++static unsigned long long copyRoom(NSURL *url) {+ return isLight(url) ? kLightCopyRoom : kCopyRoom;+}++static unsigned long long neuralRoom(NSURL *url) {+ return isLight(url) ? kLightNeuralRoom : kNeuralRoom;+}+// One load of one copy.@interface SGSingLoad : NSObject@property (nonatomic) unsigned number;@@ -152,9 +177,9 @@ static void startFast(void) {changed();return;}- if (!roomFor(kNeuralRoom)) {+ if (!roomFor(neuralRoom(sg_url))) {sg_fastState = SGSingFastSkipped;- SGLog(@"sing: no Neural Engine copy: %@, it wants %.1f GB, so Sing stays on the CPU", memoryText(), kNeuralRoom / 1e9);+ SGLog(@"sing: no Neural Engine copy: %@, it wants %.2f GB, so Sing stays on the CPU", memoryText(), neuralRoom(sg_url) / 1e9);changed();return;}@@ -168,7 +193,7 @@ static void startFast(void) {}BOOL SGSingLoaderPrepareNeural(NSURL *url, void (^done)(BOOL loaded)) {- if (!url || sg_outstanding || sg_state != SGSingLoaderIdle || !sg_foreground || !roomFor(kNeuralRoom)) return NO;+ if (!url || sg_outstanding || sg_state != SGSingLoaderIdle || !sg_foreground || !roomFor(neuralRoom(url))) return NO;__block SGSingLoad *load = startLoad(url, YES, SGSingLoaderNeuralDeadline, ^(MLModel *model, NSError *error) {BOOL taken = sg_fastLoad == load;sg_prepareLoad = nil;@@ -185,11 +210,11 @@ BOOL SGSingLoaderPrepareNeural(NSURL *url, void (^done)(BOOL loaded)) {}static void startCPU(void) {- if (!roomFor(sg_outstanding ? kCopyRoom : kFirstRoom)) {+ if (!roomFor(sg_outstanding ? copyRoom(sg_url) : firstRoom(sg_url))) {sg_state = SGSingLoaderFailed;sg_error = sg_outstanding? [NSString stringWithFormat:@"A load abandoned earlier still holds memory, and %@. Close Spotify and open it again to load the voice model.", memoryText()]- : [NSString stringWithFormat:@"iOS leaves Spotify too little memory for the voice model: %@, and it needs %.1f GB. Closing Spotify and opening it again can free some.", memoryText(), kFirstRoom / 1e9];+ : [NSString stringWithFormat:@"iOS leaves Spotify too little memory for the voice model: %@, and it needs %.1f GB. Closing Spotify and opening it again can free some.", memoryText(), firstRoom(sg_url) / 1e9];SGLog(@"sing: no new load: %@", sg_error);changed();return;@@ -284,7 +309,7 @@ void SGSingLoaderSetForeground(BOOL foreground) {}BOOL SGSingLoaderHasRoom(void) {- return roomFor(kCopyRoom);+ return roomFor(copyRoom(sg_url));}NSURL *SGSingLoaderURL(void) {diff --git a/tweak/Sources/Shared/Sing/SGSingModel.m b/tweak/Sources/Shared/Sing/SGSingModel.mindex 0251f8f5..6e670904 100644--- a/tweak/Sources/Shared/Sing/SGSingModel.m+++ b/tweak/Sources/Shared/Sing/SGSingModel.m@@ -179,6 +179,8 @@ static double sg_progress;static BOOL sg_waitingForNetwork; // offline, or on cellular unless allowed: the session holds the requeststatic BOOL sg_cellular; // the download may use cellular and Low Data Mode networksstatic BOOL sg_checking; // the weights' checksum is being read+// Removals of each model (kModels' order), main thread: a stopped download's late resume data checks it.+static NSUInteger sg_removals[2];@implementation SGSingDownloader {NSURLSession *_session;@@ -216,10 +218,16 @@ static BOOL sg_checking; // the weights' checksum is being readreturn;}NSURL *resume = resumeFile(self.model, self.file);+ NSUInteger index = (NSUInteger)(self.model - kModels), removals = sg_removals[index];[task cancelByProducingResumeData:^(NSData *data) {- if (data) [data writeToURL:resume atomically:YES];- SGLog(@"sing: the download is stopped%@", data ? @", what came in is kept" : @"");[session invalidateAndCancel];+ // On the main thread, where models are removed: a removal since the stop wins, as the resume data would bring+ // back a file of the model it removed.+ dispatch_async(dispatch_get_main_queue(), ^{+ BOOL removed = sg_removals[index] != removals;+ if (data && !removed) [data writeToURL:resume atomically:YES];+ SGLog(@"sing: the download is stopped%@", removed ? @", and what came in is dropped with its model" : data ? @", what came in is kept" : @"");+ });}];}@@ -313,6 +321,8 @@ static BOOL sg_checking; // the weights' checksum is being read// The file is checked here, before the session deletes it, and kept in the staging folder if it is the one// pinned.- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)task didFinishDownloadingToURL:(NSURL *)location {+ // Stopped as the file came in: keeping it would put back a staging folder a removal may have just taken away.+ if (_cancelled) return;SGSingFile file = self.model->files[self.file];NSInteger status = [task.response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)task.response).statusCode : 0;BOOL answered = status == 200 || status == 206;@@ -380,13 +390,16 @@ SGSingModelState SGSingModelCurrentState(void) {}NSURL *SGSingModelURL(void) {- // Checked by size once a launch, and again after a download, a delete or a switch changes it.+ // Checked by size once a launch, and again after a download, a delete or a switch changes it, or the key changes by+ // another way (a settings import): a folder not checked could be missing a file, which crashes Core ML.static int known = -1, oldKnown = -1;static NSURL *dev;static NSUInteger knownGeneration = NSUIntegerMax;+ static const SGSingModelFiles *knownModel;const SGSingModelFiles *model = chosen();- if (knownGeneration != sg_modelGeneration) {+ if (knownGeneration != sg_modelGeneration || knownModel != model) {knownGeneration = sg_modelGeneration;+ knownModel = model;known = complete(model);// The old model stands in for the full one alone: it is the full one's earlier export.oldKnown = model == &kModels[SGSingModelFull] && oldComplete();@@ -556,6 +569,7 @@ static const SGSingModelFiles *unchosen(void) {}static void removeModel(const SGSingModelFiles *model) {+ sg_removals[model - kModels]++;long long freed = 0;removeAll(filesOf(model), &freed);[NSUserDefaults.standardUserDefaults removeObjectForKey:model->pausedKey];