Skip to content

Commit 1b811f6

Browse files
committed
src: make minor improvements to EnabledDebugList
Change the underlying type of DebugCategory to unsigned int so that it can be safely cast to an unsigned type without having to worry about negative values, removing the need for the DCHECK_GE statements. To fully benefit from type safety, remove DebugCategory::CATEGORY_COUNT and instead add a constexpr kDebugCategoryCount. Change the list of enabled categories from a bool array to a std::bitset, which is slightly more space-efficient. Use std::bitset::set() to enable categories, removing the need for the DCHECK_LT in EnabledDebugList::set_enabled(). Note: intentionally do not use std::bitset::test() in EnabledDebugList::enabled() because it might be in a hot path and, unlike DCHECK_LT, std::bitset::test() may incur a runtime cost. Remove the second argument from EnabledDebugList::set_enabled() and EnabledDebugList::Parse() because it was always set to true.
1 parent a5671e2 commit 1b811f6

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/debug_utils.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ void EnabledDebugList::Parse(std::shared_ptr<KVStore> env_vars,
6363
v8::Isolate* isolate) {
6464
std::string cats;
6565
credentials::SafeGetenv("NODE_DEBUG_NATIVE", &cats, env_vars, isolate);
66-
Parse(cats, true);
66+
Parse(cats);
6767
}
6868

69-
void EnabledDebugList::Parse(const std::string& cats, bool enabled) {
69+
void EnabledDebugList::Parse(const std::string& cats) {
7070
std::string debug_categories = cats;
7171
while (!debug_categories.empty()) {
7272
std::string::size_type comma_pos = debug_categories.find(',');
@@ -76,7 +76,7 @@ void EnabledDebugList::Parse(const std::string& cats, bool enabled) {
7676
{ \
7777
static const std::string available_category = ToLower(#name); \
7878
if (available_category.find(wanted) != std::string::npos) \
79-
set_enabled(DebugCategory::name, enabled); \
79+
set_enabled(DebugCategory::name); \
8080
}
8181

8282
DEBUG_CATEGORY_NAMES(V)

src/debug_utils.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "util.h"
88

99
#include <algorithm>
10+
#include <bitset>
1011
#include <sstream>
1112
#include <string>
1213

@@ -51,20 +52,21 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
5152
V(WASI) \
5253
V(MKSNAPSHOT)
5354

54-
enum class DebugCategory {
55+
enum class DebugCategory : unsigned int {
5556
#define V(name) name,
5657
DEBUG_CATEGORY_NAMES(V)
5758
#undef V
58-
CATEGORY_COUNT
5959
};
6060

61+
#define V(name) +1
62+
constexpr unsigned int kDebugCategoryCount = DEBUG_CATEGORY_NAMES(V);
63+
#undef V
64+
6165
class NODE_EXTERN_PRIVATE EnabledDebugList {
6266
public:
6367
bool enabled(DebugCategory category) const {
64-
DCHECK_GE(static_cast<int>(category), 0);
65-
DCHECK_LT(static_cast<int>(category),
66-
static_cast<int>(DebugCategory::CATEGORY_COUNT));
67-
return enabled_[static_cast<int>(category)];
68+
DCHECK_LT(static_cast<unsigned int>(category), kDebugCategoryCount);
69+
return enabled_[static_cast<unsigned int>(category)];
6870
}
6971

7072
// Uses NODE_DEBUG_NATIVE to initialize the categories. The env_vars variable
@@ -75,15 +77,12 @@ class NODE_EXTERN_PRIVATE EnabledDebugList {
7577

7678
private:
7779
// Set all categories matching cats to the value of enabled.
78-
void Parse(const std::string& cats, bool enabled);
79-
void set_enabled(DebugCategory category, bool enabled) {
80-
DCHECK_GE(static_cast<int>(category), 0);
81-
DCHECK_LT(static_cast<int>(category),
82-
static_cast<int>(DebugCategory::CATEGORY_COUNT));
83-
enabled_[static_cast<int>(category)] = true;
80+
void Parse(const std::string& cats);
81+
void set_enabled(DebugCategory category) {
82+
enabled_.set(static_cast<unsigned int>(category));
8483
}
8584

86-
bool enabled_[static_cast<int>(DebugCategory::CATEGORY_COUNT)] = {false};
85+
std::bitset<kDebugCategoryCount> enabled_;
8786
};
8887

8988
template <typename... Args>

0 commit comments

Comments
 (0)