Skip to content

Commit 427e0b0

Browse files
committed
fix a copious amount of unnecessary ModMetadata and event callback copies
1 parent 4137a6a commit 427e0b0

File tree

18 files changed

+243
-229
lines changed

18 files changed

+243
-229
lines changed

loader/include/Geode/loader/Event.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace geode {
3939

4040
template <class... Args>
4141
class DispatchFilter;
42-
42+
4343
class GEODE_DLL DefaultEventListenerPool : public EventListenerPool {
4444
protected:
4545
// fix this in Geode 4.0.0
@@ -66,7 +66,7 @@ namespace geode {
6666
friend class DispatchEvent;
6767

6868
template <class... Args>
69-
friend class DispatchFilter;
69+
friend class DispatchFilter;
7070
};
7171

7272
class GEODE_DLL EventListenerProtocol {
@@ -92,7 +92,7 @@ namespace geode {
9292

9393
template <typename T>
9494
concept is_event = std::is_base_of_v<Event, T>;
95-
95+
9696
template <is_event T>
9797
class EventFilter {
9898
protected:
@@ -102,7 +102,8 @@ namespace geode {
102102
using Callback = ListenerResult(T*);
103103
using Event = T;
104104

105-
ListenerResult handle(std::function<Callback> fn, T* e) {
105+
template <typename F> requires (std::is_invocable_r_v<ListenerResult, F, T*>)
106+
ListenerResult handle(F&& fn, T* e) {
106107
return fn(e);
107108
}
108109

@@ -203,11 +204,11 @@ namespace geode {
203204
m_filter.setListener(this);
204205
other.disable();
205206

206-
return *this;
207+
return *this;
207208
}
208209

209210
void bind(std::function<Callback> fn) {
210-
m_callback = fn;
211+
m_callback = std::move(fn);
211212
}
212213

213214
template <typename C>
@@ -216,7 +217,7 @@ namespace geode {
216217
}
217218

218219
void setFilter(T filter) {
219-
m_filter = filter;
220+
m_filter = std::move(filter);
220221
m_filter.setListener(this);
221222
}
222223

@@ -252,7 +253,7 @@ namespace geode {
252253
ListenerResult post() {
253254
return postFromMod(getMod());
254255
}
255-
256+
256257
virtual ~Event();
257258
};
258259

loader/include/Geode/loader/Mod.hpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <unordered_map>
2323
#include <vector>
2424

25-
namespace geode {
25+
namespace geode {
2626
template <class T>
2727
struct HandleToSaved : public T {
2828
Mod* m_mod;
@@ -99,7 +99,11 @@ namespace geode {
9999
bool isOrWillBeEnabled() const;
100100
bool isInternal() const;
101101
bool needsEarlyLoad() const;
102-
ModMetadata getMetadata() const;
102+
103+
[[deprecated("Use Mod::getMetadataRef which is better for efficiency")]]
104+
ModMetadata getMetadata() const; // TODO: remove in v5
105+
ModMetadata const& getMetadataRef() const;
106+
103107
std::filesystem::path getTempDir() const;
104108
/**
105109
* Get the path to the mod's platform binary (.dll on Windows, .dylib
@@ -113,11 +117,11 @@ namespace geode {
113117
std::filesystem::path getResourcesDir() const;
114118

115119
/**
116-
* Get the dependency settings for a specific dependency via its ID. For
117-
* example, if this mod depends on Custom Keybinds, it can specify the
118-
* keybinds it wants to add in `mod.json` under
120+
* Get the dependency settings for a specific dependency via its ID. For
121+
* example, if this mod depends on Custom Keybinds, it can specify the
122+
* keybinds it wants to add in `mod.json` under
119123
* `dependencies."geode.custom-keybinds".settings.keybinds`
120-
* @returns Null JSON value if there are no settings or if the mod
124+
* @returns Null JSON value if there are no settings or if the mod
121125
* doesn't depend on the given mod ID
122126
*/
123127
matjson::Value getDependencySettingsFor(std::string_view dependencyID) const;
@@ -129,13 +133,13 @@ namespace geode {
129133

130134
using CheckUpdatesTask = Task<Result<std::optional<VersionInfo>, std::string>>;
131135
/**
132-
* Check if this Mod has updates available on the mods index. If
133-
* you're using this for automatic update checking, use
134-
* `openInfoPopup` from the `ui/GeodeUI.hpp` header to open the Mod's
136+
* Check if this Mod has updates available on the mods index. If
137+
* you're using this for automatic update checking, use
138+
* `openInfoPopup` from the `ui/GeodeUI.hpp` header to open the Mod's
135139
* page to let the user install the update
136-
* @returns A task that resolves to an option, either the latest
137-
* available version on the index if there are updates available, or
138-
* `std::nullopt` if there are no updates. On error, the Task returns
140+
* @returns A task that resolves to an option, either the latest
141+
* available version on the index if there are updates available, or
142+
* `std::nullopt` if there are no updates. On error, the Task returns
139143
* an error
140144
*/
141145
CheckUpdatesTask checkUpdates() const;
@@ -162,26 +166,26 @@ namespace geode {
162166
*/
163167
bool hasSettings() const;
164168
/**
165-
* Get a list of all this mod's setting keys (in the order they were
169+
* Get a list of all this mod's setting keys (in the order they were
166170
* declared in `mod.json`)
167171
*/
168172
std::vector<std::string> getSettingKeys() const;
169173
bool hasSetting(std::string_view key) const;
170174

171175
/**
172-
* Get the definition of a setting, or null if the setting was not found,
173-
* or if it's a custom setting that has not yet been registered using
176+
* Get the definition of a setting, or null if the setting was not found,
177+
* or if it's a custom setting that has not yet been registered using
174178
* `Mod::registerCustomSettingType`
175179
* @param key The key of the setting as defined in `mod.json`
176180
*/
177181
std::shared_ptr<Setting> getSetting(std::string_view key) const;
178182

179183
/**
180-
* Register a custom setting type. See
184+
* Register a custom setting type. See
181185
* [the setting docs](https://docs.geode-sdk.org/mods/settings) for more
182-
* @param type The type of the setting. This should **not** include the
186+
* @param type The type of the setting. This should **not** include the
183187
* `custom:` prefix!
184-
* @param generator A pointer to a function that, when called, returns a
188+
* @param generator A pointer to a function that, when called, returns a
185189
* newly-created instance of the setting type
186190
*/
187191
Result<> registerCustomSettingType(std::string_view type, SettingGenerator generator);
@@ -229,9 +233,9 @@ namespace geode {
229233
matjson::Value& getSavedSettingsData();
230234

231235
/**
232-
* Get the value of a [setting](https://docs.geode-sdk.org/mods/settings).
233-
* To use this for custom settings, first specialize the
234-
* `SettingTypeForValueType` class, and then make sure your custom
236+
* Get the value of a [setting](https://docs.geode-sdk.org/mods/settings).
237+
* To use this for custom settings, first specialize the
238+
* `SettingTypeForValueType` class, and then make sure your custom
235239
* setting type has a `getValue` function which returns the value
236240
*/
237241
template <class T>
@@ -475,13 +479,13 @@ namespace geode {
475479
void setLogLevel(Severity level);
476480

477481
/**
478-
* If this mod is built for an outdated GD or Geode version, returns the
479-
* `LoadProblem` describing the situation. Otherwise `nullopt` if the
482+
* If this mod is built for an outdated GD or Geode version, returns the
483+
* `LoadProblem` describing the situation. Otherwise `nullopt` if the
480484
* mod is made for the correct version of the game and Geode
481485
*/
482486
std::optional<LoadProblem> targetsOutdatedVersion() const;
483487
/**
484-
* @note Make sure to also call `targetsOutdatedVersion` if you want to
488+
* @note Make sure to also call `targetsOutdatedVersion` if you want to
485489
* make sure the mod is actually loadable
486490
*/
487491
bool hasLoadProblems() const;

0 commit comments

Comments
 (0)