perf: cache preferred languages in L10n to avoid recomputing on every call (#16)#27
Merged
Merged
Conversation
…dLanguages on every L10n call
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
修复内容
问题:
L10n.bundle初始化时,每次遍历Locale.preferredLanguages.map({ $0.lowercased() })都会重新创建一个新数组,而Locale.preferredLanguages在 App 生命周期内完全稳定。修复:
cachedPreferredLanguages: [String]static let,在L10nenum 初始化时计算一次,后续bundle初始化以及string()/format()间接调用时直接读取缓存,不再重复调用Locale.preferredLanguages.map(...)。与 issue #10 的关系
cachedAvailableLocalizations(Bundle.module.localizations的 Set)cachedPreferredLanguages,避免每次 bundle 构造时重新映射Locale.preferredLanguageslocalizationCandidates(for:)本身已是纯函数、计算轻量,暂不需要进一步缓存影响范围