Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit d4ced88

Browse files
jasnelladuh95
authored andcommitted
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: #65158 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent efb649e commit d4ced88

3 files changed

Lines changed: 93 additions & 35 deletions

File tree

β€Žsrc/permission/fs_permission.ccβ€Ž

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ void FreeRecursivelyNode(
3939
return;
4040
}
4141

42-
if (node->children.size()) {
43-
for (auto& c : node->children) {
44-
FreeRecursivelyNode(c.second);
45-
}
42+
for (auto& [label, child] : node->children) {
43+
FreeRecursivelyNode(child);
4644
}
4745

4846
delete node->wildcard_child;
@@ -106,7 +104,7 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
106104
node::DebugCategory::PERMISSION_MODEL, "%s%s\n", indent, node->prefix);
107105
}
108106

109-
if (node->children.size() > 0) {
107+
if (!node->children.empty()) {
110108
size_t count = 0;
111109
size_t total = node->children.size();
112110

@@ -120,10 +118,10 @@ void PrintTree(const node::permission::FSPermission::RadixTree::Node* node,
120118
}
121119
}
122120

123-
for (constauto& pair : node->children) {
121+
for (constauto& [label, child] : node->children) {
124122
count++;
125123
bool child_is_last = (count == total);
126-
PrintTree(pair.second, depth + 1, next_branch_prefix, child_is_last);
124+
PrintTree(child, depth + 1, next_branch_prefix, child_is_last);
127125
}
128126
}
129127
}
@@ -278,8 +276,8 @@ FSPermission::RadixTree::~RadixTree() {
278276
}
279277

