Skip to content

Commit 836eebd

Browse files
authored
Merge pull request Expensify#65721 from callstack-internal/perf/skip-checks-for-derived-value
wrap Onyx.set for derived values in a method
2 parents 619a0b6 + 68fbc0d commit 836eebd

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

src/libs/actions/OnyxDerived/configs/reportAttributes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {generateIsEmptyReport, generateReportAttributes, generateReportName, isValidReport} from '@libs/ReportUtils';
22
import SidebarUtils from '@libs/SidebarUtils';
33
import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig';
4-
import hasKeyTriggeredCompute from '@userActions/OnyxDerived/utils';
4+
import {hasKeyTriggeredCompute} from '@userActions/OnyxDerived/utils';
55
import CONST from '@src/CONST';
66
import ONYXKEYS from '@src/ONYXKEYS';
77
import type {ReportAttributesDerivedValue} from '@src/types/onyx';

src/libs/actions/OnyxDerived/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
1313
import ObjectUtils from '@src/types/utils/ObjectUtils';
1414
import ONYX_DERIVED_VALUES from './ONYX_DERIVED_VALUES';
1515
import type {DerivedValueContext} from './types';
16+
import {setDerivedValue} from './utils';
1617

1718
/**
1819
* Initialize all Onyx derived values, store them in Onyx, and setup listeners to update them when dependencies change.
@@ -42,7 +43,7 @@ function init() {
4243
// @ts-expect-error TypeScript can't confirm the shape of dependencyValues matches the compute function's parameters
4344
derivedValue = compute(dependencyValues, initialContext);
4445
dependencyValues = values;
45-
Onyx.set(key, derivedValue ?? null);
46+
setDerivedValue(key, derivedValue ?? null);
4647
});
4748
}
4849

@@ -84,7 +85,7 @@ function init() {
8485
const newDerivedValue = compute(dependencyValues, context);
8586
Log.info(`[OnyxDerived] updating value for ${key} in Onyx`);
8687
derivedValue = newDerivedValue;
87-
Onyx.set(key, derivedValue ?? null);
88+
setDerivedValue(key, derivedValue);
8889
};
8990

9091
for (let i = 0; i < dependencies.length; i++) {

src/libs/actions/OnyxDerived/utils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import Onyx from 'react-native-onyx';
2+
import type {OnyxInput} from 'react-native-onyx';
13
import type {NonEmptyTuple} from 'type-fest';
2-
import type {OnyxKey} from '@src/ONYXKEYS';
4+
import type {OnyxDerivedKey, OnyxKey} from '@src/ONYXKEYS';
35
import type {DerivedValueContext} from './types';
46

57
/**
@@ -12,4 +14,16 @@ const hasKeyTriggeredCompute = <K extends OnyxKey, Deps extends NonEmptyTuple<Ex
1214
return Object.keys(sourceValues).some((sourceKey) => sourceKey === key);
1315
};
1416

15-
export default hasKeyTriggeredCompute;
17+
/**
18+
* Set a derived value in Onyx
19+
* As a performance optimization, it skips the cache check and null removal
20+
* For derived values, we fully control their lifecycle and recompute them when any dependency changes - so we don’t need a deep comparison
21+
* Also, null may be a legitimate result of the computation, so pruning it is unnecessary
22+
*/
23+
const setDerivedValue = (key: OnyxDerivedKey, value: OnyxInput<OnyxDerivedKey>) =>
24+
Onyx.set(key, value, {
25+
skipCacheCheck: true,
26+
skipNullRemoval: true,
27+
});
28+
29+
export {hasKeyTriggeredCompute, setDerivedValue};

0 commit comments

Comments
 (0)