optimise: pass all large objects (std::function, std::string, structs) by const ref rather than value.

really, these functions should be passed by && and using std::forward on assignment.
however, this means writing a lot of extra code for every single class, as well as explicitly calling move
in a lot of cases.

In the case of std::string, passing by value and calling std::move is the correct approach, especially if the
string is created as an rvalue, then it is only created once and moved into the dest.
Whereas with a const ref, the string is created and then copied into the dst, basically creating 2 copies.
The same thing happens std::function, and well any object.

However, accepting everything by value sucks if you call a constructor from within a constructor, as now you need to create 2 impls
that accept by value and the other by rvalue. All of this extra code to have a more efficent impl just isn't worth it when, going by
the benchmarks, makes no measurable difference (i count anything within >= 1ms as measurable).
This commit is contained in:
ITotalJustice
2025-08-04 18:58:20 +01:00
parent 9fe9c9d491
commit 54d73a6d3b
36 changed files with 127 additions and 133 deletions

View File

@@ -71,7 +71,7 @@ public:
static void PopToMenu();
// this is thread safe
static void Notify(std::string text, ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT);
static void Notify(const std::string& text, ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT);
static void Notify(ui::NotifEntry entry);
static void NotifyPop(ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT);
static void NotifyClear(ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT);

View File

@@ -52,7 +52,7 @@ struct Fields {
struct Header {
Header() = default;
Header(std::initializer_list<std::pair<const std::string, std::string>> p) : m_map{p} {}
Header(std::initializer_list<std::pair<const std::string, std::string>>&& p) : m_map{std::forward<decltype(p)>(p)} {}
std::unordered_map<std::string, std::string> m_map;
auto Find(const std::string& key) const {
@@ -91,7 +91,7 @@ struct UserPass {
struct UploadInfo {
UploadInfo() = default;
UploadInfo(const std::string& name) : m_name{name} {}
UploadInfo(const std::string& name, s64 size, OnUploadCallback cb) : m_name{name}, m_size{size}, m_callback{cb} {}
UploadInfo(const std::string& name, s64 size, const OnUploadCallback& cb) : m_name{name}, m_size{size}, m_callback{cb} {}
UploadInfo(const std::string& name, const std::vector<u8>& data) : m_name{name}, m_data{data} {}
std::string m_name{};
std::vector<u8> m_data{};

View File

@@ -487,7 +487,7 @@ struct FsNative : Fs {
FsFileSystem m_fs{};
Result m_open_result{};
bool m_own{true};
const bool m_own{true};
};
#if 0

View File

@@ -11,7 +11,7 @@ using OnInstallStart = std::function<bool(const char* path)>;
using OnInstallWrite = std::function<bool(const void* buf, size_t size)>;
using OnInstallClose = std::function<void()>;
void InitInstallMode(OnInstallStart on_start, OnInstallWrite on_write, OnInstallClose on_close);
void InitInstallMode(const OnInstallStart& on_start, const OnInstallWrite& on_write, const OnInstallClose& on_close);
void DisableInstallMode();
unsigned GetPort();

View File

@@ -11,7 +11,7 @@ using OnInstallStart = std::function<bool(const char* path)>;
using OnInstallWrite = std::function<bool(const void* buf, size_t size)>;
using OnInstallClose = std::function<void()>;
void InitInstallMode(OnInstallStart on_start, OnInstallWrite on_write, OnInstallClose on_close);
void InitInstallMode(const OnInstallStart& on_start, const OnInstallWrite& on_write, const OnInstallClose& on_close);
void DisableInstallMode();
} // namespace sphaira::haze

View File

@@ -32,11 +32,11 @@ using StartCallback = std::function<Result(PullCallback pull)>;
using StartCallback2 = std::function<Result(StartThreadCallback start, PullCallback pull)>;
// reads data from rfunc into wfunc.
Result Transfer(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, WriteCallback wfunc, Mode mode = Mode::MultiThreaded);
Result Transfer(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const WriteCallback& wfunc, Mode mode = Mode::MultiThreaded);
// reads data from rfunc, pull data from provided pull() callback.
Result TransferPull(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, StartCallback sfunc, Mode mode = Mode::MultiThreaded);
Result TransferPull(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, StartCallback2 sfunc, Mode mode = Mode::MultiThreaded);
Result TransferPull(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const StartCallback& sfunc, Mode mode = Mode::MultiThreaded);
Result TransferPull(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const StartCallback2& sfunc, Mode mode = Mode::MultiThreaded);
// helper for extract zips.
// this will multi-thread unzip if size >= 512KiB, otherwise it'll single pass.
@@ -50,7 +50,7 @@ using UnzipAllFilter = std::function<bool(const fs::FsPath& name, fs::FsPath& pa
// helper all-in-one unzip function that unzips a zip (either open or path provided).
// the filter function can be used to modify the path and filter out unwanted files.
Result TransferUnzipAll(ui::ProgressBox* pbox, void* zfile, fs::Fs* fs, const fs::FsPath& base_path, UnzipAllFilter filter = nullptr, Mode mode = Mode::SingleThreadedIfSmaller);
Result TransferUnzipAll(ui::ProgressBox* pbox, const fs::FsPath& zip_out, fs::Fs* fs, const fs::FsPath& base_path, UnzipAllFilter filter = nullptr, Mode mode = Mode::SingleThreadedIfSmaller);
Result TransferUnzipAll(ui::ProgressBox* pbox, void* zfile, fs::Fs* fs, const fs::FsPath& base_path, const UnzipAllFilter& filter = nullptr, Mode mode = Mode::SingleThreadedIfSmaller);
Result TransferUnzipAll(ui::ProgressBox* pbox, const fs::FsPath& zip_out, fs::Fs* fs, const fs::FsPath& base_path, const UnzipAllFilter& filter = nullptr, Mode mode = Mode::SingleThreadedIfSmaller);
} // namespace sphaira::thread

View File

@@ -10,14 +10,14 @@ struct List final : Object {
GRID,
};
using Callback = std::function<void(NVGcontext* vg, Theme* theme, Vec4 v, s64 index)>;
using Callback = std::function<void(NVGcontext* vg, Theme* theme, const Vec4& v, s64 index)>;
using TouchCallback = std::function<void(bool touch, s64 index)>;
List(s64 row, s64 page, const Vec4& pos, const Vec4& v, const Vec2& pad = {});
void OnUpdate(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback);
void OnUpdate(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback);
void Draw(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const;
void Draw(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const;
auto SetScrollBarPos(float x, float y, float h) {
m_scrollbar.x = x;
@@ -73,10 +73,10 @@ private:
auto ClampX(float x, s64 count) const -> float;
auto ClampY(float y, s64 count) const -> float;
void OnUpdateHome(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback);
void OnUpdateGrid(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback);
void DrawHome(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const;
void DrawGrid(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const;
void OnUpdateHome(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback);
void OnUpdateGrid(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback);
void DrawHome(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const;
void DrawGrid(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const;
private:
const s64 m_row;

View File

@@ -86,14 +86,16 @@ struct EntryMenu final : MenuBase {
private:
struct Option {
Option(const std::string& dt, const std::string& ct, std::function<void(void)> f)
using Callback = std::function<void(void)>;
Option(const std::string& dt, const std::string& ct, const Callback& f)
: display_text{dt}, confirm_text{ct}, func{f} {}
Option(const std::string& dt, std::function<void(void)> f)
Option(const std::string& dt, const Callback& f)
: display_text{dt}, func{f} {}
std::string display_text{};
std::string confirm_text{};
std::function<void(void)> func{};
const std::string display_text;
const std::string confirm_text;
const Callback func{};
};
Entry& m_entry;

View File

@@ -33,9 +33,17 @@ struct MenuBase : Widget {
return true;
}
void SetTitle(std::string title);
void SetTitleSubHeading(std::string sub_heading);
void SetSubHeading(std::string sub_heading);
void SetTitle(const std::string& title) {
m_title = title;
}
void SetTitleSubHeading(const std::string& sub_heading) {
m_title_sub_heading = sub_heading;
}
void SetSubHeading(const std::string& sub_heading) {
m_sub_heading = sub_heading;
}
auto GetTitle() const {
return m_title;

View File

@@ -104,7 +104,7 @@ struct Config {
u32 limit{18};
bool nsfw{false};
void SetQuery(std::string new_query) {
void SetQuery(const std::string& new_query) {
query = new_query;
}
@@ -112,7 +112,7 @@ struct Config {
query.clear();
}
void SetCreator(Creator new_creator) {
void SetCreator(const Creator& new_creator) {
creator = new_creator.id;
}

View File

@@ -11,8 +11,7 @@ public:
enum class Side { LEFT, RIGHT };
public:
NotifEntry(std::string text, Side side);
~NotifEntry() = default;
NotifEntry(const std::string& text, Side side);
auto Draw(NVGcontext* vg, Theme* theme, float y) -> bool;
auto GetSide() const noexcept { return m_side; }
@@ -22,17 +21,14 @@ private:
void Draw(NVGcontext* vg, Theme* theme) override;
private:
std::string m_text{};
std::string m_text;
Side m_side;
std::size_t m_count{180}; // count down to zero
Side m_side{};
bool m_bounds_measured{};
};
class NotifMananger final : public Object {
public:
NotifMananger() = default;
~NotifMananger() = default;
void Draw(NVGcontext* vg, Theme* theme) override;
void Push(const NotifEntry& entry);

View File

@@ -54,7 +54,7 @@ public:
m_pos = { x, y, w, h };
}
auto SetPos(Vec4 v) noexcept -> void {
auto SetPos(const Vec4& v) noexcept -> void {
m_pos = v;
}

View File

@@ -9,7 +9,7 @@ class OptionBoxEntry final : public Widget {
public:
public:
OptionBoxEntry(const std::string& text, Vec4 pos);
OptionBoxEntry(const std::string& text, const Vec4& pos);
auto Update(Controller* controller, TouchInfo* touch) -> void override {}
auto Draw(NVGcontext* vg, Theme* theme) -> void override;

View File

@@ -13,11 +13,11 @@ public:
using Callback = std::function<void(std::optional<s64>)>;
public:
explicit PopupList(std::string title, Items items, Callback cb, s64 index = 0);
PopupList(std::string title, Items items, Callback cb, std::string index);
PopupList(std::string title, Items items, std::string& index_str_ref, s64& index);
PopupList(std::string title, Items items, std::string& index_ref);
PopupList(std::string title, Items items, s64& index_ref);
explicit PopupList(const std::string& title, const Items& items, const Callback& cb, s64 index = 0);
PopupList(const std::string& title, const Items& items, const Callback& cb, const std::string& index);
PopupList(const std::string& title, const Items& items, std::string& index_str_ref, s64& index);
PopupList(const std::string& title, const Items& items, std::string& index_ref);
PopupList(const std::string& title, const Items& items, s64& index_ref);
auto Update(Controller* controller, TouchInfo* touch) -> void override;
auto Draw(NVGcontext* vg, Theme* theme) -> void override;
@@ -33,8 +33,8 @@ private:
static constexpr float m_text_xoffset{15.f};
static constexpr float m_line_width{1220.f};
std::string m_title{};
Items m_items{};
const std::string m_title;
const Items m_items;
Callback m_callback{};
s64 m_index{}; // index in list array
s64 m_starting_index{};

View File

@@ -17,7 +17,7 @@ struct ProgressBox final : Widget {
int image,
const std::string& action,
const std::string& title,
ProgressBoxCallback callback, ProgressBoxDoneCallback done = [](Result rc){},
const ProgressBoxCallback& callback, const ProgressBoxDoneCallback& done = [](Result rc){},
int cpuid = 1, int prio = PRIO_PREEMPTIVE, int stack_size = 1024*128
);
~ProgressBox();

View File

@@ -59,10 +59,10 @@ protected:
}
protected:
std::string m_title;
const std::string m_title;
private:
std::string m_info{};
const std::string m_info;
std::string m_depends_info{};
DependsCallback m_depends_callback{};
DependsClickCallback m_depends_click{};
@@ -78,7 +78,7 @@ public:
using Callback = std::function<void(bool&)>;
public:
explicit SidebarEntryBool(const std::string& title, bool option, Callback cb, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off");
explicit SidebarEntryBool(const std::string& title, bool option, const Callback& cb, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off");
explicit SidebarEntryBool(const std::string& title, bool& option, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off");
explicit SidebarEntryBool(const std::string& title, option::OptionBool& option, const Callback& cb, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off");
explicit SidebarEntryBool(const std::string& title, option::OptionBool& option, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off");
@@ -96,13 +96,13 @@ public:
using Callback = std::function<void()>;
public:
explicit SidebarEntryCallback(const std::string& title, Callback cb, const std::string& info);
explicit SidebarEntryCallback(const std::string& title, Callback cb, bool pop_on_click = false, const std::string& info = "");
explicit SidebarEntryCallback(const std::string& title, const Callback& cb, const std::string& info);
explicit SidebarEntryCallback(const std::string& title, const Callback& cb, bool pop_on_click = false, const std::string& info = "");
void Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, bool left) override;
private:
Callback m_callback;
bool m_pop_on_click;
const Callback m_callback;
const bool m_pop_on_click;
};
class SidebarEntryArray final : public SidebarEntryBase {
@@ -112,16 +112,16 @@ public:
using Callback = std::function<void(s64& index)>;
public:
explicit SidebarEntryArray(const std::string& title, const Items& items, Callback cb, s64 index = 0, const std::string& info = "");
explicit SidebarEntryArray(const std::string& title, const Items& items, Callback cb, const std::string& index, const std::string& info = "");
explicit SidebarEntryArray(const std::string& title, const Items& items, const Callback& cb, s64 index = 0, const std::string& info = "");
explicit SidebarEntryArray(const std::string& title, const Items& items, const Callback& cb, const std::string& index, const std::string& info = "");
explicit SidebarEntryArray(const std::string& title, const Items& items, std::string& index, const std::string& info = "");
void Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, bool left) override;
private:
Items m_items;
ListCallback m_list_callback;
Callback m_callback;
const Items m_items;
const Callback m_callback;
s64 m_index;
ListCallback m_list_callback{};
};
// single text entry.
@@ -204,9 +204,9 @@ private:
void SetupButtons();
private:
std::string m_title;
std::string m_sub;
Side m_side;
const std::string m_title;
const std::string m_sub;
const Side m_side;
Items m_items;
s64 m_index{};

View File

@@ -56,8 +56,8 @@ struct Vec2 {
struct Vec4 {
constexpr Vec4() = default;
constexpr Vec4(float _x, float _y, float _w, float _h) : x{_x}, y{_y}, w{_w}, h{_h} {}
constexpr Vec4(Vec2 vec0, Vec2 vec1) : x{vec0.x}, y{vec0.y}, w{vec1.x}, h{vec1.y} {}
constexpr Vec4(Vec4 vec0, Vec4 vec1) : x{vec0.x}, y{vec0.y}, w{vec1.w}, h{vec1.h} {}
constexpr Vec4(const Vec2& vec0, const Vec2& vec1) : x{vec0.x}, y{vec0.y}, w{vec1.x}, h{vec1.y} {}
constexpr Vec4(const Vec4& vec0, const Vec4& vec1) : x{vec0.x}, y{vec0.y}, w{vec1.w}, h{vec1.h} {}
float& operator[](std::size_t idx) {
switch (idx) {
@@ -339,10 +339,10 @@ struct Action final {
CallbackWithBool
>;
Action(Callback cb) : Action{ActionType::DOWN, "", cb} {}
Action(std::string hint, Callback cb) : Action{ActionType::DOWN, hint, cb} {}
Action(u8 type, Callback cb) : Action{type, "", cb} {}
Action(u8 type, std::string hint, Callback cb) : m_type{type}, m_callback{cb}, m_hint{hint} {}
explicit Action(const Callback& cb) : Action{ActionType::DOWN, "", cb} {}
explicit Action(const std::string& hint, const Callback& cb) : Action{ActionType::DOWN, hint, cb} {}
explicit Action(u8 type, const Callback& cb) : Action{type, "", cb} {}
explicit Action(u8 type, const std::string& hint, const Callback& cb) : m_type{type}, m_callback{cb}, m_hint{hint} {}
auto IsHidden() const noexcept { return m_hint.empty(); }

View File

@@ -48,8 +48,8 @@ struct Widget : public Object {
}
auto HasAction(Button button) const -> bool;
void SetAction(Button button, Action action);
void SetActions(std::same_as<std::pair<Button, Action>> auto ...args) {
void SetAction(Button button, const Action& action);
void SetActions(std::same_as<std::pair<Button, Action>> auto&& ...args) {
const std::array list = {args...};
for (const auto& [button, action] : list) {
SetAction(button, action);