Skip to content

Commit

Permalink
Add ability to disable the stats timer
Browse files Browse the repository at this point in the history
Summary:
Recording timing stats for every JS<->C++ transition has some overhead, so add the ability for applications where such transitions are extremely frequent to disable the stats timer.

Use this in the recently added JSI microbenchmark.

Reviewed By: mhorowitz

Differential Revision: D32055120

fbshipit-source-id: 5d173c58e82ea1fbf6fdad2f6b37ab7d4f235f42
  • Loading branch information
kodafb authored and facebook-github-bot committed Nov 1, 2021
1 parent cf59eac commit 2a32afb
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions API/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -837,9 +848,7 @@ class HermesRuntimeImpl final : public HermesRuntime,
: rt_(rt), ho_(ho) {}

vm::CallResult<vm::HermesValue> 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<jsi::PropNameID>(vm::HermesValue::encodeSymbolValue(id));
jsi::Value ret;
Expand Down Expand Up @@ -873,9 +882,7 @@ class HermesRuntimeImpl final : public HermesRuntime,
}

vm::CallResult<bool> 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<jsi::PropNameID>(vm::HermesValue::encodeSymbolValue(id));
try {
Expand Down Expand Up @@ -907,9 +914,7 @@ class HermesRuntimeImpl final : public HermesRuntime,
}

vm::CallResult<vm::Handle<vm::JSArray>> 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_);

Expand Down Expand Up @@ -959,9 +964,7 @@ class HermesRuntimeImpl final : public HermesRuntime,
HFContext *hfc = reinterpret_cast<HFContext *>(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<jsi::Value, 8> apiArgs;
for (vm::HermesValue hv : hvArgs) {
Expand Down Expand Up @@ -1476,9 +1479,7 @@ jsi::Value HermesRuntimeImpl::evaluatePreparedJavaScript(
assert(
dynamic_cast<const HermesPreparedJavaScript *>(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<const HermesPreparedJavaScript *>(js.get());
vm::GCScope gcScope(&runtime_);
Expand Down Expand Up @@ -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<uint32_t>(count),
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2a32afb

Please sign in to comment.