src: apply a modest performance perf to permissions

Improve the way the RadixTree works and apply a
fast api call.

Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65158
Reviewed-By: Xuguang Mei <meixuguang@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
James M Snell committed 2026-08-12 08:58:36 -07:00
1 parent 14a4f3aacb
commit 9e733fa232
3 files changed
+93 -35

No files matched your search

+9 -12
View File
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
return;
}
if (node->children.size()) {
for (auto& c : node->children) {
FreeRecursivelyNode(c.second);
}
for (auto& [label, child] : node->children) {
FreeRecursivelyNode(child);
}
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
}
if (node->children.size() > 0) {
if (!node->children.empty()) {
size_t count = 0;
size_t total = node->children.size();
@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
}
}
for (const auto& pair : node->children) {
for (const auto& [label, child] : node->children) {
count++;
bool child_is_last = (count == total);
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
}
}
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
}
void FSPermission::RadixTree::Clear() {
for (auto& c : root_node_->children) {
FreeRecursivelyNode(c.second);
for (auto& [label, child] : root_node_->children) {
FreeRecursivelyNode(child);
}
root_node_->children.clear();
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
return when_empty_return;
}
size_t parent_node_prefix_len = current_node->prefix.length();
const std::string path(s);
auto path_len = path.length();
auto path_len = s.length();
while (true) {
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
return true;
}
auto node = current_node->NextNode(path, parent_node_prefix_len);
auto node = current_node->NextNode(s, parent_node_prefix_len);
if (node == nullptr) {
return false;
}
+36 -22
View File
@@ -5,7 +5,7 @@
#include "v8.h"
#include <unordered_map>
#include <vector>
#include "permission/permission_base.h"
#include "util.h"
@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
struct RadixTree {
struct Node {
std::string prefix;
std::unordered_map<char, Node*> children;
Node* wildcard_child;
bool is_leaf;
std::vector<std::pair<char, Node*>> children;
Node* wildcard_child = nullptr;
bool is_leaf = false;
explicit Node(const std::string& pre)
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
explicit Node(std::string_view pre)
: prefix(pre) {}
Node() : wildcard_child(nullptr), is_leaf(false) {}
Node() = default;
Node* CreateChild(const std::string& path_prefix) {
Node* FindChild(char label) const {
for (const auto& [c, node] : children) {
if (c == label) return node;
}
return nullptr;
}
void SetChild(char label, Node* node) {
for (auto& [c, n] : children) {
if (c == label) { n = node; return; }
}
children.emplace_back(label, node);
}
Node* CreateChild(std::string_view path_prefix) {
if (path_prefix.empty() && !is_leaf) {
is_leaf = true;
return this;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
CHECK(!path_prefix.empty());
char label = path_prefix[0];
Node* child = children[label];
Node* child = FindChild(label);
if (child == nullptr) {
children[label] = new Node(path_prefix);
return children[label];
child = new Node(path_prefix);
children.emplace_back(label, child);
return child;
}
bool child_was_end_node = child->IsEndNode();
@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
size_t prefix_len = path_prefix.length();
for (; i < child->prefix.length(); ++i) {
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
std::string parent_prefix = child->prefix.substr(0, i);
std::string child_prefix = child->prefix.substr(i);
std::string parent_prefix(child->prefix.substr(0, i));
std::string child_prefix(child->prefix.substr(i));
child->prefix = child_prefix;
Node* split_child = new Node(parent_prefix);
split_child->children[child_prefix[0]] = child;
children[parent_prefix[0]] = split_child;
split_child->children.emplace_back(child_prefix[0], child);
SetChild(parent_prefix[0], split_child);
return split_child->CreateChild(path_prefix.substr(i));
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
return wildcard_child;
}
Node* NextNode(const std::string& path, size_t idx) const {
Node* NextNode(std::string_view path, size_t idx) const {
if (idx >= path.length()) {
return nullptr;
}
// wildcard node takes precedence
if (children.size() > 1) {
auto it = children.find('*');
if (it != children.end()) {
return it->second;
Node* wc = FindChild('*');
if (wc != nullptr) {
return wc;
}
}
auto it = children.find(path[idx]);
if (it == children.end()) {
Node* child = FindChild(path[idx]);
if (child == nullptr) {
return nullptr;
}
auto child = it->second;
// match prefix
size_t prefix_len = child->prefix.length();
for (size_t i = 0; i < path.length(); ++i) {
+48 -1
View File
@@ -3,12 +3,14 @@
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_debug.h"
#include "node_diagnostics_channel.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_file.h"
#include "permission/permission_base.h"
#include "v8-fast-api-calls.h"
#include "v8-template.h"
#include "v8.h"
@@ -18,13 +20,16 @@
namespace node {
using v8::CFunction;
using v8::Context;
using v8::DictionaryTemplate;
using v8::FastApiCallbackOptions;
using v8::FunctionCallbackInfo;
using v8::IntegrityLevel;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Undefined;
using v8::Value;
@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
}
static bool FastHas(Local<Value> receiver,
Local<Value> scope_arg,
Local<Value> resource_arg,
// NOLINTNEXTLINE(runtime/references) This is V8 api.
FastApiCallbackOptions& options) {
TRACK_V8_FAST_API_CALL("permission.has");
auto isolate = options.isolate;
v8::HandleScope handle_scope(isolate);
auto context = isolate->GetCurrentContext();
Environment* env = Environment::GetCurrent(context);
Local<String> str;
if (!scope_arg->ToString(context).ToLocal(&str)) {
return false;
}
Utf8Value utf8_scope(isolate, str);
PermissionScope scope =
Permission::StringToPermission(utf8_scope.ToStringView());
if (scope == PermissionScope::kPermissionsRoot) {
return false;
}
if (resource_arg->IsUndefined()) {
return env->permission()->is_granted(env, scope);
}
Local<String> res_str;
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
return false;
}
Utf8Value utf8_res(isolate, res_str);
if (utf8_res.length() == 0) {
return false;
}
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
}
static CFunction fast_has_(CFunction::Make(FastHas));
} // namespace
#define V(Name, label, _, __) \
@@ -349,7 +395,7 @@ void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
SetMethodNoSideEffect(context, target, "has", Has);
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
SetMethod(context, target, "drop", Drop);
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
@@ -357,6 +403,7 @@ void Initialize(Local<Object> target,
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Has);
registry->Register(fast_has_);
registry->Register(Drop);
}