diff --git a/API/hermes/hermes.cpp b/API/hermes/hermes.cpp index 97daa0b0ab1..c2ac8fefbe0 100644 --- a/API/hermes/hermes.cpp +++ b/API/hermes/hermes.cpp @@ -249,6 +249,17 @@ class StackRuntime { } // namespace +// Recording timing stats for every JS<->C++ transition has some overhead, so +// applications where such transitions are extremely frequent may want to define +// the HERMESJSI_DISABLE_STATS_TIMER symbol to save this overhead. +#ifdef HERMESJSI_DISABLE_STATS_TIMER +#define STATS_TIMER(rt, desc, field) +#else +#define STATS_TIMER(rt, desc, field) \ + auto &_stats = (rt).runtime_.getRuntimeStats(); \ + const vm::instrumentation::RAIITimer _timer{desc, _stats, _stats.field}; +#endif + class HermesRuntimeImpl final : public HermesRuntime, private InstallHermesFatalErrorHandler, private jsi::Instrumentation { @@ -837,9 +848,7 @@ class HermesRuntimeImpl final : public HermesRuntime, : rt_(rt), ho_(ho) {} vm::CallResult get(vm::SymbolID id) override { - auto &stats = rt_.runtime_.getRuntimeStats(); - const vm::instrumentation::RAIITimer timer{ - "HostObject.get", stats, stats.hostFunction}; + STATS_TIMER(rt_, "HostObject.get", hostFunction); jsi::PropNameID sym = rt_.add(vm::HermesValue::encodeSymbolValue(id)); jsi::Value ret; @@ -873,9 +882,7 @@ class HermesRuntimeImpl final : public HermesRuntime, } vm::CallResult set(vm::SymbolID id, vm::HermesValue value) override { - auto &stats = rt_.runtime_.getRuntimeStats(); - const vm::instrumentation::RAIITimer timer{ - "HostObject.set", stats, stats.hostFunction}; + STATS_TIMER(rt_, "HostObject.set", hostFunction); jsi::PropNameID sym = rt_.add(vm::HermesValue::encodeSymbolValue(id)); try { @@ -907,9 +914,7 @@ class HermesRuntimeImpl final : public HermesRuntime, } vm::CallResult> getHostPropertyNames() override { - auto &stats = rt_.runtime_.getRuntimeStats(); - const vm::instrumentation::RAIITimer timer{ - "HostObject.getHostPropertyNames", stats, stats.hostFunction}; + STATS_TIMER(rt_, "HostObject.getHostPropertyNames", hostFunction); try { auto names = ho_->getPropertyNames(rt_); @@ -959,9 +964,7 @@ class HermesRuntimeImpl final : public HermesRuntime, HFContext *hfc = reinterpret_cast(context); HermesRuntimeImpl &rt = hfc->hermesRuntimeImpl; assert(runtime == &rt.runtime_); - auto &stats = rt.runtime_.getRuntimeStats(); - const vm::instrumentation::RAIITimer timer{ - "Host Function", stats, stats.hostFunction}; + STATS_TIMER(rt, "Host Function", hostFunction); llvh::SmallVector apiArgs; for (vm::HermesValue hv : hvArgs) { @@ -1476,9 +1479,7 @@ jsi::Value HermesRuntimeImpl::evaluatePreparedJavaScript( assert( dynamic_cast(js.get()) && "js must be an instance of HermesPreparedJavaScript"); - auto &stats = runtime_.getRuntimeStats(); - const vm::instrumentation::RAIITimer timer{ - "Evaluate JS", stats, stats.evaluateJS}; + STATS_TIMER(*this, "Evaluate JS", evaluateJS); const auto *hermesPrep = static_cast(js.get()); vm::GCScope gcScope(&runtime_); @@ -2008,9 +2009,7 @@ jsi::Value HermesRuntimeImpl::call( "HermesRuntimeImpl::call: Unable to call function: stack overflow"); } - auto &stats = runtime_.getRuntimeStats(); - const vm::instrumentation::RAIITimer timer{ - "Incoming Function", stats, stats.incomingFunction}; + STATS_TIMER(*this, "Incoming Function", incomingFunction); vm::ScopedNativeCallFrame newFrame{ &runtime_, static_cast(count), @@ -2049,11 +2048,8 @@ jsi::Value HermesRuntimeImpl::callAsConstructor( "HermesRuntimeImpl::call: Unable to call function: stack overflow"); } - auto &stats = runtime_.getRuntimeStats(); - const vm::instrumentation::RAIITimer timer{ - "Incoming Function: Call As Constructor", - stats, - stats.incomingFunction}; + STATS_TIMER( + *this, "Incoming Function: Call As Constructor", incomingFunction); // We follow es5 13.2.2 [[Construct]] here. Below F == func. // 13.2.2.5: