Skip to content

src: use FastStringKey for TrackV8FastApiCall #59148

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/node_debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ using v8::Number;
using v8::Object;
using v8::Value;

thread_local std::unordered_map<std::string_view, int> v8_fast_api_call_counts;
thread_local std::unordered_map<FastStringKey, int, FastStringKey::Hash>
v8_fast_api_call_counts;

void TrackV8FastApiCall(std::string_view key) {
void TrackV8FastApiCall(FastStringKey key) {
v8_fast_api_call_counts[key]++;
}

int GetV8FastApiCallCount(std::string_view key) {
int GetV8FastApiCallCount(FastStringKey key) {
return v8_fast_api_call_counts[key];
}

Expand All @@ -40,7 +41,8 @@ void GetV8FastApiCallCount(const FunctionCallbackInfo<Value>& args) {
return;
}
Utf8Value utf8_key(env->isolate(), args[0]);
args.GetReturnValue().Set(GetV8FastApiCallCount(utf8_key.ToStringView()));
args.GetReturnValue().Set(GetV8FastApiCallCount(
FastStringKey::AllowDynamic(utf8_key.ToStringView())));
}

void SlowIsEven(const FunctionCallbackInfo<Value>& args) {
Expand Down
9 changes: 5 additions & 4 deletions src/node_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#ifdef DEBUG
#include <string_view>
#include "util.h"
#endif // DEBUG

namespace node {
namespace debug {

#ifdef DEBUG
void TrackV8FastApiCall(std::string_view key);
int GetV8FastApiCallCount(std::string_view key);
void TrackV8FastApiCall(FastStringKey key);
int GetV8FastApiCallCount(FastStringKey key);

#define TRACK_V8_FAST_API_CALL(key) node::debug::TrackV8FastApiCall(key)
#define TRACK_V8_FAST_API_CALL(key) \
node::debug::TrackV8FastApiCall(FastStringKey(key))
#else // !DEBUG
#define TRACK_V8_FAST_API_CALL(key)
#endif // DEBUG
Expand Down
9 changes: 8 additions & 1 deletion src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,14 @@ constexpr bool FastStringKey::operator==(const FastStringKey& other) const {
return name_ == other.name_;
}

constexpr FastStringKey::FastStringKey(std::string_view name)
consteval FastStringKey::FastStringKey(std::string_view name)
: FastStringKey(name, 0) {}

constexpr FastStringKey FastStringKey::AllowDynamic(std::string_view name) {
return FastStringKey(name, 0);
}

constexpr FastStringKey::FastStringKey(std::string_view name, int dummy)
: name_(name), cached_hash_(HashImpl(name)) {}

constexpr std::string_view FastStringKey::as_string_view() const {
Expand Down
8 changes: 7 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,11 @@ class PersistentToLocal {
// computations.
class FastStringKey {
public:
constexpr explicit FastStringKey(std::string_view name);
// consteval ensures that the argument is a compile-time constant.
consteval explicit FastStringKey(std::string_view name);
// passing something that is not a compile-time constant needs explicit
// opt-in via this helper, as it defeats the purpose of FastStringKey.
static constexpr FastStringKey AllowDynamic(std::string_view name);

struct Hash {
constexpr size_t operator()(const FastStringKey& key) const;
Expand All @@ -832,6 +836,8 @@ class FastStringKey {
constexpr std::string_view as_string_view() const;

private:
constexpr explicit FastStringKey(std::string_view name, int dummy);

static constexpr size_t HashImpl(std::string_view str);

const std::string_view name_;
Expand Down
Loading