Commit efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

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 efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

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 efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

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 efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

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 efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

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 efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

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 efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

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 efb649e

Browse files
jasnelladuh95
authored andcommitted
src: make permission storage a bit more efficient
Use a fixed array rather than an unordered list 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 521aaf1 commit efb649e

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,53 +141,49 @@ PermissionScope Permission::StringToPermission(std::string_view perm) {
141141
#undef V
142142

143143
Permission::Permission() : enabled_(false), warning_only_(false) {
144-
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
145-
std::shared_ptr<PermissionBase> child_p =
146-
std::make_shared<ChildProcessPermission>();
147-
std::shared_ptr<PermissionBase> worker_t =
148-
std::make_shared<WorkerPermission>();
149-
std::shared_ptr<PermissionBase> inspector =
150-
std::make_shared<InspectorPermission>();
151-
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
152-
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
153-
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
154-
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
155-
std::shared_ptr<PermissionBase> openssl_store =
156-
std::make_shared<OpenSSLStorePermission>();
144+
auto fs = std::make_shared<FSPermission>();
145+
auto child_p = std::make_shared<ChildProcessPermission>();
146+
autoworker_t = std::make_shared<WorkerPermission>();
147+
auto inspector = std::make_shared<InspectorPermission>();
148+
auto wasi = std::make_shared<WASIPermission>();
149+
auto net = std::make_shared<NetPermission>();
150+
auto addon = std::make_shared<AddonPermission>();
151+
auto ffi = std::make_shared<FFIPermission>();
152+
auto openssl_store = std::make_shared<OpenSSLStorePermission>();
157153
#defineV(Name, _, __, ___) \
158-
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
154+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = fs;
159155
FILESYSTEM_PERMISSIONS(V)
160156
#undef V
161157
#defineV(Name, _, __, ___) \
162-
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
158+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = child_p;
163159
CHILD_PROCESS_PERMISSIONS(V)
164160
#undef V
165161
#defineV(Name, _, __, ___) \
166-
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
162+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = worker_t;
167163
WORKER_THREADS_PERMISSIONS(V)
168164
#undef V
169165
#defineV(Name, _, __, ___) \
170-
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
166+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = inspector;
171167
INSPECTOR_PERMISSIONS(V)
172168
#undef V
173169
#defineV(Name, _, __, ___) \
174-
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
170+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = wasi;
175171
WASI_PERMISSIONS(V)
176172
#undef V
177173
#defineV(Name, _, __, ___) \
178-
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
174+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = net;
179175
NET_PERMISSIONS(V)
180176
#undef V
181177
#defineV(Name, _, __, ___) \
182-
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
178+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = addon;
183179
ADDON_PERMISSIONS(V)
184180
#undef V
185181
#defineV(Name, _, __, ___) \
186-
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
182+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = ffi;
187183
FFI_PERMISSIONS(V)
188184
#undef V
189185
#defineV(Name, _, __, ___) \
190-
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
186+
nodes_[static_cast<size_t>(PermissionScope::k##Name)] = openssl_store;
191187
OPENSSL_STORE_PERMISSIONS(V)
192188
#undef V
193189
}
@@ -264,10 +260,10 @@ bool Permission::is_scope_granted(Environment* env,
264260
std::string_view res) const {
265261
CHECK(permission != PermissionScope::kPermissionsRoot &&
266262
permission != PermissionScope::kPermissionsCount);
267-
auto perm_node = nodes_.find(permission);
263+
auto& perm_node = nodes_[static_cast<size_t>(permission)];
268264
bool result = false;
269-
if (perm_node != nodes_.end()) {
270-
result = perm_node->second->is_granted(env, permission, res);
265+
if (perm_node) {
266+
result = perm_node->is_granted(env, permission, res);
271267
}
272268

273269
if (!result && !publishing_) {
@@ -296,17 +292,13 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
296292
Environment* env, PermissionScope scope) const {
297293
CHECK(scope != PermissionScope::kPermissionsRoot &&
298294
scope != PermissionScope::kPermissionsCount);
299-
auto it = channels_.find(scope);
300-
if (it != channels_.end()) {
301-
// Promote weak ref to strong for the duration of this call.
302-
BaseObjectPtr<diagnostics_channel::Channel> ptr(it->second.get());
303-
if (ptr) return ptr;
304-
channels_.erase(it);
305-
}
295+
auto& weak_ch = channels_[static_cast<size_t>(scope)];
296+
// Promote weak ref to strong for the duration of this call.
297+
BaseObjectPtr<diagnostics_channel::Channel> ptr(weak_ch.get());
298+
if (ptr) return ptr;
306299
auto channel_name = GetDiagnosticsChannelName(scope);
307300
if (auto ch = diagnostics_channel::Channel::Get(env, channel_name)) {
308-
channels_.emplace(scope,
309-
BaseObjectWeakPtr<diagnostics_channel::Channel>(ch));
301+
weak_ch = BaseObjectWeakPtr<diagnostics_channel::Channel>(ch.get());
310302
return ch;
311303
}
312304
return {};
@@ -315,9 +307,9 @@ BaseObjectPtr<diagnostics_channel::Channel> Permission::GetOrCreateChannel(
315307
voidPermission::Apply(Environment* env,
316308
std::span<const std::string> allow,
317309
PermissionScope scope) {
318-
auto permission = nodes_.find(scope);
319-
if (permission != nodes_.end()) {
320-
permission->second->Apply(env, allow, scope);
310+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
311+
if (perm_node) {
312+
perm_node->Apply(env, allow, scope);
321313
}
322314
}
323315

@@ -326,9 +318,9 @@ void Permission::Drop(Environment* env,
326318
std::string_view param) {
327319
CHECK(scope != PermissionScope::kPermissionsRoot &&
328320
scope != PermissionScope::kPermissionsCount);
329-
auto permission = nodes_.find(scope);
330-
if (permission != nodes_.end()) {
331-
permission->second->Drop(env, scope, param);
321+
auto& perm_node = nodes_[static_cast<size_t>(scope)];
322+
if (perm_node) {
323+
perm_node->Drop(env, scope, param);
332324
}
333325

334326
// Publish to diagnostics channel so observers can track drops

‎src/permission/permission.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include"permission/worker_permission.h"
1919
#include"v8.h"
2020

21+
#include<array>
2122
#include<string_view>
22-
#include<unordered_map>
2323

2424
namespacenode {
2525

@@ -140,14 +140,17 @@ class Permission {
140140
BaseObjectPtr<diagnostics_channel::Channel> GetOrCreateChannel(
141141
Environment* env, PermissionScope scope) const;
142142

143-
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
143+
staticconstexprsize_tkPermissionCount =
144+
static_cast<size_t>(PermissionScope::kPermissionsCount);
145+
146+
std::array<std::shared_ptr<PermissionBase>, kPermissionCount> nodes_;
144147
bool enabled_;
145148
bool warning_only_;
146149
mutablebool publishing_ = false;
147150
// Weak refs: BindingData (via BaseObjectPtr) is the sole owner of Channels.
148151
// Using weak refs here avoids keeping Channels alive past Realm teardown.
149-
mutable std::unordered_map<PermissionScope,
150-
BaseObjectWeakPtr<diagnostics_channel::Channel>>
152+
mutable std::array<BaseObjectWeakPtr<diagnostics_channel::Channel>,
153+
kPermissionCount>
151154
channels_;
152155
};
153156

0 commit comments

Comments
 (0)