Skip to content

Commit 9e9ac3c

Browse files
anonrigtargos
authored andcommitted
src: avoid copy by using std::views::keys
PR-URL: #56080 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 086cdc2 commit 9e9ac3c

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

src/node_builtins.cc

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,6 @@ Local<String> BuiltinLoader::GetConfigString(Isolate* isolate) {
8888
return config_.ToStringChecked(isolate);
8989
}
9090

91-
std::vector<std::string_view> BuiltinLoader::GetBuiltinIds() const {
92-
std::vector<std::string_view> ids;
93-
auto source = source_.read();
94-
ids.reserve(source->size());
95-
for (auto const& x : *source) {
96-
ids.emplace_back(x.first);
97-
}
98-
return ids;
99-
}
100-
10191
BuiltinLoader::BuiltinCategories BuiltinLoader::GetBuiltinCategories() const {
10292
BuiltinCategories builtin_categories;
10393

@@ -515,26 +505,25 @@ bool BuiltinLoader::CompileAllBuiltinsAndCopyCodeCache(
515505
Local<Context> context,
516506
const std::vector<std::string>& eager_builtins,
517507
std::vector<CodeCacheInfo>* out) {
518-
std::vector<std::string_view> ids = GetBuiltinIds();
508+
auto ids = GetBuiltinIds();
519509
bool all_succeeded = true;
520-
std::string v8_tools_prefix = "internal/deps/v8/tools/";
521-
std::string primordial_prefix = "internal/per_context/";
522-
std::string bootstrap_prefix = "internal/bootstrap/";
523-
std::string main_prefix = "internal/main/";
524-
to_eager_compile_ = std::unordered_set<std::string>(eager_builtins.begin(),
525-
eager_builtins.end());
510+
constexpr std::string_view v8_tools_prefix = "internal/deps/v8/tools/";
511+
constexpr std::string_view primordial_prefix = "internal/per_context/";
512+
constexpr std::string_view bootstrap_prefix = "internal/bootstrap/";
513+
constexpr std::string_view main_prefix = "internal/main/";
514+
to_eager_compile_ =
515+
std::unordered_set(eager_builtins.begin(), eager_builtins.end());
526516

527517
for (const auto& id : ids) {
528-
if (id.compare(0, v8_tools_prefix.size(), v8_tools_prefix) == 0) {
518+
if (id.starts_with(v8_tools_prefix)) {
529519
// No need to generate code cache for v8 scripts.
530520
continue;
531521
}
532522

533523
// Eagerly compile primordials/boostrap/main scripts during code cache
534524
// generation.
535-
if (id.compare(0, primordial_prefix.size(), primordial_prefix) == 0 ||
536-
id.compare(0, bootstrap_prefix.size(), bootstrap_prefix) == 0 ||
537-
id.compare(0, main_prefix.size(), main_prefix) == 0) {
525+
if (id.starts_with(primordial_prefix) || id.starts_with(bootstrap_prefix) ||
526+
id.starts_with(main_prefix)) {
538527
to_eager_compile_.emplace(id);
539528
}
540529

@@ -554,8 +543,8 @@ bool BuiltinLoader::CompileAllBuiltinsAndCopyCodeCache(
554543
}
555544

556545
RwLock::ScopedReadLock lock(code_cache_->mutex);
557-
for (auto const& item : code_cache_->map) {
558-
out->push_back({item.first, item.second});
546+
for (const auto& [id, data] : code_cache_->map) {
547+
out->push_back({id, data});
559548
}
560549
return all_succeeded;
561550
}
@@ -564,8 +553,8 @@ void BuiltinLoader::RefreshCodeCache(const std::vector<CodeCacheInfo>& in) {
564553
RwLock::ScopedLock lock(code_cache_->mutex);
565554
code_cache_->map.reserve(in.size());
566555
DCHECK(code_cache_->map.empty());
567-
for (auto const& item : in) {
568-
auto result = code_cache_->map.emplace(item.id, item.data);
556+
for (auto const& [id, data] : in) {
557+
auto result = code_cache_->map.emplace(id, data);
569558
USE(result.second);
570559
DCHECK(result.second);
571560
}
@@ -665,7 +654,7 @@ void BuiltinLoader::BuiltinIdsGetter(Local<Name> property,
665654
Environment* env = Environment::GetCurrent(info);
666655
Isolate* isolate = env->isolate();
667656

668-
std::vector<std::string_view> ids = env->builtin_loader()->GetBuiltinIds();
657+
auto ids = env->builtin_loader()->GetBuiltinIds();
669658
info.GetReturnValue().Set(
670659
ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked());
671660
}

src/node_builtins.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <map>
88
#include <memory>
99
#include <optional>
10+
#include <ranges>
1011
#include <string>
1112
#include <unordered_set>
1213
#include <vector>
@@ -126,7 +127,9 @@ class NODE_EXTERN_PRIVATE BuiltinLoader {
126127

127128
void CopySourceAndCodeCacheReferenceFrom(const BuiltinLoader* other);
128129

129-
std::vector<std::string_view> GetBuiltinIds() const;
130+
[[nodiscard]] auto GetBuiltinIds() const {
131+
return std::views::keys(*source_.read());
132+
}
130133

131134
void SetEagerCompile() { should_eager_compile_ = true; }
132135

src/util-inl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <cmath>
2828
#include <cstring>
2929
#include <locale>
30+
#include <ranges>
3031
#include <regex> // NOLINT(build/c++11)
3132
#include "node_revert.h"
3233
#include "util.h"
@@ -379,6 +380,25 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
379380
return set_js;
380381
}
381382

383+
template <typename T, std::size_t U>
384+
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
385+
const std::ranges::elements_view<T, U>& vec,
386+
v8::Isolate* isolate) {
387+
if (isolate == nullptr) isolate = context->GetIsolate();
388+
v8::EscapableHandleScope handle_scope(isolate);
389+
390+
MaybeStackBuffer<v8::Local<v8::Value>, 128> arr(vec.size());
391+
arr.SetLength(vec.size());
392+
auto it = vec.begin();
393+
for (size_t i = 0; i < vec.size(); ++i) {
394+
if (!ToV8Value(context, *it, isolate).ToLocal(&arr[i]))
395+
return v8::MaybeLocal<v8::Value>();
396+
std::advance(it, 1);
397+
}
398+
399+
return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length()));
400+
}
401+
382402
template <typename T, typename U>
383403
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
384404
const std::unordered_map<T, U>& map,

src/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <limits>
4343
#include <memory>
4444
#include <optional>
45+
#include <ranges>
4546
#include <set>
4647
#include <string>
4748
#include <string_view>
@@ -733,6 +734,12 @@ inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
733734
const std::unordered_map<T, U>& map,
734735
v8::Isolate* isolate = nullptr);
735736

737+
template <typename T, std::size_t U>
738+
inline v8::MaybeLocal<v8::Value> ToV8Value(
739+
v8::Local<v8::Context> context,
740+
const std::ranges::elements_view<T, U>& vec,
741+
v8::Isolate* isolate = nullptr);
742+
736743
// These macros expects a `Isolate* isolate` and a `Local<Context> context`
737744
// to be in the scope.
738745
#define READONLY_PROPERTY(obj, name, value) \

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy