Repository navigation
General purpose memoization tool #43868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
roryabraham
merged 71 commits into
Expensify:main
from
kacper-mikolajczak:feat/memoization-tool-poc2
Jul 23, 2024
Merged
Changes from all commits
Commits
Show all changes
71 commits
Select commit
Hold shift + click to select a range
2e34ee7
install fast-equals
kacper-mikolajczak 0cbbed0
add array cache POC
kacper-mikolajczak 4af2f05
fix comments and types
kacper-mikolajczak fbc0d00
remove unnecessary variable
kacper-mikolajczak af24aa6
add MemoizedFn type
kacper-mikolajczak f049597
fix array cache
kacper-mikolajczak 6a699b8
fix get method and remove has
kacper-mikolajczak 6e4ca00
fix delete method
kacper-mikolajczak a8a8849
add tests
kacper-mikolajczak 932d26c
fix types
kacper-mikolajczak 6fb59ab
make monitor false on default
kacper-mikolajczak 3212301
add cache statistics module
kacper-mikolajczak 3dfe8d5
fix cache entries
kacper-mikolajczak 8e26459
add entry identity check
kacper-mikolajczak a368c07
rename registerStat to track
kacper-mikolajczak 3ea3af0
add cache size to cache and stats
kacper-mikolajczak 5695b2d
Global stats api v1
kacper-mikolajczak 7b59ac5
fix cache size & entries api
kacper-mikolajczak 19e59b1
fix function id for stats
kacper-mikolajczak 383488f
change default equality to deep
kacper-mikolajczak 7858c9d
fix cumulative avg calcs
kacper-mikolajczak 4cf90e6
add monitoring to profiling menu
kacper-mikolajczak 35c842f
rename delete variable name
kacper-mikolajczak dc8ad8b
use memoize for NumberFormatUtils
kacper-mikolajczak 20a9711
replace lodash memoize
kacper-mikolajczak 1b1ff6f
add monitoringName for anonymous funcitons
kacper-mikolajczak 85a0e91
fix tests
kacper-mikolajczak df7466a
Improve cache array builder description
kacper-mikolajczak 7b03364
add getKeyIndex comment
kacper-mikolajczak a815e24
arraCacheBuilder get early return
kacper-mikolajczak bc9b9ce
remove unused APIs
kacper-mikolajczak 67890fc
use default export for const
kacper-mikolajczak fa904c0
rename monitoringEnable
kacper-mikolajczak 1b0c9e6
Fix typo in memoize desc
kacper-mikolajczak e90d63e
MemoizeStats enabled -> isEnabled
kacper-mikolajczak c6910ac
add explanation to cumulativeAvg
kacper-mikolajczak dc64819
take out the isMemoizeStatsEntry identity function
kacper-mikolajczak 1f3bd24
fix saveEntry
kacper-mikolajczak 9390547
move eslint-disable
kacper-mikolajczak 65139da
fix tests
kacper-mikolajczak 7da3423
merge main
kacper-mikolajczak 0a06149
fix package-lock.json
kacper-mikolajczak 9226c50
add eslint recommendation
kacper-mikolajczak 607db57
add getSet method to cache
kacper-mikolajczak 4ae9a9c
add trackTime method to statsEntry
kacper-mikolajczak 0228144
fix typo
kacper-mikolajczak e95e208
refactor keyComparator
kacper-mikolajczak 5e13d40
add maxArgs option
kacper-mikolajczak d8e82a0
fix lodash memoize instance
kacper-mikolajczak de4a8aa
Merge branch 'main' into feat/memoization-tool-poc2
kacper-mikolajczak acbe201
supress TS errors
kacper-mikolajczak 8055ec0
fix constructable
kacper-mikolajczak dc9850e
add description for getSet method
kacper-mikolajczak 79d19c0
lodash memoize named export restriction
kacper-mikolajczak 4ecb327
add fast-equals license
kacper-mikolajczak ff557b7
merge main
kacper-mikolajczak baf7a5f
fix iOS Intl.NumberFormat polyfill
kacper-mikolajczak 50b98d8
NumberFormatUtils ios platform variant
kacper-mikolajczak 7f8aea1
restructure ios variant
kacper-mikolajczak f1aa456
Merge branch 'main' into feat/memoization-tool-poc2
kacper-mikolajczak 62b4684
fix package-lock typo
kacper-mikolajczak 5c173c3
refactor ArrayCache
kacper-mikolajczak f7b4d9c
add lines between methods
kacper-mikolajczak 462d70e
remove avgKeyLength stat
kacper-mikolajczak b1e4b02
add transformKey
kacper-mikolajczak c218004
make Key generic required
kacper-mikolajczak 79328ce
fix Search memoization
kacper-mikolajczak ec6251e
Merge branch 'main' into feat/memoization-tool-poc2
kacper-mikolajczak f710dc3
add comment explainers on existing issues
kacper-mikolajczak cc8b268
replace lodash in freezeScreenWithLazyLoading
kacper-mikolajczak 768bf3a
ArrayCache tweaks
kacper-mikolajczak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
src/libs/NumberFormatUtils.ts → src/libs/NumberFormatUtils/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,18 @@ | ||
| import type {ValueOf} from 'type-fest'; | ||
| import memoize from '@libs/memoize'; | ||
| import type CONST from '@src/CONST'; | ||
| import initPolyfill from './intlPolyfill'; | ||
|
|
||
| initPolyfill(); | ||
|
|
||
| const MemoizedNumberFormat = memoize(Intl.NumberFormat, {maxSize: 10}); | ||
|
|
||
| function format(locale: ValueOf<typeof CONST.LOCALES>, number: number, options?: Intl.NumberFormatOptions): string { | ||
| return new Intl.NumberFormat(locale, options).format(number); | ||
| return new MemoizedNumberFormat(locale, options).format(number); | ||
| } | ||
|
|
||
| function formatToParts(locale: ValueOf<typeof CONST.LOCALES>, number: number, options?: Intl.NumberFormatOptions): Intl.NumberFormatPart[] { | ||
| return new Intl.NumberFormat(locale, options).formatToParts(number); | ||
| return new MemoizedNumberFormat(locale, options).formatToParts(number); | ||
| } | ||
|
|
||
| export {format, formatToParts}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import intlPolyfill from '@libs/IntlPolyfill'; | ||
|
|
||
| // On iOS, polyfills from `additionalSetup` are applied after memoization, which results in incorrect cache entry of `Intl.NumberFormat` (e.g. lacking `formatToParts` method). | ||
| // To fix this, we need to apply the polyfill manually before memoization. | ||
| // For further information, see: https://github.com/Expensify/App/pull/43868#issuecomment-2217637217 | ||
| const initPolyfill = () => { | ||
| intlPolyfill(); | ||
| }; | ||
|
|
||
| export default initPolyfill; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| const initPolyfill = () => {}; | ||
| export default initPolyfill; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import type {Cache, CacheConfig} from './types'; | ||
|
|
||
| /** | ||
| * Builder of the cache using `Array` primitive under the hood. It is an LRU cache, where the most recently accessed elements are at the end of the array, and the least recently accessed elements are at the front. | ||
| * @param config - Cache configuration, check `CacheConfig` type for more details. | ||
| * @returns | ||
| */ | ||
| function ArrayCache<K, V>(config: CacheConfig<K>): Cache<K, V> { | ||
|
kacper-mikolajczak marked this conversation as resolved.
Resolved
|
||
| const cache: Array<[K, V]> = []; | ||
|
|
||
| const {maxSize, keyComparator} = config; | ||
|
|
||
| /** | ||
| * Returns the index of the key in the cache array. | ||
| * We search the array backwards because the most recently added entries are at the end, and our heuristic follows the principles of an LRU cache - that the most recently added entries are most likely to be used again. | ||
| */ | ||
| function getKeyIndex(key: K): number { | ||
| for (let i = cache.length - 1; i >= 0; i--) { | ||
| if (keyComparator(cache[i][0], key)) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| return { | ||
| get(key) { | ||
|
kacper-mikolajczak marked this conversation as resolved.
Resolved
|
||
| const index = getKeyIndex(key); | ||
|
|
||
| if (index === -1) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const [entry] = cache.splice(index, 1); | ||
| cache.push(entry); | ||
| return {value: entry[1]}; | ||
| }, | ||
|
|
||
| set(key, value) { | ||
| const index = getKeyIndex(key); | ||
|
|
||
| if (index !== -1) { | ||
| cache.splice(index, 1); | ||
| } | ||
|
|
||
| cache.push([key, value]); | ||
|
|
||
| if (cache.length > maxSize) { | ||
| cache.shift(); | ||
| } | ||
| }, | ||
|
|
||
| getSet(key, valueProducer) { | ||
| const index = getKeyIndex(key); | ||
|
|
||
| if (index !== -1) { | ||
| const [entry] = cache.splice(index, 1); | ||
| cache.push(entry); | ||
| return {value: entry[1]}; | ||
| } | ||
|
|
||
| const value = valueProducer(); | ||
|
|
||
| cache.push([key, value]); | ||
|
|
||
| if (cache.length > maxSize) { | ||
| cache.shift(); | ||
| } | ||
|
|
||
| return {value}; | ||
| }, | ||
|
|
||
| snapshot: { | ||
| keys: () => cache.map((entry) => entry[0]), | ||
| values: () => cache.map((entry) => entry[1]), | ||
| entries: () => [...cache], | ||
| }, | ||
|
|
||
| get size() { | ||
| return cache.length; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export default ArrayCache; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.