280278
voidFSPermission::RadixTree::Clear() {
281-
for (auto& c : root_node_->children) {
282-
FreeRecursivelyNode(c.second);
279+
for (auto& [label, child] : root_node_->children) {
280+
FreeRecursivelyNode(child);
283281
}
284282
root_node_->children.clear();
285283
delete root_node_->wildcard_child;
@@ -294,15 +292,14 @@ bool FSPermission::RadixTree::Lookup(std::string_view s,
294292
return when_empty_return;
295293
}
296294
size_t parent_node_prefix_len = current_node->prefix.length();
297-
const std::string path(s);
298-
auto path_len = path.length();
295+
auto path_len = s.length();
299296

300297
while (true) {
301298
if (parent_node_prefix_len == path_len && current_node->IsEndNode()) {
302299
returntrue;
303300
}
304301

305-
auto node = current_node->NextNode(path, parent_node_prefix_len);
302+
auto node = current_node->NextNode(s, parent_node_prefix_len);
306303
if (node == nullptr) {
307304
returnfalse;
308305
}

β€Žsrc/permission/fs_permission.hβ€Ž

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include"v8.h"
77

8-
#include<unordered_map>
8+
#include<vector>
99
#include"permission/permission_base.h"
1010
#include"util.h"
1111

@@ -28,16 +28,30 @@ class FSPermission final : public PermissionBase {
2828
structRadixTree {
2929
structNode {
3030
std::string prefix;
31-
std::unordered_map<char, Node*> children;
32-
Node* wildcard_child;
33-
bool is_leaf;
31+
std::vector<std::pair<char, Node*>> children;
32+
Node* wildcard_child = nullptr;
33+
bool is_leaf = false;
3434

35-
explicitNode(conststd::string&pre)
36-
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
35+
explicitNode(std::string_viewpre)
36+
: prefix(pre) {}
3737

38-
Node() : wildcard_child(nullptr), is_leaf(false) {}
38+
Node() = default;
3939

40-
Node* CreateChild(const std::string& path_prefix) {
40+
Node* FindChild(char label) const {
41+
for (constauto& [c, node] : children) {
42+
if (c == label) return node;
43+
}
44+
returnnullptr;
45+
}
46+
47+
voidSetChild(char label, Node* node) {
48+
for (auto& [c, n] : children) {
49+
if (c == label) { n = node; return; }
50+
}
51+
children.emplace_back(label, node);
52+
}
53+
54+
Node* CreateChild(std::string_view path_prefix) {
4155
if (path_prefix.empty() && !is_leaf) {
4256
is_leaf = true;
4357
returnthis;
@@ -46,10 +60,11 @@ class FSPermission final : public PermissionBase {
4660
CHECK(!path_prefix.empty());
4761
char label = path_prefix[0];
4862

49-
Node* child = children[label];
63+
Node* child = FindChild(label);
5064
if (child == nullptr) {
51-
children[label] = newNode(path_prefix);
52-
return children[label];
65+
child = newNode(path_prefix);
66+
children.emplace_back(label, child);
67+
return child;
5368
}
5469
bool child_was_end_node = child->IsEndNode();
5570

@@ -58,13 +73,13 @@ class FSPermission final : public PermissionBase {
5873
size_t prefix_len = path_prefix.length();
5974
for (; i < child->prefix.length(); ++i) {
6075
if (i >= prefix_len || path_prefix[i] != child->prefix[i]) {
61-
std::string parent_prefix = child->prefix.substr(0, i);
62-
std::string child_prefix = child->prefix.substr(i);
76+
std::string parent_prefix(child->prefix.substr(0, i));
77+
std::string child_prefix(child->prefix.substr(i));
6378

6479
child->prefix = child_prefix;
6580
Node* split_child = newNode(parent_prefix);
66-
split_child->children[child_prefix[0]] = child;
67-
children[parent_prefix[0]] = split_child;
81+
split_child->children.emplace_back(child_prefix[0], child);
82+
SetChild(parent_prefix[0], split_child);
6883

6984
return split_child->CreateChild(path_prefix.substr(i));
7085
}
@@ -83,24 +98,23 @@ class FSPermission final : public PermissionBase {
8398
return wildcard_child;
8499
}
85100

86-
Node* NextNode(conststd::string& path, size_t idx) const {
101+
Node* NextNode(std::string_view path, size_t idx) const {
87102
if (idx >= path.length()) {
88103
returnnullptr;
89104
}
90105

91106
// wildcard node takes precedence
92107
if (children.size() > 1) {
93-
auto it = children.find('*');
94-
if (it != children.end()) {
95-
returnit->second;
108+
Node* wc = FindChild('*');
109+
if (wc != nullptr) {
110+
returnwc;
96111
}
97112
}
98113

99-
auto it = children.find(path[idx]);
100-
if (it == children.end()) {
114+
Node* child = FindChild(path[idx]);
115+
if (child == nullptr) {
101116
returnnullptr;
102117
}
103-
auto child = it->second;
104118
// match prefix
105119
size_t prefix_len = child->prefix.length();
106120
for (size_t i = 0; i < path.length(); ++i) {

β€Žsrc/permission/permission.ccβ€Ž

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include"env-inl.h"
44
#include"memory_tracker-inl.h"
55
#include"node.h"
6+
#include"node_debug.h"
67
#include"node_diagnostics_channel.h"
78
#include"node_errors.h"
89
#include"node_external_reference.h"
910
#include"node_file.h"
1011

1112
#include"permission/permission_base.h"
13+
#include"v8-fast-api-calls.h"
1214
#include"v8-template.h"
1315
#include"v8.h"
1416

@@ -18,13 +20,16 @@
1820

1921
namespacenode {
2022

23+
using v8::CFunction;
2124
using v8::Context;
2225
using v8::DictionaryTemplate;
26+
using v8::FastApiCallbackOptions;
2327
using v8::FunctionCallbackInfo;
2428
using v8::IntegrityLevel;
2529
using v8::Local;
2630
using v8::MaybeLocal;
2731
using v8::Object;
32+
using v8::String;
2833
using v8::Undefined;
2934
using v8::Value;
3035

@@ -121,6 +126,47 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
121126
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
122127
}
123128

129+
staticboolFastHas(Local<Value> receiver,
130+
Local<Value> scope_arg,
131+
Local<Value> resource_arg,
132+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
133+
FastApiCallbackOptions& options) {
134+
TRACK_V8_FAST_API_CALL("permission.has");
135+
auto isolate = options.isolate;
136+
v8::HandleScope handle_scope(isolate);
137+
auto context = isolate->GetCurrentContext();
138+
139+
Environment* env = Environment::GetCurrent(context);
140+
141+
Local<String> str;
142+
if (!scope_arg->ToString(context).ToLocal(&str)) {
143+
returnfalse;
144+
}
145+
Utf8Value utf8_scope(isolate, str);
146+
PermissionScope scope =
147+
Permission::StringToPermission(utf8_scope.ToStringView());
148+
if (scope == PermissionScope::kPermissionsRoot) {
149+
returnfalse;
150+
}
151+
152+
if (resource_arg->IsUndefined()) {
153+
return env->permission()->is_granted(env, scope);
154+
}
155+
156+
Local<String> res_str;
157+
if (!resource_arg->ToString(context).ToLocal(&res_str)) {
158+
returnfalse;
159+
}
160+
Utf8Value utf8_res(isolate, res_str);
161+
if (utf8_res.length() == 0) {
162+
returnfalse;
163+
}
164+
165+
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
166+
}
167+
168+
static CFunction fast_has_(CFunction::Make(FastHas));
169+
124170
} // namespace
125171

126172
#defineV(Name, label, _, __) \
@@ -349,14 +395,15 @@ void Initialize(Local<Object> target,
349395
Local<Value> unused,
350396
Local<Context> context,
351397
void* priv) {
352-
SetMethodNoSideEffect(context, target, "has", Has);
398+
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
353399
SetMethod(context, target, "drop", Drop);
354400

355401
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
356402
}
357403

358404
voidRegisterExternalReferences(ExternalReferenceRegistry* registry) {
359405
registry->Register(Has);
406+
registry->Register(fast_has_);
360407
registry->Register(Drop);
361408
}
362409

0 commit comments

Comments
Β (0)