Skip to content

Commit 49a637f

Browse files
authored
fix(firebase-perf): Update device cache only if RC value is different from cached value (#6431)
Fixes #6407
1 parent e3dd809 commit 49a637f

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

firebase-perf/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
* [fixed] Fixed a performance issue with shared preferences
4+
calling `.apply()` every time a value is read from remote config (#6407)
35

46
# 21.0.3
57
* [changed] Bump internal dependencies.

firebase-perf/src/main/java/com/google/firebase/perf/config/ConfigResolver.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ private boolean getIsSdkEnabled() {
221221
// 2. If the value exists in device cache, return this value.
222222
// 3. Otherwise, return default value.
223223
SdkEnabled config = SdkEnabled.getInstance();
224+
Optional<Boolean> deviceCacheValue = getDeviceCacheBoolean(config);
224225

225226
// 1. Reads value from Firebase Remote Config, saves this value in cache layer if fetch status
226227
// is not failure.
@@ -230,13 +231,19 @@ private boolean getIsSdkEnabled() {
230231
if (remoteConfigManager.isLastFetchFailed()) {
231232
return false;
232233
}
233-
// b. Cache and return this value.
234-
deviceCacheManager.setValue(config.getDeviceCacheFlag(), rcValue.get());
235-
return rcValue.get();
234+
235+
Boolean newValue = rcValue.get();
236+
// b. Only cache and return this value if it is different from the current value.
237+
if (deviceCacheValue == null
238+
|| !deviceCacheValue.isAvailable()
239+
|| deviceCacheValue.get() != newValue) {
240+
deviceCacheManager.setValue(config.getDeviceCacheFlag(), newValue);
241+
}
242+
243+
return newValue;
236244
}
237245

238246
// 2. If the value exists in device cache, return this value.
239-
Optional<Boolean> deviceCacheValue = getDeviceCacheBoolean(config);
240247
if (deviceCacheValue.isAvailable()) {
241248
return deviceCacheValue.get();
242249
}
@@ -257,17 +264,23 @@ private boolean getIsSdkVersionDisabled() {
257264
// 2. If the value exists in device cache, return this value.
258265
// 3. Otherwise, return default value.
259266
SdkDisabledVersions config = SdkDisabledVersions.getInstance();
267+
Optional<String> deviceCacheValue = getDeviceCacheString(config);
260268

261269
// 1. Reads value from Firebase Remote Config, cache and return this value.
262270
Optional<String> rcValue = getRemoteConfigString(config);
263271
if (rcValue.isAvailable()) {
264272
// Do not check FRC last fetch status because it is the most recent value device can get.
265-
deviceCacheManager.setValue(config.getDeviceCacheFlag(), rcValue.get());
266-
return isFireperfSdkVersionInList(rcValue.get());
273+
String newValue = rcValue.get();
274+
// Only cache and return this value if it is different from the current value.
275+
if (deviceCacheValue == null
276+
|| !deviceCacheValue.isAvailable()
277+
|| !deviceCacheValue.get().equals(newValue)) {
278+
deviceCacheManager.setValue(config.getDeviceCacheFlag(), newValue);
279+
}
280+
return isFireperfSdkVersionInList(newValue);
267281
}
268282

269283
// 2. If the value exists in device cache, return this value.
270-
Optional<String> deviceCacheValue = getDeviceCacheString(config);
271284
if (deviceCacheValue.isAvailable()) {
272285
return isFireperfSdkVersionInList(deviceCacheValue.get());
273286
}

firebase-perf/src/test/java/com/google/firebase/perf/config/ConfigResolverTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,23 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnDefa
280280
verify(mockDeviceCacheManager, never()).setValue(any(), anyString());
281281
}
282282

283+
@Test
284+
public void getIsServiceCollectionEnabled_deviceCacheHasSameValueAsFrc_returnCacheValue() {
285+
when(mockRemoteConfigManager.getBoolean(FIREBASE_PERFORMANCE_SDK_ENABLED_FRC_KEY))
286+
.thenReturn(Optional.of(true));
287+
when(mockDeviceCacheManager.getBoolean(FIREBASE_PERFORMANCE_SDK_ENABLED_CACHE_KEY))
288+
.thenReturn(Optional.of(true));
289+
290+
when(mockDeviceCacheManager.getString(FIREBASE_PERFORMANCE_DISABLED_VERSIONS_CACHE_KEY))
291+
.thenReturn(Optional.of(""));
292+
when(mockRemoteConfigManager.getString(FIREBASE_PERFORMANCE_DISABLED_VERSIONS_FRC_KEY))
293+
.thenReturn(Optional.of(""));
294+
295+
assertThat(testConfigResolver.getIsServiceCollectionEnabled()).isTrue();
296+
verify(mockDeviceCacheManager, never()).setValue(any(), anyBoolean());
297+
verify(mockDeviceCacheManager, never()).setValue(any(), anyString());
298+
}
299+
283300
@Test
284301
public void
285302
getIsPerformanceCollectionConfigValueAvailable_noDeviceCacheNoRemoteConfig_returnsFalse() {

0 commit comments

Comments
 (0)