-
Notifications
You must be signed in to change notification settings - Fork 573
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
RJS-2852: Adding a CallInvoker
-based scheduler for Core on React Native
#6791
Changes from 4 commits
b2b449d
49bc2da
aa65852
43492ab
fca3679
edc2c1b
7acc8b5
4ee3d51
1fa05ed
5fac334
d24db8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ | |
#import <objc/runtime.h> | ||
|
||
#import "jsi/jsi_init.h" | ||
#import "flush_ui_queue_workaround.h" | ||
#import "jsi/react_scheduler.h" | ||
|
||
// the part of the RCTCxxBridge private class we care about | ||
@interface RCTBridge (Realm_RCTCxxBridge) | ||
|
@@ -54,7 +54,7 @@ - (dispatch_queue_t)methodQueue { | |
} | ||
|
||
- (void)invalidate { | ||
realm::js::flush_ui_workaround::reset_js_call_invoker(); | ||
realm::js::react_scheduler::reset_scheduler(); | ||
#if DEBUG | ||
// Immediately close any open sync sessions to prevent race condition with new | ||
// JS thread when hot reloading | ||
|
@@ -79,7 +79,7 @@ - (void)dealloc { | |
// See https://github.com/facebook/react-native/pull/43396#issuecomment-2178586017 for context | ||
// If it was, we could use the enablement of "microtasks" to avoid the overhead of calling the invokeAsync on every call from C++ into JS. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove the comments? |
||
// if (!facebook::react::ReactNativeFeatureFlags::enableMicrotasks()) { | ||
realm::js::flush_ui_workaround::inject_js_call_invoker([bridge jsCallInvoker]); | ||
realm::js::react_scheduler::create_scheduler([bridge jsCallInvoker]); | ||
|
||
auto exports = jsi::Object(rt); | ||
realm_jsi_init(rt, exports); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 Realm Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "react_scheduler.h" | ||
|
||
#include <realm/object-store/util/scheduler.hpp> | ||
|
||
#include <ReactCommon/CallInvoker.h> | ||
#include <ReactCommon/SchedulerPriority.h> | ||
|
||
#include <thread> | ||
|
||
namespace realm::js::react_scheduler { | ||
|
||
using Scheduler = realm::util::Scheduler; | ||
|
||
// std::shared_ptr<facebook::react::CallInvoker> js_call_invoker_{}; | ||
std::shared_ptr<Scheduler> scheduler_{}; | ||
|
||
|
||
class ReactScheduler : public realm::util::Scheduler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very minor, but this type and the global variable should be in an anonymous namespace so that the symbols are not exported. |
||
public: | ||
ReactScheduler(std::shared_ptr<facebook::react::CallInvoker> js_call_invoker) | ||
: m_js_call_invoker(js_call_invoker) | ||
{ | ||
} | ||
|
||
bool is_on_thread() const noexcept override | ||
{ | ||
return m_id == std::this_thread::get_id(); | ||
} | ||
|
||
bool is_same_as(const Scheduler* other) const noexcept override | ||
{ | ||
auto o = dynamic_cast<const ReactScheduler*>(other); | ||
return (o && (o->m_js_call_invoker == m_js_call_invoker)); | ||
} | ||
|
||
bool can_invoke() const noexcept override | ||
{ | ||
return true; | ||
} | ||
|
||
void invoke(util::UniqueFunction<void()>&& func) override | ||
{ | ||
m_js_call_invoker->invokeAsync( | ||
// Using low priority to avoid blocking rendering | ||
facebook::react::SchedulerPriority::LowPriority, | ||
// Wrapping the func in a shared_ptr to ensure it outlives the invocation | ||
[func = std::make_shared<util::UniqueFunction<void()>>(std::move(func))] { | ||
(*func)(); | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
LowPriority seems suspect to me. We do want it to be below user input, but processing refreshes promptly is pretty important. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are the available values https://github.com/facebook/react-native/blob/8d0cbbf0e6435e0a29d1d5d218b93c9eef1a2dcd/packages/react-native/ReactCommon/callinvoker/ReactCommon/SchedulerPriority.h#L14-L20
Unfortunately not greatly documented 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Glancing at the implementation, it looks like it always runs the highest priority waiting task unless a lower priority task has hit a timeout (250ms for UserBlocking, 5s for Normal, 10s for Low). If there's some specific Normal priority thing that we know we need to not interrupt then using Low makes sense, but otherwise I would default to Normal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I try to refrain from c-style castings 🙈 It seems to work, but what ensure the original function owned by The docs for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no casts involved there. That's constructing a new UniqueFunction using the impl pointer (which takes ownership of the pointer) and then immediately invoking it. The function is then destroyed at the end of the statement as it's an unnamed temporary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how I misread that as a cast 🙈 What about the case where the lambda is dropped and never called, that would leak the function, right? (since no new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This relies on CallInvoker guaranteeing that it'll invoke the function it's given exactly once; more than once and it's a use-after-free and zero times is a memory leak. This is generally a very safe thing to rely on as basically everything gets way more complicated otherwise. |
||
|
||
private: | ||
std::shared_ptr<facebook::react::CallInvoker> m_js_call_invoker; | ||
std::thread::id m_id = std::this_thread::get_id(); | ||
}; | ||
|
||
std::shared_ptr<Scheduler> get_scheduler() | ||
{ | ||
if (scheduler_) { | ||
REALM_ASSERT(scheduler_->is_on_thread()); | ||
return scheduler_; | ||
} | ||
else { | ||
return Scheduler::make_platform_default(); | ||
} | ||
} | ||
|
||
void create_scheduler(std::shared_ptr<facebook::react::CallInvoker> js_call_invoker) | ||
{ | ||
scheduler_ = std::make_shared<ReactScheduler>(js_call_invoker); | ||
Scheduler::set_default_factory(get_scheduler); | ||
} | ||
|
||
void reset_scheduler() | ||
{ | ||
scheduler_.reset(); | ||
} | ||
|
||
} // namespace realm::js::react_scheduler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we remove the comments?