Skip to content

Commit 7548421

Browse files
committed
optimize m_fields access to perform no allocations
1 parent 27f82a5 commit 7548421

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

loader/src/hooks/GeodeNodeMetadata.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ constexpr auto METADATA_TAG = 0xB324ABC;
1515

1616
struct ProxyCCNode;
1717

18+
static uint64_t fnv1aHash(char const* str) {
19+
uint64_t hash = 0xcbf29ce484222325;
20+
while (*str) {
21+
hash ^= *str++;
22+
hash *= 0x100000001b3;
23+
}
24+
return hash;
25+
}
26+
1827
class GeodeNodeMetadata final : public cocos2d::CCObject {
1928
private:
20-
std::unordered_map<std::string, FieldContainer*> m_classFieldContainers;
29+
// for performance reasons, this key is the hash of the class name
30+
std::unordered_map<uint64_t, FieldContainer*> m_classFieldContainers;
2131
std::string m_id = "";
2232
Ref<Layout> m_layout = nullptr;
2333
Ref<LayoutOptions> m_layoutOptions = nullptr;
@@ -63,10 +73,14 @@ class GeodeNodeMetadata final : public cocos2d::CCObject {
6373
}
6474

6575
FieldContainer* getFieldContainer(char const* forClass) {
66-
if (!m_classFieldContainers.count(forClass)) {
67-
m_classFieldContainers[forClass] = new FieldContainer();
76+
auto hash = fnv1aHash(forClass);
77+
78+
auto& container = m_classFieldContainers[hash];
79+
if (!container) {
80+
container = new FieldContainer();
6881
}
69-
return m_classFieldContainers[forClass];
82+
83+
return container;
7084
}
7185
};
7286

@@ -78,7 +92,7 @@ struct ProxyCCNode : Modify<ProxyCCNode, CCNode> {
7892
return asNode->getUserObject("");
7993
}
8094
else {
81-
// apparently this function is the same as
95+
// apparently this function is the same as
8296
// CCDirector::getNextScene so yeah
8397
return m_pUserObject;
8498
}
@@ -224,7 +238,7 @@ class NodeQuery final {
224238
}
225239
collectedID.push_back(c);
226240
}
227-
// Any other character is syntax error due to needing to reserve
241+
// Any other character is syntax error due to needing to reserve
228242
// stuff for possible future features
229243
else {
230244
return Err("Unexpected character '{}' at index {}", c, i);

0 commit comments

Comments
 (0)