Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: flag overrides do less work on the main thread at launch

My-Name-Is-Jeffeca0e476c7a3
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 5cb7678c..88a9cb61 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -477,7 +477,10 @@ once, and the redesign is built on it (the glass navigation bar, the new player
player, the queue and Connect sheets, the redesigned player header, the sleep timer's options sheet):
while Redesigned UI is on each is forced, over an override too, and their rows elsewhere show what is
forced and take no touch. `Redesigned/Kit/SGRGlassDesign.x` holds the list; what else forces a flag
-registers in `Core/SGFlagForce.h`. A change shows after Spotify restarts.
+registers in `Core/SGFlagForce.h`. A change shows after Spotify restarts. Spotify reads most flags
+only when their feature first needs them, so 15 s after launch the log says how many stored overrides
+it has asked for and which not yet (`flags: N overrides stored, …`), and an override asked for later
+gets its own line as it is asked (`flags: Spotify asked for <flag> only now, …`).
The tab editor on the Navbar page is the exception and applies as soon as the bar lays out again. It lists the tabs in the order
the bar shows them: drag to reorder, tap to hide or show, and Add a tab puts a page of Spotify's or
diff --git a/harness/launch/README.md b/harness/launch/README.md
new file mode 100644
index 00000000..b4183dab
--- /dev/null
+++ b/harness/launch/README.md
@@ -0,0 +1,25 @@
+# Launch harness
+
+What the mod adds to each remote-config flag Spotify reads as it starts. `Shared/Flags/Flags.x` hands
+every read to `SGForcedFlagValue` (`Core/SGFlagForce.m`), which asks the redesign's forcer, the All
+flags override, AdBlock.m's forcer and the lyrics sources' one. The check builds those sources as Mac
+Catalyst, so the tweak's UIKit headers compile, and times passes over the generated table of every
+flag Spotify 9.1.78 has, with Hide ads, Hide upsells and three overrides stored.
+
+ cp ../../../spotifyglass-gpl/tweak/Sources/Shared/Flags/SGFlagList.m ../../tweak/Sources/Shared/Flags/ # or make flags
+ ./build.sh && ./build/launch-check
+
+It also checks that every answer is the one the live switches and overrides give, and fails if not.
+Last it checks the override report Flags.x logs 15 s in (`SGFlagOverrideReport`): two overrides outside
+the table, one a forcer that beats overrides answers and one asked for only after the first report.
+2026-10-06: held.
+
+On an M-series Mac, five runs each:
+
+| | first pass | later pass | a read |
+|---|---|---|---|
+| every read asking the defaults (before) | 7.3 to 22.9 ms | 6.2 to 11.8 ms | 2.6 to 4.9 µs |
+| switches and overrides read once (after) | 2.8 to 4.0 ms | 1.6 ms | 0.67 µs |
+
+How many flags Spotify reads, and on which thread, is not measured here. Flags.x logs both on the
+phone 15 s after launch: `flags: N reads in the first 15 s, M on the main thread, X ms in the mod's answers`.
diff --git a/harness/launch/build.sh b/harness/launch/build.sh
new file mode 100755
index 00000000..ad5bdb8d
--- /dev/null
+++ b/harness/launch/build.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+# Builds the launch check for the Mac as Mac Catalyst, so the tweak's UIKit headers compile and the
+# sources go in as the tweak builds them: the flag forcing of Core/, AdBlock.m's forcer, the redesign's
+# and the generated flag table (copy tweak/Sources/Shared/Flags/SGFlagList.m in first, `make flags`).
+set -e
+cd "$(dirname "$0")"
+SRC=../../tweak/Sources
+mkdir -p build
+SDK=$(xcrun --sdk macosx --show-sdk-path)
+IOS="$SDK/System/iOSSupport"
+xcrun clang -isysroot "$SDK" -iframework "$IOS/System/Library/Frameworks" -isystem "$IOS/usr/include" \
+ -F "$IOS/System/Library/Frameworks" -L "$IOS/usr/lib" -fobjc-arc -O2 -g -Wall -Werror -target arm64-apple-ios17.0-macabi -I "$SRC" -DSG_VERSION='"0.0.0"' \
+ main.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGLog.m "$SRC"/Core/SGUIMode.m \
+ "$SRC"/Shared/AdBlock/AdBlock.m "$SRC"/Shared/Flags/SGFlagList.m "$SRC"/Redesigned/Kit/SGRedesign.m \
+ -framework Foundation -framework UIKit -o build/launch-check
+echo "built build/launch-check"
diff --git a/harness/launch/main.m b/harness/launch/main.m
new file mode 100644
index 00000000..e74fcf6a
--- /dev/null
+++ b/harness/launch/main.m
@@ -0,0 +1,105 @@
+// What the mod adds to every remote-config flag Spotify reads as it starts: Flags.x hands each read to
+// SGForcedFlagValue, which asks every forcer and the All flags override. Run on the Mac (Mac Catalyst,
+// so the tweak's UIKit headers compile) over the generated table of every flag Spotify 9.1.78 has,
+// with the forcers registered the way the tweak registers them: AdBlock.m's from its constructor, the
+// redesign's through SGRedesignForceFlags, and one standing in for LyricsSources.m's (a dispatch_once
+// and a nil, which is what it costs with the lyrics sources off).
+//
+// The stored switches are a typical install's: Hide ads and Hide upsells on, three overrides from the
+// All flags page. Prints the cost of one pass over the table, the best of several, and checks that
+// every answer is the same with and without the mod's launch snapshot, so a faster answer is never a
+// different one.
+#import <Foundation/Foundation.h>
+#import <mach/mach_time.h>
+#import "Core/SGFlagForce.h"
+#import "Core/SGPrefs.h"
+#import "Shared/AdBlock/AdBlock.h"
+#import "Shared/Flags/Flags.h"
+#import "Redesigned/Kit/SGRedesign.h"
+
+static double now(void) {
+ static mach_timebase_info_data_t base;
+ if (!base.denom) mach_timebase_info(&base);
+ return (double)mach_absolute_time() * base.numer / base.denom / 1e6;
+}
+
+int main(int argc, char **argv) {
+ @autoreleasepool {
+ NSUserDefaults *store = NSUserDefaults.standardUserDefaults;
+ for (NSString *key in [store dictionaryRepresentation]) {
+ if ([key hasPrefix:@"spotifyglass."]) [store removeObjectForKey:key];
+ }
+ [store setBool:YES forKey:SGKeyHideAds];
+ [store setBool:YES forKey:SGKeyHideUpsells];
+ SGSetFlagOverride(@"ios-account-switching.is_enabled", @YES);
+ SGSetFlagOverride(@"ios-account-switching.max_accounts", @4);
+ SGSetFlagOverride(@"ios-feature-adonappopen.enabled", @YES);
+ // Two flags outside the table, for the override report after the passes: one Spotify asks for
+ // late and one a forcer that beats an override answers.
+ SGSetFlagOverride(@"ios-harness.late", @YES);
+ SGSetFlagOverride(@"ios-harness.beaten", @NO);
+
+ // LyricsSources.m's forcer, off: a dispatch_once and a nil.
+ SGRegisterFlagForcer(NO, ^id(NSString *key) {
+ static BOOL on;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ on = NO; });
+ return on ? @5000 : nil;
+ }, nil);
+ SGRedesignForceFlags(@"harness", @{@"ios-feature-nowplaying.sheet_style": @YES});
+
+ NSMutableArray<NSString *> *keys = [NSMutableArray arrayWithCapacity:SGFlagCount];
+ for (NSUInteger i = 0; i < SGFlagCount; i++) [keys addObject:@(SGFlagTable[i].key)];
+
+ // Every answer, for the check after the passes; this first pass also reads what is read once.
+ NSMutableDictionary<NSString *, id> *answers = [NSMutableDictionary dictionary];
+ double first = now();
+ for (NSString *key in keys) answers[key] = SGForcedFlagValue(key) ?: NSNull.null;
+ first = now() - first;
+
+ double best = INFINITY;
+ for (int pass = 0; pass < 20; pass++) {
+ double start = now();
+ for (NSString *key in keys) (void)SGForcedFlagValue(key);
+ best = MIN(best, now() - start);
+ }
+ printf("%lu flags, first pass %.2f ms, then one pass: %.2f ms, %.2f us a read (best of 20)\n",
+ (unsigned long)keys.count, first, best, best * 1000 / keys.count);
+
+ NSUInteger forced = 0, wrong = 0;
+ for (NSString *key in keys) {
+ id expected = SGFlagOverride(key) ?: (SGAdBlockForcesFlagOff(key) ? @NO : nil);
+ id got = SGForcedFlagValue(key);
+ if (got) forced++;
+ if (!(expected == got || [expected isEqual:got])) {
+ printf("wrong answer for %s\n", key.UTF8String);
+ wrong++;
+ }
+ if (!([answers[key] isEqual:got ?: NSNull.null])) {
+ printf("answer changed between passes for %s\n", key.UTF8String);
+ wrong++;
+ }
+ }
+ printf("%lu flags forced, %lu wrong\n", (unsigned long)forced, (unsigned long)wrong);
+
+ // The override report Flags.x logs 15 s after launch, and what it says once the late flag is asked.
+ SGRegisterFlagForcer(YES, ^id(NSString *key) { return [key isEqualToString:@"ios-harness.beaten"] ? @YES : nil; }, nil);
+ if (![SGForcedFlagValue(@"ios-harness.beaten") isEqual:@YES]) { printf("the beating forcer lost\n"); wrong++; }
+ NSString *report = SGFlagOverrideReport();
+ printf("report: %s\n", report.UTF8String);
+ if (![report isEqualToString:@"5 overrides stored, 3 asked for and forced, 1 answered by another switch: "
+ @"ios-harness.beaten (1, not 0), 1 not asked for yet: ios-harness.late"]) {
+ printf("wrong report\n");
+ wrong++;
+ }
+ if (![SGForcedFlagValue(@"ios-harness.late") isEqual:@YES]) { printf("the late override lost\n"); wrong++; }
+ report = SGFlagOverrideReport();
+ printf("report after the late ask: %s\n", report.UTF8String);
+ if (![report hasPrefix:@"5 overrides stored, 4 asked for and forced, 1 answered by another switch"] ||
+ ![report hasSuffix:@", 0 not asked for yet"]) {
+ printf("wrong report after the late ask\n");
+ wrong++;
+ }
+ return wrong ? 1 : 0;
+ }
+}
diff --git a/tweak/Sources/Core/SGFlagForce.h b/tweak/Sources/Core/SGFlagForce.h
index 3780048c..98c9700c 100644
--- a/tweak/Sources/Core/SGFlagForce.h
+++ b/tweak/Sources/Core/SGFlagForce.h
@@ -14,8 +14,15 @@ typedef id (^SGFlagForcer)(NSString *key);
void SGRegisterFlagForcer(BOOL beatsOverride, SGFlagForcer atLaunch, SGFlagForcer locked);
-// The value the provider hands Spotify: forcers that beat an override, the override, then the rest.
+// The value the provider hands Spotify: forcers that beat an override, the override as it was at
+// launch, then the rest.
id SGForcedFlagValue(NSString *key);
// The value a settings row is locked at, nil when none; `beatsOverride` says whether an override
// from the All flags page gives way to it.
id SGLockedFlagValue(NSString *key, BOOL *beatsOverride);
+
+// What became of the overrides stored at launch (a flag row's switch or the All flags page), for the
+// log: how many Spotify asked for and got, which a forcer that beats an override answered instead, and
+// which it has not asked for yet. Spotify builds most features' flags when the feature first loads,
+// not at launch, so an override it asks for after this report is logged on its own as it is asked.
+NSString *SGFlagOverrideReport(void);
diff --git a/tweak/Sources/Core/SGFlagForce.m b/tweak/Sources/Core/SGFlagForce.m
index 897d3f03..0f286e4d 100644
--- a/tweak/Sources/Core/SGFlagForce.m
+++ b/tweak/Sources/Core/SGFlagForce.m
@@ -1,5 +1,6 @@
#import <os/lock.h>
#import "SGFlagForce.h"
+#import "SGLog.h"
#import "SGPrefs.h"
@interface SGFlagForcerEntry : NSObject
@@ -32,16 +33,55 @@ static NSArray<SGFlagForcerEntry *> *forcers(void) {
return list;
}
+// The All flags overrides as they were at launch, the prefix taken off: Spotify asks for its flags
+// once each, many as it starts, and one dictionary read from these costs a fraction of a defaults lookup each
+// (harness/launch). An override set later shows after a restart, as every flag does.
+static NSDictionary<NSString *, id> *overridesAtLaunch(void) {
+ static NSDictionary<NSString *, id> *overrides;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ NSMutableDictionary<NSString *, id> *found = [NSMutableDictionary dictionary];
+ [NSUserDefaults.standardUserDefaults.dictionaryRepresentation enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
+ if ([key hasPrefix:SGFlagOverridePrefix]) found[[key substringFromIndex:SGFlagOverridePrefix.length]] = value;
+ }];
+ overrides = [found copy];
+ });
+ return overrides;
+}
+
+// The overrides Spotify has asked for, with the answer it got. Only an override's key takes the lock,
+// so the reads of every other flag cost what they did.
+static os_unfair_lock sg_askedLock = OS_UNFAIR_LOCK_INIT;
+static NSMutableDictionary<NSString *, id> *sg_asked;
+static BOOL sg_reported;
+
+static void noteAsked(NSString *key, id override, id answer) {
+ os_unfair_lock_lock(&sg_askedLock);
+ BOOL first = !sg_asked[key];
+ if (first) {
+ if (!sg_asked) sg_asked = [NSMutableDictionary dictionary];
+ sg_asked[[key copy]] = answer;
+ }
+ BOOL late = first && sg_reported;
+ os_unfair_lock_unlock(&sg_askedLock);
+ if (!late) return;
+ if ([answer isEqual:override]) SGLog(@"flags: Spotify asked for %@ only now, and got the override %@", key, answer);
+ else SGLog(@"flags: Spotify asked for %@ only now, and got %@ from another switch, not the override %@", key, answer, override);
+}
+
id SGForcedFlagValue(NSString *key) {
if (!key) return nil;
NSArray<SGFlagForcerEntry *> *list = forcers();
+ id value = nil;
for (SGFlagForcerEntry *entry in list) {
if (!entry.beatsOverride) continue;
- id value = entry.atLaunch(key);
- if (value) return value;
+ value = entry.atLaunch(key);
+ if (value) break;
}
- id value = SGFlagOverride(key);
+ id override = overridesAtLaunch()[key];
+ if (override) noteAsked(key, override, value ?: override);
if (value) return value;
+ if (override) return override;
for (SGFlagForcerEntry *entry in list) {
if (entry.beatsOverride) continue;
value = entry.atLaunch(key);
@@ -66,3 +106,25 @@ id SGLockedFlagValue(NSString *key, BOOL *beatsOverride) {
}
return nil;
}
+
+NSString *SGFlagOverrideReport(void) {
+ NSDictionary<NSString *, id> *overrides = overridesAtLaunch();
+ os_unfair_lock_lock(&sg_askedLock);
+ NSDictionary<NSString *, id> *asked = [sg_asked copy];
+ sg_reported = YES;
+ os_unfair_lock_unlock(&sg_askedLock);
+ NSUInteger forced = 0;
+ NSMutableArray<NSString *> *beaten = [NSMutableArray array], *notAsked = [NSMutableArray array];
+ for (NSString *key in [overrides.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
+ id answer = asked[key];
+ if (!answer) [notAsked addObject:key];
+ else if ([answer isEqual:overrides[key]]) forced++;
+ else [beaten addObject:[NSString stringWithFormat:@"%@ (%@, not %@)", key, answer, overrides[key]]];
+ }
+ NSMutableString *text = [NSMutableString stringWithFormat:@"%lu overrides stored, %lu asked for and forced",
+ (unsigned long)overrides.count, (unsigned long)forced];
+ if (beaten.count) [text appendFormat:@", %lu answered by another switch: %@", (unsigned long)beaten.count, [beaten componentsJoinedByString:@", "]];
+ [text appendFormat:@", %lu not asked for yet", (unsigned long)notAsked.count];
+ if (notAsked.count) [text appendFormat:@": %@", [notAsked componentsJoinedByString:@", "]];
+ return text;
+}
diff --git a/tweak/Sources/Shared/AdBlock/AdBlock.m b/tweak/Sources/Shared/AdBlock/AdBlock.m
index feceea5c..d720c474 100644
--- a/tweak/Sources/Shared/AdBlock/AdBlock.m
+++ b/tweak/Sources/Shared/AdBlock/AdBlock.m
@@ -46,16 +46,34 @@ static BOOL listed(NSString *key, NSString *const list[], size_t count) {
return NO;
}
+// The switches that force flags off, one bit each, so the forcer below can read them once.
+enum { kAds = 1, kVideos = 2, kUpsells = 4 };
+
+static unsigned switchesNow(void) {
+ return (SGHidden(SGKeyHideAds) ? kAds : 0) | (SGHidden(SGKeyHideSearchVideos) ? kVideos : 0)
+ | (SGHidden(SGKeyHideUpsells) ? kUpsells : 0);
+}
+
+static BOOL forcesOff(NSString *key, unsigned on) {
+ if ((on & kAds) && listed(key, adFlags, sizeof(adFlags) / sizeof(adFlags[0]))) return YES;
+ if ((on & kVideos) && [key isEqualToString:@"ios-feature-search.video_carousel_section_enabled"]) return YES;
+ return (on & kUpsells) && listed(key, upsellFlags, sizeof(upsellFlags) / sizeof(upsellFlags[0]));
+}
+
BOOL SGAdBlockForcesFlagOff(NSString *key) {
- if (SGHidden(SGKeyHideAds) && listed(key, adFlags, sizeof(adFlags) / sizeof(adFlags[0]))) return YES;
- if (SGHidden(SGKeyHideSearchVideos) && [key isEqualToString:@"ios-feature-search.video_carousel_section_enabled"]) return YES;
- return SGHidden(SGKeyHideUpsells) && listed(key, upsellFlags, sizeof(upsellFlags) / sizeof(upsellFlags[0]));
+ return forcesOff(key, switchesNow());
}
// After an override from the All flags page, and locking the rows that would turn the same flag off.
+// Spotify asks for every flag it has as it starts, so the running app's answer reads the switches once
+// rather than four defaults lookups a flag (harness/launch); the rows read them as they are stored now.
__attribute__((constructor)) static void registerForcer(void) {
- SGFlagForcer off = ^id(NSString *key) { return SGAdBlockForcesFlagOff(key) ? @NO : nil; };
- SGRegisterFlagForcer(NO, off, off);
+ SGRegisterFlagForcer(NO, ^id(NSString *key) {
+ static unsigned atLaunch;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ atLaunch = switchesNow(); });
+ return atLaunch && forcesOff(key, atLaunch) ? @NO : nil;
+ }, ^id(NSString *key) { return SGAdBlockForcesFlagOff(key) ? @NO : nil; });
}
#pragma mark - counters
diff --git a/tweak/Sources/Shared/Flags/Flags.x b/tweak/Sources/Shared/Flags/Flags.x
index dd0ae9c9..72230284 100644
--- a/tweak/Sources/Shared/Flags/Flags.x
+++ b/tweak/Sources/Shared/Flags/Flags.x
@@ -1,13 +1,25 @@
-// Spotify reads every remote-config flag once at startup through the configuration provider,
-// keyed "component.property". The value handed back is Core/SGFlagForce.h's: what the redesign forces
+// Spotify reads each feature's remote-config flags once, through the configuration provider, keyed
+// "component.property": a few dozen as it starts, the rest when their feature first needs them. The value handed back is Core/SGFlagForce.h's: what the redesign forces
// (Redesigned/Kit/SGRedesign.h), which comes before an override so one left from Spotify's own screens
// cannot pull a redesigned one apart, then an override from the Flags page, then what the ad blocking
// and the lyrics sources force.
+#import <mach/mach_time.h>
+#import <pthread.h>
+#import <stdatomic.h>
#import "Core/SGCore.h"
#import "Flags.h"
+// What the mod's answers cost as Spotify starts, logged once (harness/launch measures it on the Mac).
+static _Atomic uint64_t sg_reads, sg_mainReads, sg_ticks;
+static const NSTimeInterval kCountFor = 15;
+
static id forced(NSString *key) {
- return SGForcedFlagValue(key);
+ uint64_t start = mach_absolute_time();
+ id value = SGForcedFlagValue(key);
+ atomic_fetch_add_explicit(&sg_ticks, mach_absolute_time() - start, memory_order_relaxed);
+ atomic_fetch_add_explicit(&sg_reads, 1, memory_order_relaxed);
+ if (pthread_main_np()) atomic_fetch_add_explicit(&sg_mainReads, 1, memory_order_relaxed);
+ return value;
}
static BOOL boolFor(NSString *key, BOOL orig) {
@@ -62,4 +74,13 @@ static id enumFor(NSString *key, id orig) {
@"_TtC22RemoteConfigurationSDK25ConfigurationProviderImpl",
@"_TtC22RemoteConfigurationSDK35ObservableConfigurationProviderImpl",
]);
+ // Off the main queue, so a main thread held up at launch does not hold the report back with it.
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kCountFor * NSEC_PER_SEC)), dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
+ mach_timebase_info_data_t base;
+ mach_timebase_info(&base);
+ double ms = (double)atomic_load(&sg_ticks) * base.numer / base.denom / 1e6;
+ SGLog(@"flags: %llu reads in the first %.0f s, %llu on the main thread, %.1f ms in the mod's answers",
+ atomic_load(&sg_reads), kCountFor, atomic_load(&sg_mainReads), ms);
+ SGLog(@"flags: %@", SGFlagOverrideReport());
+ });
}