mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
f5c246d50e
Summary: Right now, when two threads require two NativeModules, both threads fight for the same `std::mutex`. Why? Because NativeModule require can read from/write to the shared `std::unordered_map<std::string, TurboModuleHolder*>`. **A Few Thoughts:** - All threads should be able to read from the TurboModule cache concurrently, without issue. Only writes into the cache require exclusive access. *With our current setup, both reads and writes acquire exclusive access.* - During the lifetime of an application, there will only be tens of NativeModule create events (i.e: writes into the TurboModule cache). However, there may be hundreds, if not thousands of TurboModule cache lookups. *We don't need to serialize those hundreds/thousands of TurboModule cache reads.* This is a potential optimization opportunity for the TurboModule infra. ## Changes This diff introduces a `std::shared_mutex` inside `RCTTurboModuleManager`. We use either the regular `std::mutex` or the `std::shared_mutex`, depending on whether `RCTTurboModuleSharedMutexInitEnabled()` is `YES`. Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D23413118 fbshipit-source-id: 0880413c691b141db98a2715648f0c3e05983307