Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions core/2d/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ THE SOFTWARE.
# define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
#endif

/*
* 4.5x faster than std::hash in release mode
*/
#define AX_HASH_NODE_NAME(name) (!name.empty() ? XXH3_64bits(name.data(), name.length()) : 0)

namespace ax
{
uint64_t hashNodeName(std::string_view name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint64_t hashNodeName(std::string_view name)
AX_DLL uint64_t hashNodeName(std::string_view name)

{
// 4.5x faster than std::hash in release mode.
return !name.empty() ? XXH3_64bits(name.data(), name.length()) : 0;
}

// FIXME:: Yes, nodes might have a sort problem once every 30 days if the game runs at 60 FPS and each frame sprites are
// reordered.
Expand Down Expand Up @@ -717,6 +717,11 @@ void Node::setName(std::string_view name)
_name = name;
}

uint64_t Node::getHashOfName() const
{
return _hashOfName;
}

void Node::updateParentChildrenIndexer(int tag)
{
auto parentChildrenIndexer = getParentChildrenIndexer();
Expand All @@ -730,11 +735,11 @@ void Node::updateParentChildrenIndexer(int tag)

void Node::updateParentChildrenIndexer(std::string_view name)
{
uint64_t newHash = AX_HASH_NODE_NAME(name);
uint64_t newHash = hashNodeName(name);
auto parentChildrenIndexer = getParentChildrenIndexer();
if (parentChildrenIndexer)
{
auto oldHash = AX_HASH_NODE_NAME(_name);
auto oldHash = hashNodeName(_name);
if (oldHash != newHash)
parentChildrenIndexer->erase(oldHash);
(*parentChildrenIndexer)[newHash] = this;
Expand Down Expand Up @@ -831,7 +836,7 @@ Node* Node::getChildByTag(int tag) const
Node* Node::getChildByName(std::string_view name) const
{
// AXASSERT(!name.empty(), "Invalid name");
auto hash = AX_HASH_NODE_NAME(name);
auto hash = hashNodeName(name);
if (_childrenIndexer)
{
auto it = _childrenIndexer->find(hash);
Expand Down
4 changes: 4 additions & 0 deletions core/2d/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class EventListener;

typedef std::map<uint64_t, Node*> NodeIndexerMap_t;

uint64_t hashNodeName(std::string_view);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint64_t hashNodeName(std::string_view);
AX_DLL uint64_t hashNodeName(std::string_view);


/** @class Node
* @brief Node is the base element of the Scene Graph. Elements of the Scene Graph must be Node objects or subclasses of
it. The most common Node objects are: Scene, Layer, Sprite, Menu, Label.
Expand Down Expand Up @@ -981,6 +983,8 @@ class AX_DLL Node : public Object
*/
virtual void setName(std::string_view name);

uint64_t getHashOfName() const;

/**
* Returns a custom user data pointer.
*
Expand Down
Loading