internal: reference command-line arguments instead of copying them (#11422)

Eliminates the need to copy command-line arguments into a std::vector<std::string>, reducing memory usage and improving performance by referencing the original arguments directly.
This commit is contained in:
Hato
2025-08-18 04:18:51 +09:00
committed by GitHub
parent bca96a5d3b
commit d890178610
2 changed files with 166 additions and 149 deletions

View File

@@ -22,11 +22,16 @@
#include <filesystem> #include <filesystem>
#include <hyprutils/os/Process.hpp> #include <hyprutils/os/Process.hpp>
#include <hyprutils/memory/WeakPtr.hpp> #include <hyprutils/memory/WeakPtr.hpp>
#include <hyprutils/memory/Casts.hpp>
using namespace Hyprutils::Memory;
#include <csignal> #include <csignal>
#include <cerrno> #include <cerrno>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <print> #include <print>
#include <string_view>
#include <span>
#include "Log.hpp" #include "Log.hpp"
@@ -91,10 +96,13 @@ int main(int argc, char** argv, char** envp) {
std::string binaryPath = ""; std::string binaryPath = "";
std::string pluginPath = std::filesystem::current_path().string(); std::string pluginPath = std::filesystem::current_path().string();
std::vector<std::string> args{argv + 1, argv + argc}; if (argc > 1) {
std::span<char*> args{argv + 1, sc<std::size_t>(argc - 1)};
for (auto it = args.begin(); it != args.end(); it++) { for (auto it = args.begin(); it != args.end(); it++) {
if (*it == "--config" || *it == "-c") { std::string_view value = *it;
if (value == "--config" || value == "-c") {
if (std::next(it) == args.end()) { if (std::next(it) == args.end()) {
help(); help();
@@ -119,7 +127,7 @@ int main(int argc, char** argv, char** envp) {
it++; it++;
continue; continue;
} else if (*it == "--binary" || *it == "-b") { } else if (value == "--binary" || value == "-b") {
if (std::next(it) == args.end()) { if (std::next(it) == args.end()) {
help(); help();
@@ -144,7 +152,7 @@ int main(int argc, char** argv, char** envp) {
it++; it++;
continue; continue;
} else if (*it == "--plugin" || *it == "-p") { } else if (value == "--plugin" || value == "-p") {
if (std::next(it) == args.end()) { if (std::next(it) == args.end()) {
help(); help();
@@ -169,7 +177,7 @@ int main(int argc, char** argv, char** envp) {
it++; it++;
continue; continue;
} else if (*it == "--help" || *it == "-h") { } else if (value == "--help" || value == "-h") {
help(); help();
return 0; return 0;
@@ -180,6 +188,7 @@ int main(int argc, char** argv, char** envp) {
return 1; return 1;
} }
} }
}
NLog::log("{}launching hl", Colors::YELLOW); NLog::log("{}launching hl", Colors::YELLOW);
if (!launchHyprland(configPath, binaryPath)) { if (!launchHyprland(configPath, binaryPath)) {

View File

@@ -7,8 +7,10 @@
#include <cstdio> #include <cstdio>
#include <hyprutils/string/String.hpp> #include <hyprutils/string/String.hpp>
#include <hyprutils/memory/Casts.hpp>
#include <print> #include <print>
using namespace Hyprutils::String; using namespace Hyprutils::String;
using namespace Hyprutils::Memory;
#include <fcntl.h> #include <fcntl.h>
#include <iostream> #include <iostream>
@@ -16,6 +18,8 @@ using namespace Hyprutils::String;
#include <vector> #include <vector>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view>
#include <span>
#include <filesystem> #include <filesystem>
static void help() { static void help() {
@@ -53,14 +57,17 @@ int main(int argc, char** argv) {
int socketFd = -1; int socketFd = -1;
bool ignoreSudo = false, verifyConfig = false; bool ignoreSudo = false, verifyConfig = false;
std::vector<std::string> args{argv + 1, argv + argc}; if (argc > 1) {
std::span<char*> args{argv + 1, sc<std::size_t>(argc - 1)};
for (auto it = args.begin(); it != args.end(); it++) { for (auto it = args.begin(); it != args.end(); it++) {
if (*it == "--i-am-really-stupid" && !ignoreSudo) { std::string_view value = *it;
if (value == "--i-am-really-stupid" && !ignoreSudo) {
std::println("[ WARNING ] Running Hyprland with superuser privileges might damage your system"); std::println("[ WARNING ] Running Hyprland with superuser privileges might damage your system");
ignoreSudo = true; ignoreSudo = true;
} else if (*it == "--socket") { } else if (value == "--socket") {
if (std::next(it) == args.end()) { if (std::next(it) == args.end()) {
help(); help();
@@ -69,7 +76,7 @@ int main(int argc, char** argv) {
socketName = *std::next(it); socketName = *std::next(it);
it++; it++;
} else if (*it == "--wayland-fd") { } else if (value == "--wayland-fd") {
if (std::next(it) == args.end()) { if (std::next(it) == args.end()) {
help(); help();
@@ -90,7 +97,7 @@ int main(int argc, char** argv) {
} }
it++; it++;
} else if (*it == "-c" || *it == "--config") { } else if (value == "-c" || value == "--config") {
if (std::next(it) == args.end()) { if (std::next(it) == args.end()) {
help(); help();
@@ -116,26 +123,27 @@ int main(int argc, char** argv) {
it++; it++;
continue; continue;
} else if (*it == "-h" || *it == "--help") { } else if (value == "-h" || value == "--help") {
help(); help();
return 0; return 0;
} else if (*it == "-v" || *it == "--version") { } else if (value == "-v" || value == "--version") {
std::println("{}", versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); std::println("{}", versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, ""));
return 0; return 0;
} else if (*it == "--systeminfo") { } else if (value == "--systeminfo") {
std::println("{}", systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); std::println("{}", systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, ""));
return 0; return 0;
} else if (*it == "--verify-config") { } else if (value == "--verify-config") {
verifyConfig = true; verifyConfig = true;
continue; continue;
} else { } else {
std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it); std::println(stderr, "[ ERROR ] Unknown option '{}' !", value);
help(); help();
return 1; return 1;
} }
} }
}
if (!ignoreSudo && NInit::isSudo()) { if (!ignoreSudo && NInit::isSudo()) {
std::println(stderr, std::println(stderr,