Skip to content

Commit b36c67d

Browse files
authored
metadata generator performance optimization (#26)
* refactor: improve parameter handling and memory management across various classes - Updated function and method signatures to use const references for string parameters, enhancing performance and clarity. - Introduced new member variables for better organization and memory management in classes such as MemberDecl and MetadataFactory. - Enhanced the handling of collections by reserving space in vectors and unordered sets to optimize memory usage. - Improved the process of adding and managing protocols, enums, and classes within the MetadataFactory, ensuring more efficient lookups and insertions. - Refactored various methods to utilize move semantics for better performance when dealing with temporary objects. * chore: regenerate metadata types
1 parent 9e10cd5 commit b36c67d

File tree

339 files changed

+499439
-32276
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

339 files changed

+499439
-32276
lines changed

metadata-generator/include/IR.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class MemberDecl {
234234

235235
// Method overloads for TS emission (used to preserve superclass signatures).
236236
std::vector<MemberDecl> overloads;
237+
std::vector<std::string> overloadSignatureKeys;
237238

238239
void addOverloadFrom(const MemberDecl& member);
239240
};
@@ -248,7 +249,7 @@ class ClassDecl {
248249

249250
ClassDecl(CXCursor cursor);
250251

251-
MemberDecl* getMemberNamed(std::string& name);
252+
MemberDecl* getMemberNamed(const std::string& name);
252253

253254
void postProcessMembers();
254255

@@ -279,7 +280,7 @@ class ProtocolDecl {
279280

280281
ProtocolDecl(CXCursor cursor);
281282

282-
MemberDecl* getMemberNamed(std::string& name);
283+
MemberDecl* getMemberNamed(const std::string& name);
283284

284285
void postProcessMembers();
285286

@@ -376,10 +377,11 @@ class MetadataFactory {
376377
std::unordered_set<std::string> referencedProtocols;
377378

378379
std::unordered_set<std::string> renamedProtocols;
379-
std::unordered_set<std::string> missingClasses;
380+
std::unordered_set<std::string> missingClasses;
380381

381382
private:
382383
bool _checkAvailability = false;
384+
std::unordered_map<std::string, bool> shouldProcessCache;
383385

384386
std::unordered_map<std::string, EnumDecl> skippedEnums;
385387
std::unordered_map<std::string, StructDecl> skippedStructs;

metadata-generator/include/MetadataWriter.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,22 @@ class MDSection : public std::unordered_map<MDSectionOffset, T> {
236236
size_t section_size;
237237
S serde;
238238
std::unordered_map<std::string, MDSectionOffset> stringToKey;
239+
std::vector<std::pair<MDSectionOffset, T>> orderedEntries;
239240

240241
MDSection(S serde) : section_offset(0), section_size(0), serde(serde) {}
241242

242-
inline MDSectionOffset add(T value, std::string strKey) {
243-
if (stringToKey.contains(strKey)) {
244-
return stringToKey[strKey];
243+
inline MDSectionOffset add(T value, const std::string& strKey) {
244+
auto keyIt = stringToKey.find(strKey);
245+
if (keyIt != stringToKey.end()) {
246+
return keyIt->second;
245247
}
246248
MDSectionOffset key = (MDSectionOffset)section_offset;
247249
size_t valueSize = serde.size(value);
248250
section_offset += valueSize;
249251
section_size += valueSize;
250-
this->insert(std::make_pair(key, value));
251-
stringToKey.insert(std::make_pair(strKey, key));
252+
this->emplace(key, value);
253+
orderedEntries.emplace_back(key, value);
254+
stringToKey.emplace(strKey, key);
252255
return key;
253256
}
254257
};
@@ -272,7 +275,7 @@ class MDMetadataWriter {
272275
protocols(MDProtocolSerde()), classes(MDClassSerde()),
273276
structs(MDStructSerde()), unions(MDUnionSerde()) {}
274277

275-
MDTypeInfo *getTypeInfo(TypeSpec &type);
278+
MDTypeInfo *getTypeInfo(const TypeSpec &type);
276279

277280
MDMember *memberFromDecl(MemberDecl &decl);
278281

metadata-generator/include/TSEmitter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TSLines {
1818
public:
1919
TSLines() {}
2020

21-
void write(std::string value);
21+
void write(const std::string& value);
2222
void newline();
2323
void enter();
2424
void exit();
@@ -48,7 +48,7 @@ class TSFile {
4848

4949
std::string toString();
5050

51-
inline void import(std::string module) { imports.insert(module); }
51+
inline void import(const std::string& module) { imports.insert(module); }
5252

5353
MetadataFactory *factory = nullptr;
5454

@@ -72,7 +72,7 @@ class TSEmitter {
7272

7373
void resolveImports(TSFile &file);
7474

75-
void ensureFile(std::string framework);
75+
TSFile& ensureFile(const std::string& framework);
7676

7777
void write();
7878

metadata-generator/include/Util.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
namespace metagen {
77

8-
inline std::string jsifySelector(std::string selector) {
8+
inline std::string jsifySelector(const std::string& selector) {
99
std::string jsifiedSelector;
10+
jsifiedSelector.reserve(selector.size());
1011
bool nextupper = false;
1112
for (auto c : selector) {
1213
if (c == ':') {
@@ -21,29 +22,31 @@ inline std::string jsifySelector(std::string selector) {
2122
return jsifiedSelector;
2223
}
2324

24-
inline std::string jsifyName(std::string name) {
25+
inline std::string jsifyName(const std::string& name) {
2526
if (name == "arguments" || name == "function" || name == "DOMException") {
2627
return name + "$";
2728
} else {
2829
return name;
2930
}
3031
}
3132

32-
inline std::vector<std::string> splitCamelCase(std::string value) {
33+
inline std::vector<std::string> splitCamelCase(const std::string& value) {
3334
std::vector<std::string> result;
35+
result.reserve(value.size() / 4 + 1);
3436

35-
std::string current = "";
37+
std::string current;
38+
current.reserve(value.size());
3639
for (auto c : value) {
3740
if (isupper(c)) {
38-
if (current != "") {
41+
if (!current.empty()) {
3942
result.emplace_back(current);
4043
}
4144
current = "";
4245
}
4346
current += c;
4447
}
4548

46-
if (current != "") {
49+
if (!current.empty()) {
4750
result.emplace_back(current);
4851
}
4952

@@ -117,7 +120,7 @@ inline bool isAvailable(CXCursor cursor) {
117120
availability == CXAvailability_Deprecated;
118121
}
119122

120-
inline bool isSelectorOwned(std::string selectorName) {
123+
inline bool isSelectorOwned(const std::string& selectorName) {
121124
return selectorName.find("copy") == 0 ||
122125
selectorName.find("mutableCopy") == 0 ||
123126
selectorName.find("new") == 0 || selectorName.find("alloc") == 0;

0 commit comments

Comments
 (0)