mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Deduplicate strings and provide std::string_view (#51098)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51098 # Changelog: [Internal] Depending on the sampling rate, there could be lots of samples recorded. Right now, React Native requests the rate to be 10kHz. In reality, Hermes will be slower. How exactly slower will depend on the platform, because sampling profiler implementation is platform-specific. Windows peaks at ~500Hz, whereas iOS and Android that are using signals can reach up to 5kHz, based on my observations. This means that we could record sample approximately every 0.5ms. For 30 seconds of sampling, we can get around 150k samples, every one of which may have up to 500 call frames (it is a custom limitation on Hermes side). Previously, every call frame could have a unique copy of function name string and url string. This diff changes the approach to deduplicate strings, keeping a single copy for every unique string. A new abstraction `StringEntry` is added that will keep a const pointer to the string storage and an offset to the entry in this storage. On React Native side, we are going to re-use these `std::string_view` and keep Hermes' `Profile` alive as long as they are used. We are going to achieve this by keeping unique pointer to the Hermes' `Profile` in React Native's Profile abstraction - `RuntimeSamplingProfile`, which is agnostic to the runtime. Reviewed By: fbmal7 Differential Revision: D73106068 fbshipit-source-id: bdc7026e19c2677e39e20f14c8532080c5842026
This commit is contained in:
1 parent
2d7342f8b4
commit
3148f0fb2b
5 files changed
+73
-31
No files matched your search
+10
-6
@@ -18,9 +18,9 @@ namespace fhsp = facebook::hermes::sampling_profiler;
|
||||
/// Fallback script ID for call frames, when Hermes didn't provide one or when
|
||||
/// this frame is part of the VM, like native functions, used for parity with
|
||||
/// Chromium + V8.
|
||||
const uint32_t FALLBACK_SCRIPT_ID = 0;
|
||||
constexpr uint32_t FALLBACK_SCRIPT_ID = 0;
|
||||
/// Garbage collector frame name, used for parity with Chromium + V8.
|
||||
const std::string GARBAGE_COLLECTOR_FRAME_NAME = "(garbage collector)";
|
||||
constexpr std::string_view GARBAGE_COLLECTOR_FRAME_NAME = "(garbage collector)";
|
||||
|
||||
/// Filters out Hermes Suspend frames related to Debugger.
|
||||
/// Even though Debugger domain is expected to be disabled, Hermes might run
|
||||
@@ -74,8 +74,9 @@ RuntimeSamplingProfile::SampleCallStackFrame convertJSFunctionHermesFrame(
|
||||
RuntimeSamplingProfile::SampleCallStackFrame::Kind::JSFunction,
|
||||
frame.getScriptId(),
|
||||
frame.getFunctionName(),
|
||||
frame.hasUrl() ? std::optional<std::string>{frame.getUrl()}
|
||||
: std::nullopt,
|
||||
frame.hasScriptUrl()
|
||||
? std::optional<std::string_view>{frame.getScriptUrl()}
|
||||
: std::nullopt,
|
||||
frame.hasFunctionLineNumber()
|
||||
? std::optional<uint32_t>{frame.getFunctionLineNumber() - 1}
|
||||
// Hermes VM keeps line numbers as 1-based. Convert
|
||||
@@ -145,7 +146,7 @@ RuntimeSamplingProfile::Sample convertHermesSampleToTracingSample(
|
||||
|
||||
/* static */ RuntimeSamplingProfile
|
||||
HermesRuntimeSamplingProfileSerializer::serializeToTracingSamplingProfile(
|
||||
const hermes::sampling_profiler::Profile& hermesProfile) {
|
||||
hermes::sampling_profiler::Profile hermesProfile) {
|
||||
const auto samplesRange = hermesProfile.getSamplesRange();
|
||||
std::vector<RuntimeSamplingProfile::Sample> reconciledSamples;
|
||||
reconciledSamples.reserve(hermesProfile.getSamplesCount());
|
||||
@@ -156,7 +157,10 @@ HermesRuntimeSamplingProfileSerializer::serializeToTracingSamplingProfile(
|
||||
reconciledSamples.push_back(std::move(reconciledSample));
|
||||
}
|
||||
|
||||
return RuntimeSamplingProfile{"Hermes", std::move(reconciledSamples)};
|
||||
return RuntimeSamplingProfile{
|
||||
"Hermes",
|
||||
std::move(reconciledSamples),
|
||||
std::make_unique<RawHermesRuntimeProfile>(std::move(hermesProfile))};
|
||||
}
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern::tracing
|
||||
+11
-1
@@ -13,10 +13,20 @@
|
||||
|
||||
namespace facebook::react::jsinspector_modern::tracing {
|
||||
|
||||
class RawHermesRuntimeProfile : public RawRuntimeProfile {
|
||||
public:
|
||||
explicit RawHermesRuntimeProfile(
|
||||
hermes::sampling_profiler::Profile hermesProfile)
|
||||
: hermesProfile_{std::move(hermesProfile)} {}
|
||||
|
||||
private:
|
||||
hermes::sampling_profiler::Profile hermesProfile_;
|
||||
};
|
||||
|
||||
class HermesRuntimeSamplingProfileSerializer {
|
||||
public:
|
||||
static tracing::RuntimeSamplingProfile serializeToTracingSamplingProfile(
|
||||
const hermes::sampling_profiler::Profile& hermesProfile);
|
||||
hermes::sampling_profiler::Profile hermesProfile);
|
||||
};
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern::tracing
|
||||
+28
-8
@@ -7,13 +7,23 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace facebook::react::jsinspector_modern::tracing {
|
||||
|
||||
/// Opaque class to represent the original runtime profile returned by the
|
||||
/// Runtime. RuntimeSamplingProfile class is designed to be agnostic to the
|
||||
/// Runtime where sampling occurred.
|
||||
class RawRuntimeProfile {
|
||||
public:
|
||||
virtual ~RawRuntimeProfile() = default;
|
||||
};
|
||||
|
||||
/// Contains relevant information about the sampled runtime from start to
|
||||
/// finish.
|
||||
struct RuntimeSamplingProfile {
|
||||
@@ -33,8 +43,8 @@ struct RuntimeSamplingProfile {
|
||||
SampleCallStackFrame(
|
||||
const Kind kind,
|
||||
const uint32_t scriptId,
|
||||
std::string functionName,
|
||||
std::optional<std::string> url = std::nullopt,
|
||||
std::string_view functionName,
|
||||
std::optional<std::string_view> url = std::nullopt,
|
||||
const std::optional<uint32_t>& lineNumber = std::nullopt,
|
||||
const std::optional<uint32_t>& columnNumber = std::nullopt)
|
||||
: kind_(kind),
|
||||
@@ -55,7 +65,7 @@ struct RuntimeSamplingProfile {
|
||||
}
|
||||
|
||||
/// \return name of the function that represents call frame.
|
||||
const std::string& getFunctionName() const {
|
||||
std::string_view getFunctionName() const {
|
||||
return functionName_;
|
||||
}
|
||||
|
||||
@@ -64,7 +74,7 @@ struct RuntimeSamplingProfile {
|
||||
}
|
||||
|
||||
/// \return source url of the corresponding script in the VM.
|
||||
const std::string& getUrl() const {
|
||||
std::string_view getUrl() const {
|
||||
return url_.value();
|
||||
}
|
||||
|
||||
@@ -95,8 +105,8 @@ struct RuntimeSamplingProfile {
|
||||
private:
|
||||
Kind kind_;
|
||||
uint32_t scriptId_;
|
||||
std::string functionName_;
|
||||
std::optional<std::string> url_;
|
||||
std::string_view functionName_;
|
||||
std::optional<std::string_view> url_;
|
||||
std::optional<uint32_t> lineNumber_;
|
||||
std::optional<uint32_t> columnNumber_;
|
||||
};
|
||||
@@ -148,8 +158,13 @@ struct RuntimeSamplingProfile {
|
||||
std::vector<SampleCallStackFrame> callStack_;
|
||||
};
|
||||
|
||||
RuntimeSamplingProfile(std::string runtimeName, std::vector<Sample> samples)
|
||||
: runtimeName_(std::move(runtimeName)), samples_(std::move(samples)) {}
|
||||
RuntimeSamplingProfile(
|
||||
std::string runtimeName,
|
||||
std::vector<Sample> samples,
|
||||
std::unique_ptr<RawRuntimeProfile> rawRuntimeProfile)
|
||||
: runtimeName_(std::move(runtimeName)),
|
||||
samples_(std::move(samples)),
|
||||
rawRuntimeProfile_(std::move(rawRuntimeProfile)) {}
|
||||
|
||||
// Movable.
|
||||
RuntimeSamplingProfile& operator=(RuntimeSamplingProfile&&) = default;
|
||||
@@ -174,6 +189,11 @@ struct RuntimeSamplingProfile {
|
||||
std::string runtimeName_;
|
||||
/// List of recorded samples, should be chronologically sorted.
|
||||
std::vector<Sample> samples_;
|
||||
/// A unique pointer to the original raw runtime profile, collected from the
|
||||
/// runtime in RuntimeTargetDelegate. Keeping a pointer to the original
|
||||
/// profile allows it to remain alive as long as RuntimeSamplingProfile is
|
||||
/// alive, since it may be using the same std::string_view.
|
||||
std::unique_ptr<RawRuntimeProfile> rawRuntimeProfile_;
|
||||
};
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern::tracing
|
||||
+18
-10
@@ -5,8 +5,10 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "RuntimeSamplingProfileTraceEventSerializer.h"
|
||||
#include <string_view>
|
||||
|
||||
#include "ProfileTreeNode.h"
|
||||
#include "RuntimeSamplingProfileTraceEventSerializer.h"
|
||||
|
||||
namespace facebook::react::jsinspector_modern::tracing {
|
||||
|
||||
@@ -21,6 +23,11 @@ constexpr uint16_t PROFILE_ID = 1;
|
||||
/// by Chrome DevTools.
|
||||
constexpr uint32_t FALLBACK_SCRIPT_ID = 0;
|
||||
|
||||
constexpr std::string_view GARBAGE_COLLECTOR_FRAME_NAME = "(garbage collector)";
|
||||
constexpr std::string_view ROOT_FRAME_NAME = "(root)";
|
||||
constexpr std::string_view IDLE_FRAME_NAME = "(idle)";
|
||||
constexpr std::string_view PROGRAM_FRAME_NAME = "(program)";
|
||||
|
||||
uint64_t formatTimePointToUnixTimestamp(
|
||||
std::chrono::steady_clock::time_point timestamp) {
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
@@ -37,9 +44,10 @@ TraceEventProfileChunk::CPUProfile::Node convertToTraceEventProfileNode(
|
||||
node.getCodeType() == ProfileTreeNode::CodeType::JavaScript ? "JS"
|
||||
: "other",
|
||||
callFrame.getScriptId(),
|
||||
callFrame.getFunctionName(),
|
||||
callFrame.hasUrl() ? std::optional<std::string>(callFrame.getUrl())
|
||||
: std::nullopt,
|
||||
std::string(callFrame.getFunctionName()),
|
||||
callFrame.hasUrl()
|
||||
? std::optional<std::string>(std::string(callFrame.getUrl()))
|
||||
: std::nullopt,
|
||||
callFrame.hasLineNumber()
|
||||
? std::optional<uint32_t>(callFrame.getLineNumber())
|
||||
: std::nullopt,
|
||||
@@ -55,18 +63,18 @@ TraceEventProfileChunk::CPUProfile::Node convertToTraceEventProfileNode(
|
||||
}
|
||||
|
||||
RuntimeSamplingProfile::SampleCallStackFrame createArtificialCallFrame(
|
||||
std::string callFrameName) {
|
||||
std::string_view callFrameName) {
|
||||
return RuntimeSamplingProfile::SampleCallStackFrame{
|
||||
RuntimeSamplingProfile::SampleCallStackFrame::Kind::JSFunction,
|
||||
FALLBACK_SCRIPT_ID,
|
||||
std::move(callFrameName)};
|
||||
callFrameName};
|
||||
};
|
||||
|
||||
RuntimeSamplingProfile::SampleCallStackFrame createGarbageCollectorCallFrame() {
|
||||
return RuntimeSamplingProfile::SampleCallStackFrame{
|
||||
RuntimeSamplingProfile::SampleCallStackFrame::Kind::GarbageCollector,
|
||||
FALLBACK_SCRIPT_ID,
|
||||
"(garbage collector)"};
|
||||
GARBAGE_COLLECTOR_FRAME_NAME};
|
||||
};
|
||||
|
||||
class ProfileTreeRootNode : public ProfileTreeNode {
|
||||
@@ -75,7 +83,7 @@ class ProfileTreeRootNode : public ProfileTreeNode {
|
||||
: ProfileTreeNode(
|
||||
id,
|
||||
CodeType::Other,
|
||||
createArtificialCallFrame("(root)")) {}
|
||||
createArtificialCallFrame(ROOT_FRAME_NAME)) {}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -204,13 +212,13 @@ void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify(
|
||||
ProfileTreeNode* programNode = rootNode.addChild(
|
||||
nodeIdGenerator.getNext(),
|
||||
ProfileTreeNode::CodeType::Other,
|
||||
createArtificialCallFrame("(program)"));
|
||||
createArtificialCallFrame(PROGRAM_FRAME_NAME));
|
||||
chunk.nodes.push_back(*programNode);
|
||||
|
||||
ProfileTreeNode* idleNode = rootNode.addChild(
|
||||
nodeIdGenerator.getNext(),
|
||||
ProfileTreeNode::CodeType::Other,
|
||||
createArtificialCallFrame("(idle)"));
|
||||
createArtificialCallFrame(IDLE_FRAME_NAME));
|
||||
chunk.nodes.push_back(*idleNode);
|
||||
uint32_t idleNodeId = idleNode->getId();
|
||||
|
||||
|
||||
+6
-6
@@ -26,16 +26,16 @@ class RuntimeSamplingProfileTraceEventSerializerTest : public ::testing::Test {
|
||||
}
|
||||
|
||||
RuntimeSamplingProfile::SampleCallStackFrame createJSCallFrame(
|
||||
std::string functionName,
|
||||
std::string_view functionName,
|
||||
uint32_t scriptId = 1,
|
||||
std::optional<std::string> url = std::nullopt,
|
||||
std::optional<std::string_view> url = std::nullopt,
|
||||
std::optional<uint32_t> lineNumber = std::nullopt,
|
||||
std::optional<uint32_t> columnNumber = std::nullopt) {
|
||||
return RuntimeSamplingProfile::SampleCallStackFrame(
|
||||
RuntimeSamplingProfile::SampleCallStackFrame::Kind::JSFunction,
|
||||
scriptId,
|
||||
std::move(functionName),
|
||||
std::move(url),
|
||||
functionName,
|
||||
url,
|
||||
lineNumber,
|
||||
columnNumber);
|
||||
}
|
||||
@@ -55,12 +55,12 @@ class RuntimeSamplingProfileTraceEventSerializerTest : public ::testing::Test {
|
||||
}
|
||||
|
||||
RuntimeSamplingProfile createEmptyProfile() {
|
||||
return {"TestRuntime", {}};
|
||||
return {"TestRuntime", {}, {}};
|
||||
}
|
||||
|
||||
RuntimeSamplingProfile createProfileWithSamples(
|
||||
std::vector<RuntimeSamplingProfile::Sample> samples) {
|
||||
return {"TestRuntime", std::move(samples)};
|
||||
return {"TestRuntime", std::move(samples), {}};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user