Skip to content

Defer creating component provider list until we have to and optimistically create it on a separate thread #79

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

Open
wants to merge 5 commits into
base: 0.78.0-discord
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,19 @@ ComponentDescriptorProviderRegistry::createComponentDescriptorRegistry(
auto registry = std::make_shared<const ComponentDescriptorRegistry>(
parameters, *this, parameters.contextContainer);

// for (const auto& pair : componentDescriptorProviders_) {
// registry->add(pair.second);
// }

std::vector<ComponentDescriptorProvider> providers;
providers.reserve(componentDescriptorProviders_.size());
for (const auto& pair : componentDescriptorProviders_) {
registry->add(pair.second);
providers.push_back(pair.second);
}

// 2) enqueue async add
registry->addMultipleAsync(std::move(providers));

componentDescriptorRegistries_.push_back(registry);

return registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ ComponentDescriptorRegistry::ComponentDescriptorRegistry(
providerRegistry_(providerRegistry),
contextContainer_(std::move(contextContainer)) {}

void ComponentDescriptorRegistry::addMultipleAsync(
std::vector<ComponentDescriptorProvider> providers) const {
// Copy everything we need before the thread starts
auto parametersCopy = parameters_;
auto contextContainerCopy = contextContainer_;

// Start thread immediately
std::thread([this, providers = std::move(providers), parametersCopy, contextContainerCopy]() {
std::unique_lock lock(mutex_);

for (const auto& provider : providers) {
auto componentDescriptor = provider.constructor(
{parametersCopy.eventDispatcher,
contextContainerCopy,
provider.flavor});

react_native_assert(componentDescriptor->getComponentHandle() == provider.handle);
react_native_assert(componentDescriptor->getComponentName() == provider.name);

auto sharedComponentDescriptor =
std::shared_ptr<const ComponentDescriptor>(std::move(componentDescriptor));

_registryByHandle[provider.handle] = sharedComponentDescriptor;
_registryByName[provider.name] = sharedComponentDescriptor;
}
}).detach();
}

void ComponentDescriptorRegistry::add(
ComponentDescriptorProvider componentDescriptorProvider) const {
std::unique_lock lock(mutex_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <react/renderer/core/ComponentDescriptor.h>
#include <react/renderer/core/InstanceHandle.h>
#include <react/utils/ContextContainer.h>
#include <thread>

namespace facebook::react {

Expand All @@ -40,6 +41,8 @@ class ComponentDescriptorRegistry {
const ComponentDescriptorProviderRegistry& providerRegistry,
ContextContainer::Shared contextContainer);

void addMultipleAsync(std::vector<ComponentDescriptorProvider> providers) const;

/*
* This is broken. Please do not use.
* If you requesting a ComponentDescriptor and unsure that it's there, you are
Expand Down
Loading