Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TurboModuleを用いたJSスレッドの負担軽減と Follyを用いたMainスレッドのマ...

TurboModuleを用いたJSスレッドの負担軽減と Follyを用いたMainスレッドのマルチスレッド化

株式会社BluAgeの業務内容とReact Nativeの最新技術への取り組みの紹介スライドです。

TaichiAkimoto

October 02, 2021
Tweet

Other Decks in Programming

Transcript

  1. Ұؾ௨؏ͨ͠σδλϧϓϥοτϑΥʔϜΛߏங ΧφϦʔಠ઎෺݅ ΞϓϦʮΧφϦʔʯΛల։ ࣗࣾ஥հ/ૹ٬ SaaSఏڙʢސ٬؅ཧ[CRM]ʣ ௞ି؅ཧۀΛ࣮ࢪ SaaSఏڙ Ϣʔβʔ ஥հ ෺݅ݩ/؅ཧ

    ௞ିചങͷ྆ํͰෳ਺ϓϩμΫτΛఏڙ͠ɺ෇ՃՁ஋ΛੵΈॏͶΔ ͦΕΒΛ࿈ܞ͢Δ͜ͱʹΑͬͯɺߏ଄తͳ༏ҐੑʢMOATʣΛߏங
  2. Schema࡞੒ e.g. Sqlite import { TurboModule, TurboModuleRegistry } from "react-native-tscodegen-types";

    type KeyValue = { key: string; text: string; }; export interface Spec extends TurboModule { getDraft: (key: string) => Promise<string>; updateDraft: (draft: KeyValue) => Promise<boolean>; ... } export default TurboModuleRegistry.getEnforcing<Spec>("Poc") as Spec;
  3. TurboModuleग़ྗ e.g. Sqlite namespace facebook { namespace react { static

    jsi::Value __hostFunction_PocModuleSchemaCxxSpecJSI_getDraft( jsi::Runtime &rt, TurboModule &turboModule, const jsi::Vaue *args, size_t count) { return static_cast<PocModuleSchemaCxxSpecJSI *>(&turboModule) ->getData(rt, args[0].getString(rt)); }
  4. ࣮૷ e.g. Sqlite namespace poc { using namespace facebook::react; jsi::Value

    PocModule::getDraft(jsi::Runtime &rt, const jsi::String &key) { std::string keyStr = key.utf8(rt); … draftStr = DatabaseManager::getQueryExecutor().getDraft(keyStr); … this->jsInvoker_->invokeAsync([=, &innerRt]() { if (error.size()) { promise->reject(error); return; } jsi::String draft = jsi::String::createFromUtf8(innerRt, draftStr); promise->resolve(std::move(draft)); }); }; ...
  5. NativeϨΠϠʔͷॏ͍ॲཧΛεϨουͰඇಉظʹॲཧ namespace poc { namespace jsi = facebook::jsi; class PocModule

    : public facebook::react::PocModuleSchemaCxxSpecJSI { std::unique_ptr<WorkerThread> databaseThread; std::unique_ptr<WorkerThread> cryptoThread;
  6. ThreadSafeͳMPMCQueueͷར༻ WorkerThread::WorkerThread(const std::string name) : tasks(folly::MPMCQueue<std::unique_ptr<taskType>>(100)), name(name) { auto job

    = [this]() { while (true) { std::unique_ptr<taskType> lastTask; this->tasks.blockingRead(lastTask); if (lastTask == nullptr) { break; } (*lastTask)(); } }; this->thread = std::make_unique<std::thread>(job); }