From 21096c40bb64552177fc11cf4317b912f7570eb7 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 25 Apr 2025 14:45:40 -0400 Subject: [PATCH 1/2] src: use ranges library (C++20) more systematically --- src/cleanup_queue.cc | 3 +-- src/crypto/crypto_util.cc | 2 +- src/inspector_socket.cc | 3 ++- src/inspector_socket_server.cc | 2 +- src/node.cc | 9 ++++----- src/node_credentials.cc | 2 +- src/node_dotenv.cc | 2 +- src/node_http2.h | 3 +-- src/node_messaging.cc | 8 +++----- src/node_platform.cc | 3 +-- src/node_process_object.cc | 3 +-- src/node_report.cc | 6 +++--- src/node_sea.cc | 4 ++-- src/node_task_runner.cc | 2 +- src/node_watchdog.cc | 2 +- test/cctest/test_inspector_socket_server.cc | 2 +- 16 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/cleanup_queue.cc b/src/cleanup_queue.cc index 5923db6b89d4b4..52fdca92575fad 100644 --- a/src/cleanup_queue.cc +++ b/src/cleanup_queue.cc @@ -13,8 +13,7 @@ std::vector CleanupQueue::GetOrdered() // We can't erase the copied elements from `cleanup_hooks_` yet, because we // need to be able to check whether they were un-scheduled by another hook. - std::sort(callbacks.begin(), - callbacks.end(), + std::ranges::sort(callbacks, [](const CleanupHookCallback& a, const CleanupHookCallback& b) { // Sort in descending order so that the most recently inserted // callbacks are run first. diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index 975345fbef3be2..448c3d70376b86 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -216,7 +216,7 @@ void CryptoErrorStore::Capture() { ERR_error_string_n(err, buf, sizeof(buf)); errors_.emplace_back(buf); } - std::reverse(std::begin(errors_), std::end(errors_)); + std::ranges::reverse(errors_); } bool CryptoErrorStore::Empty() const { diff --git a/src/inspector_socket.cc b/src/inspector_socket.cc index 5246b9170a7a78..788a3aeb65df69 100644 --- a/src/inspector_socket.cc +++ b/src/inspector_socket.cc @@ -10,6 +10,7 @@ #include #include #include +#include #define ACCEPT_KEY_LENGTH nbytes::Base64EncodedSize(20) @@ -205,7 +206,7 @@ static bool IsIPAddress(const std::string& host) { // (other than ::/128) that represent non-routable IPv4 addresses. However, // this translation assumes that the host is interpreted as an IPv6 address // in the first place, at which point DNS rebinding should not be an issue. - if (std::all_of(ipv6, ipv6 + sizeof(ipv6), [](auto b) { return b == 0; })) { + if (std::ranges::all_of(ipv6, [](auto b) { return b == 0; })) { return false; } diff --git a/src/inspector_socket_server.cc b/src/inspector_socket_server.cc index f8f6a1b4ab6bd0..4df85fe8461bae 100644 --- a/src/inspector_socket_server.cc +++ b/src/inspector_socket_server.cc @@ -451,7 +451,7 @@ void InspectorSocketServer::TerminateConnections() { bool InspectorSocketServer::TargetExists(const std::string& id) { const std::vector& target_ids = delegate_->GetTargetIds(); - const auto& found = std::find(target_ids.begin(), target_ids.end(), id); + const auto& found = std::ranges::find(target_ids, id); return found != target_ids.end(); } diff --git a/src/node.cc b/src/node.cc index 9f41dc84340ae0..e7bd4f4ae76667 100644 --- a/src/node.cc +++ b/src/node.cc @@ -758,16 +758,15 @@ static ExitCode ProcessGlobalArgsInternal(std::vector* args, // TODO(aduh95): remove this when the harmony-import-attributes flag // is removed in V8. - if (std::find(v8_args.begin(), - v8_args.end(), + if (std::ranges::find(v8_args, "--no-harmony-import-attributes") == v8_args.end()) { v8_args.emplace_back("--harmony-import-attributes"); } auto env_opts = per_process::cli_options->per_isolate->per_env; - if (std::find(v8_args.begin(), v8_args.end(), + if (std::ranges::find(v8_args, "--abort-on-uncaught-exception") != v8_args.end() || - std::find(v8_args.begin(), v8_args.end(), + std::ranges::find(v8_args, "--abort_on_uncaught_exception") != v8_args.end()) { env_opts->abort_on_uncaught_exception = true; } @@ -780,7 +779,7 @@ static ExitCode ProcessGlobalArgsInternal(std::vector* args, // Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the // performance penalty of frequent EINTR wakeups when the profiler is running. // Only do this for v8.log profiling, as it breaks v8::CpuProfiler users. - if (std::find(v8_args.begin(), v8_args.end(), "--prof") != v8_args.end()) { + if (std::ranges::find(v8_args, "--prof") != v8_args.end()) { uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF); } #endif diff --git a/src/node_credentials.cc b/src/node_credentials.cc index f86c460f6a4b85..b2df695e7a60f1 100644 --- a/src/node_credentials.cc +++ b/src/node_credentials.cc @@ -385,7 +385,7 @@ static void GetGroups(const FunctionCallbackInfo& args) { groups.resize(ngroups); gid_t egid = getegid(); - if (std::find(groups.begin(), groups.end(), egid) == groups.end()) + if (std::ranges::find(groups, egid) == groups.end()) groups.push_back(egid); Local result; if (ToV8Value(env->context(), groups).ToLocal(&result)) { diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index d5f14fa92e2694..4f9b34a3298842 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -28,7 +28,7 @@ std::vector Dotenv::GetDataFromArgs( std::vector env_files; // This will be an iterator, pointing to args.end() if no matches are found - auto matched_arg = std::find_if(args.begin(), args.end(), find_match); + auto matched_arg = std::ranges::find_if(args, find_match); while (matched_arg != args.end()) { if (*matched_arg == "--") { diff --git a/src/node_http2.h b/src/node_http2.h index a60a7ba029db3e..4907899dea2124 100644 --- a/src/node_http2.h +++ b/src/node_http2.h @@ -692,8 +692,7 @@ class Http2Session : public AsyncWrap, bool has_pending_rststream(int32_t stream_id) { return pending_rst_streams_.end() != - std::find(pending_rst_streams_.begin(), - pending_rst_streams_.end(), + std::ranges::find(pending_rst_streams_, stream_id); } diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 30987ca04be952..f2c2e8f0078a41 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -414,8 +414,7 @@ class SerializerDelegate : public ValueSerializer::Delegate { if (!host_objects_[i]->NestedTransferables().To(&nested_transferables)) return Nothing(); for (auto& nested_transferable : nested_transferables) { - if (std::find(host_objects_.begin(), - host_objects_.end(), + if (std::ranges::find(host_objects_, nested_transferable) == host_objects_.end()) { AddHostObject(nested_transferable); } @@ -517,7 +516,7 @@ Maybe Message::Serialize(Environment* env, ThrowDataCloneException(context, env->transfer_unsupported_type_str()); return Nothing(); } - if (std::find(array_buffers.begin(), array_buffers.end(), ab) != + if (std::ranges::find(array_buffers, ab) != array_buffers.end()) { ThrowDataCloneException( context, @@ -564,8 +563,7 @@ Maybe Message::Serialize(Environment* env, "MessagePort in transfer list is already detached")); return Nothing(); } - if (std::find(delegate.host_objects_.begin(), - delegate.host_objects_.end(), + if (std::ranges::find(delegate.host_objects_, host_object) != delegate.host_objects_.end()) { ThrowDataCloneException( context, diff --git a/src/node_platform.cc b/src/node_platform.cc index 3aab5196a2bccc..a4448fb71ceb28 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -450,8 +450,7 @@ void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr task) { } void PerIsolatePlatformData::DeleteFromScheduledTasks(DelayedTask* task) { - auto it = std::find_if(scheduled_delayed_tasks_.begin(), - scheduled_delayed_tasks_.end(), + auto it = std::ranges::find_if(scheduled_delayed_tasks_, [task](const DelayedTaskPointer& delayed) -> bool { return delayed.get() == task; }); diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 3f1b276d10bdd0..9917c9de7cb976 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -97,8 +97,7 @@ static void SetVersions(Isolate* isolate, Local versions) { NODE_VERSIONS_KEYS(V) #undef V - std::sort(&versions_array[0], - &versions_array[arraysize(versions_array)], + std::ranges::sort(versions_array, [](auto& a, auto& b) { return a.first < b.first; }); for (const auto& version : versions_array) { diff --git a/src/node_report.cc b/src/node_report.cc index c64fc1a2d5f08f..3f66f90a4e0cd5 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -22,6 +22,7 @@ #include #include #include +#include constexpr int NODE_REPORT_VERSION = 5; constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000; @@ -537,7 +538,7 @@ static void PrintJavaScriptErrorStack(JSONWriter* writer, line = ss.find('\n'); while (line != -1) { l = ss.substr(0, line); - l.erase(l.begin(), std::find_if(l.begin(), l.end(), [](int ch) { + l.erase(l.begin(), std::ranges::find_if(l, [](int ch) { return !std::iswspace(ch); })); writer->json_element(l); @@ -811,8 +812,7 @@ static void PrintComponentVersions(JSONWriter* writer) { NODE_VERSIONS_KEYS(V) #undef V - std::sort(&versions_array[0], - &versions_array[arraysize(versions_array)], + std::ranges::sort(versions_array, [](auto& a, auto& b) { return a.first < b.first; }); for (const auto& version : versions_array) { diff --git a/src/node_sea.cc b/src/node_sea.cc index 1f7f3c8a707f4e..515f728a8ca82a 100644 --- a/src/node_sea.cc +++ b/src/node_sea.cc @@ -399,8 +399,8 @@ ExitCode GenerateSnapshotForSEA(const SeaConfig& config, return exit_code; } auto& persistents = snapshot.env_info.principal_realm.persistent_values; - auto it = std::find_if( - persistents.begin(), persistents.end(), [](const PropInfo& prop) { + auto it = std::ranges::find_if( + persistents, [](const PropInfo& prop) { return prop.name == "snapshot_deserialize_main"; }); if (it == persistents.end()) { diff --git a/src/node_task_runner.cc b/src/node_task_runner.cc index efaf6f01b3fbf9..08cd5675ccf532 100644 --- a/src/node_task_runner.cc +++ b/src/node_task_runner.cc @@ -351,7 +351,7 @@ void RunTask(const std::shared_ptr& result, PositionalArgs GetPositionalArgs(const std::vector& args) { // If the "--" flag is not found, return an empty optional // Otherwise, return the positional arguments as a single string - if (auto dash_dash = std::find(args.begin(), args.end(), "--"); + if (auto dash_dash = std::ranges::find(args, "--"); dash_dash != args.end()) { PositionalArgs positional_args{}; positional_args.reserve(args.size() - (dash_dash - args.begin())); diff --git a/src/node_watchdog.cc b/src/node_watchdog.cc index 4403bd4f157bff..7a493bf460701f 100644 --- a/src/node_watchdog.cc +++ b/src/node_watchdog.cc @@ -389,7 +389,7 @@ void SigintWatchdogHelper::Register(SigintWatchdogBase* wd) { void SigintWatchdogHelper::Unregister(SigintWatchdogBase* wd) { Mutex::ScopedLock lock(list_mutex_); - auto it = std::find(watchdogs_.begin(), watchdogs_.end(), wd); + auto it = std::ranges::find(watchdogs_, wd); CHECK_NE(it, watchdogs_.end()); watchdogs_.erase(it); diff --git a/test/cctest/test_inspector_socket_server.cc b/test/cctest/test_inspector_socket_server.cc index 451fbbd6acfc4d..27823f7363dc20 100644 --- a/test/cctest/test_inspector_socket_server.cc +++ b/test/cctest/test_inspector_socket_server.cc @@ -319,7 +319,7 @@ class TestSocketServerDelegate : public SocketServerDelegate { void StartSession(int session_id, const std::string& target_id) override { session_id_ = session_id; CHECK_NE(targets_.end(), - std::find(targets_.begin(), targets_.end(), target_id)); + std::ranges::find(targets_, target_id)); harness_->Connected(session_id_); } From 0a1606ac7c390f7e1a7f7a24d12b92d869b698cc Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 25 Apr 2025 14:46:38 -0400 Subject: [PATCH 2/2] lint --- src/cleanup_queue.cc | 13 +++++++------ src/node.cc | 12 ++++++------ src/node_credentials.cc | 3 +-- src/node_http2.h | 3 +-- src/node_messaging.cc | 11 +++++------ src/node_platform.cc | 9 +++++---- src/node_process_object.cc | 2 +- src/node_report.cc | 2 +- src/node_sea.cc | 7 +++---- src/node_task_runner.cc | 3 +-- test/cctest/test_inspector_socket_server.cc | 3 +-- 11 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/cleanup_queue.cc b/src/cleanup_queue.cc index 52fdca92575fad..83632515ffe967 100644 --- a/src/cleanup_queue.cc +++ b/src/cleanup_queue.cc @@ -13,12 +13,13 @@ std::vector CleanupQueue::GetOrdered() // We can't erase the copied elements from `cleanup_hooks_` yet, because we // need to be able to check whether they were un-scheduled by another hook. - std::ranges::sort(callbacks, - [](const CleanupHookCallback& a, const CleanupHookCallback& b) { - // Sort in descending order so that the most recently inserted - // callbacks are run first. - return a.insertion_order_counter_ > b.insertion_order_counter_; - }); + std::ranges::sort( + callbacks, + [](const CleanupHookCallback& a, const CleanupHookCallback& b) { + // Sort in descending order so that the most recently inserted + // callbacks are run first. + return a.insertion_order_counter_ > b.insertion_order_counter_; + }); return callbacks; } diff --git a/src/node.cc b/src/node.cc index e7bd4f4ae76667..6f85feb2b97a67 100644 --- a/src/node.cc +++ b/src/node.cc @@ -758,16 +758,16 @@ static ExitCode ProcessGlobalArgsInternal(std::vector* args, // TODO(aduh95): remove this when the harmony-import-attributes flag // is removed in V8. - if (std::ranges::find(v8_args, - "--no-harmony-import-attributes") == v8_args.end()) { + if (std::ranges::find(v8_args, "--no-harmony-import-attributes") == + v8_args.end()) { v8_args.emplace_back("--harmony-import-attributes"); } auto env_opts = per_process::cli_options->per_isolate->per_env; - if (std::ranges::find(v8_args, - "--abort-on-uncaught-exception") != v8_args.end() || - std::ranges::find(v8_args, - "--abort_on_uncaught_exception") != v8_args.end()) { + if (std::ranges::find(v8_args, "--abort-on-uncaught-exception") != + v8_args.end() || + std::ranges::find(v8_args, "--abort_on_uncaught_exception") != + v8_args.end()) { env_opts->abort_on_uncaught_exception = true; } diff --git a/src/node_credentials.cc b/src/node_credentials.cc index b2df695e7a60f1..0c7978a4843cd9 100644 --- a/src/node_credentials.cc +++ b/src/node_credentials.cc @@ -385,8 +385,7 @@ static void GetGroups(const FunctionCallbackInfo& args) { groups.resize(ngroups); gid_t egid = getegid(); - if (std::ranges::find(groups, egid) == groups.end()) - groups.push_back(egid); + if (std::ranges::find(groups, egid) == groups.end()) groups.push_back(egid); Local result; if (ToV8Value(env->context(), groups).ToLocal(&result)) { args.GetReturnValue().Set(result); diff --git a/src/node_http2.h b/src/node_http2.h index 4907899dea2124..b93c93b91f107c 100644 --- a/src/node_http2.h +++ b/src/node_http2.h @@ -692,8 +692,7 @@ class Http2Session : public AsyncWrap, bool has_pending_rststream(int32_t stream_id) { return pending_rst_streams_.end() != - std::ranges::find(pending_rst_streams_, - stream_id); + std::ranges::find(pending_rst_streams_, stream_id); } // Handle reads/writes from the underlying network transport. diff --git a/src/node_messaging.cc b/src/node_messaging.cc index f2c2e8f0078a41..abcd4a0e6bf45c 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -414,8 +414,8 @@ class SerializerDelegate : public ValueSerializer::Delegate { if (!host_objects_[i]->NestedTransferables().To(&nested_transferables)) return Nothing(); for (auto& nested_transferable : nested_transferables) { - if (std::ranges::find(host_objects_, - nested_transferable) == host_objects_.end()) { + if (std::ranges::find(host_objects_, nested_transferable) == + host_objects_.end()) { AddHostObject(nested_transferable); } } @@ -516,8 +516,7 @@ Maybe Message::Serialize(Environment* env, ThrowDataCloneException(context, env->transfer_unsupported_type_str()); return Nothing(); } - if (std::ranges::find(array_buffers, ab) != - array_buffers.end()) { + if (std::ranges::find(array_buffers, ab) != array_buffers.end()) { ThrowDataCloneException( context, FIXED_ONE_BYTE_STRING( @@ -563,8 +562,8 @@ Maybe Message::Serialize(Environment* env, "MessagePort in transfer list is already detached")); return Nothing(); } - if (std::ranges::find(delegate.host_objects_, - host_object) != delegate.host_objects_.end()) { + if (std::ranges::find(delegate.host_objects_, host_object) != + delegate.host_objects_.end()) { ThrowDataCloneException( context, String::Concat( diff --git a/src/node_platform.cc b/src/node_platform.cc index a4448fb71ceb28..71bf5aef6f6244 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -450,10 +450,11 @@ void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr task) { } void PerIsolatePlatformData::DeleteFromScheduledTasks(DelayedTask* task) { - auto it = std::ranges::find_if(scheduled_delayed_tasks_, - [task](const DelayedTaskPointer& delayed) -> bool { - return delayed.get() == task; - }); + auto it = + std::ranges::find_if(scheduled_delayed_tasks_, + [task](const DelayedTaskPointer& delayed) -> bool { + return delayed.get() == task; + }); CHECK_NE(it, scheduled_delayed_tasks_.end()); scheduled_delayed_tasks_.erase(it); } diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 9917c9de7cb976..5138ef7d616787 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -98,7 +98,7 @@ static void SetVersions(Isolate* isolate, Local versions) { #undef V std::ranges::sort(versions_array, - [](auto& a, auto& b) { return a.first < b.first; }); + [](auto& a, auto& b) { return a.first < b.first; }); for (const auto& version : versions_array) { versions diff --git a/src/node_report.cc b/src/node_report.cc index 3f66f90a4e0cd5..9ed73995be066f 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -813,7 +813,7 @@ static void PrintComponentVersions(JSONWriter* writer) { #undef V std::ranges::sort(versions_array, - [](auto& a, auto& b) { return a.first < b.first; }); + [](auto& a, auto& b) { return a.first < b.first; }); for (const auto& version : versions_array) { writer->json_keyvalue(version.first, version.second); diff --git a/src/node_sea.cc b/src/node_sea.cc index 515f728a8ca82a..ea26c02595051c 100644 --- a/src/node_sea.cc +++ b/src/node_sea.cc @@ -399,10 +399,9 @@ ExitCode GenerateSnapshotForSEA(const SeaConfig& config, return exit_code; } auto& persistents = snapshot.env_info.principal_realm.persistent_values; - auto it = std::ranges::find_if( - persistents, [](const PropInfo& prop) { - return prop.name == "snapshot_deserialize_main"; - }); + auto it = std::ranges::find_if(persistents, [](const PropInfo& prop) { + return prop.name == "snapshot_deserialize_main"; + }); if (it == persistents.end()) { FPrintF( stderr, diff --git a/src/node_task_runner.cc b/src/node_task_runner.cc index 08cd5675ccf532..2a6d3815a26c09 100644 --- a/src/node_task_runner.cc +++ b/src/node_task_runner.cc @@ -351,8 +351,7 @@ void RunTask(const std::shared_ptr& result, PositionalArgs GetPositionalArgs(const std::vector& args) { // If the "--" flag is not found, return an empty optional // Otherwise, return the positional arguments as a single string - if (auto dash_dash = std::ranges::find(args, "--"); - dash_dash != args.end()) { + if (auto dash_dash = std::ranges::find(args, "--"); dash_dash != args.end()) { PositionalArgs positional_args{}; positional_args.reserve(args.size() - (dash_dash - args.begin())); for (auto it = dash_dash + 1; it != args.end(); ++it) { diff --git a/test/cctest/test_inspector_socket_server.cc b/test/cctest/test_inspector_socket_server.cc index 27823f7363dc20..2f7fca83b7db4d 100644 --- a/test/cctest/test_inspector_socket_server.cc +++ b/test/cctest/test_inspector_socket_server.cc @@ -318,8 +318,7 @@ class TestSocketServerDelegate : public SocketServerDelegate { void StartSession(int session_id, const std::string& target_id) override { session_id_ = session_id; - CHECK_NE(targets_.end(), - std::ranges::find(targets_, target_id)); + CHECK_NE(targets_.end(), std::ranges::find(targets_, target_id)); harness_->Connected(session_id_); }