Add comment for new API Director::setChildrenIndexerEnabled [skip ci]

This commit is contained in:
halx99 2021-11-09 12:15:28 +08:00
parent 879c91e1ee
commit f1425c1fd3
5 changed files with 153 additions and 43 deletions

View File

@ -34,6 +34,7 @@ THE SOFTWARE.
#include <string> #include <string>
#include <regex> #include <regex>
#include "xxhash.h"
#include "base/CCDirector.h" #include "base/CCDirector.h"
#include "base/CCScheduler.h" #include "base/CCScheduler.h"
#include "base/CCEventDispatcher.h" #include "base/CCEventDispatcher.h"
@ -53,6 +54,10 @@ THE SOFTWARE.
#define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__)) #define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
#endif #endif
/*
* 4.5x faster than std::hash in release mode
*/
#define CC_HASH_NODE_NAME(name) (!name.empty() ? XXH3_64bits(name.c_str(), name.length()) : 0)
NS_CC_BEGIN NS_CC_BEGIN
@ -84,13 +89,14 @@ Node::Node()
, _additionalTransformDirty(false) , _additionalTransformDirty(false)
, _transformUpdated(true) , _transformUpdated(true)
// children (lazy allocs) // children (lazy allocs)
, _childrenIndexer(nullptr)
// lazy alloc // lazy alloc
, _localZOrder$Arrival(0LL) , _localZOrder$Arrival(0LL)
, _globalZOrder(0) , _globalZOrder(0)
, _parent(nullptr) , _parent(nullptr)
// "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true // "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true
, _tag(Node::INVALID_TAG) , _tag(Node::INVALID_TAG)
, _name("") , _name()
, _hashOfName(0) , _hashOfName(0)
// userData is always inited as nil // userData is always inited as nil
, _userData(nullptr) , _userData(nullptr)
@ -149,6 +155,8 @@ Node * Node::create()
Node::~Node() Node::~Node()
{ {
CCLOGINFO( "deallocing Node: %p - tag: %i", this, _tag ); CCLOGINFO( "deallocing Node: %p - tag: %i", this, _tag );
CC_SAFE_DELETE(_childrenIndexer);
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_updateScriptHandler) if (_updateScriptHandler)
@ -680,7 +688,15 @@ int Node::getTag() const
/// tag setter /// tag setter
void Node::setTag(int tag) void Node::setTag(int tag)
{ {
_tag = tag ; auto parentChildrenIndexer = getParentChildrenIndexer();
if (parentChildrenIndexer)
{
if (_tag != tag)
parentChildrenIndexer->erase(_tag);
(*parentChildrenIndexer)[tag] = this;
}
_tag = tag;
} }
const std::string& Node::getName() const const std::string& Node::getName() const
@ -690,9 +706,33 @@ const std::string& Node::getName() const
void Node::setName(const std::string& name) void Node::setName(const std::string& name)
{ {
uint64_t newHash = CC_HASH_NODE_NAME(name);
auto parentChildrenIndexer = getParentChildrenIndexer();
if (parentChildrenIndexer)
{
auto oldHash = CC_HASH_NODE_NAME(_name);
if (oldHash != newHash)
parentChildrenIndexer->erase(oldHash);
(*parentChildrenIndexer)[newHash] = this;
}
_name = name; _name = name;
std::hash<std::string> h; _hashOfName = newHash;
_hashOfName = h(name); }
NodeIndexerMap_t* Node::getParentChildrenIndexer()
{
if (!_director->isChildrenIndexerEnabled())
return nullptr;
auto parent = getParent();
if (parent)
{
auto& indexer = parent->_childrenIndexer;
if (!indexer)
indexer = new NodeIndexerMap_t();
return indexer;
}
return nullptr;
} }
/// userData setter /// userData setter
@ -750,26 +790,41 @@ Node* Node::getChildByTag(int tag) const
{ {
CCASSERT(tag != Node::INVALID_TAG, "Invalid tag"); CCASSERT(tag != Node::INVALID_TAG, "Invalid tag");
for (const auto child : _children) if (_childrenIndexer)
{ {
if(child && child->_tag == tag) auto it = _childrenIndexer->find(tag);
return child; if (it != _childrenIndexer->end())
return it->second;
}
else
{
for (const auto child : _children)
{
if (child && child->_tag == tag)
return child;
}
} }
return nullptr; return nullptr;
} }
Node* Node::getChildByName(const std::string& name) const Node* Node::getChildByName(const std::string& name) const
{ {
CCASSERT(!name.empty(), "Invalid name"); // CCASSERT(!name.empty(), "Invalid name");
auto hash = CC_HASH_NODE_NAME(name);
std::hash<std::string> h; if (_childrenIndexer)
size_t hash = h(name);
for (const auto& child : _children)
{ {
// Different strings may have the same hash code, but can use it to compare first for speed auto it = _childrenIndexer->find(hash);
if(child->_hashOfName == hash && child->_name.compare(name) == 0) if (it != _childrenIndexer->end())
return child; return it->second;
}
else
{
for (const auto& child : _children)
{
// Different strings may have the same hash code, but can use it to compare first for speed
if (child->_hashOfName == hash && child->_name.compare(name) == 0)
return child;
}
} }
return nullptr; return nullptr;
} }
@ -937,14 +992,14 @@ void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::stri
} }
this->insertChild(child, localZOrder); this->insertChild(child, localZOrder);
child->setParent(this);
if (setTag) if (setTag)
child->setTag(tag); child->setTag(tag);
else else
child->setName(name); child->setName(name);
child->setParent(this);
child->updateOrderOfArrival(); child->updateOrderOfArrival();
if( _running ) if( _running )
@ -1056,6 +1111,7 @@ void Node::removeAllChildrenWithCleanup(bool cleanup)
} }
_children.clear(); _children.clear();
CC_SAFE_DELETE(_childrenIndexer);
} }
void Node::resetChild(Node* child, bool cleanup) void Node::resetChild(Node* child, bool cleanup)

View File

@ -82,6 +82,8 @@ enum {
class EventListener; class EventListener;
typedef std::map<uint64_t, Node*> NodeIndexerMap_t;
/** @class Node /** @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. * @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. The most common Node objects are: Scene, Layer, Sprite, Menu, Label.
@ -1841,6 +1843,8 @@ protected:
private: private:
void addChildHelper(Node* child, int localZOrder, int tag, const std::string &name, bool setTag); void addChildHelper(Node* child, int localZOrder, int tag, const std::string &name, bool setTag);
NodeIndexerMap_t* getParentChildrenIndexer();
protected: protected:
float _rotationX; ///< rotation on the X-axis float _rotationX; ///< rotation on the X-axis
@ -1897,12 +1901,13 @@ protected:
static std::uint32_t s_globalOrderOfArrival; static std::uint32_t s_globalOrderOfArrival;
Vector<Node*> _children; ///< array of children nodes Vector<Node*> _children; ///< array of children nodes
NodeIndexerMap_t* _childrenIndexer; ///< The children indexer for fast find child
Node *_parent; ///< weak reference to parent node Node *_parent; ///< weak reference to parent node
Director* _director; //cached director pointer to improve rendering performance Director* _director; //cached director pointer to improve rendering performance
int _tag; ///< a tag. Can be any number you assigned just to identify this node int _tag; ///< a tag. Can be any number you assigned just to identify this node
std::string _name; ///<a string label, an user defined string to identify this node std::string _name; ///<a string label, an user defined string to identify this node
size_t _hashOfName; ///<hash value of _name, used for speed in getChildByName uint64_t _hashOfName; ///<hash value of _name, used for speed in getChildByName
void *_userData; ///< A user assigned void pointer, Can be point to any cpp object void *_userData; ///< A user assigned void pointer, Can be point to any cpp object
Ref *_userObject; ///< A user assigned Object Ref *_userObject; ///< A user assigned Object

View File

@ -4,8 +4,9 @@
Copyright (c) 2011 Zynga Inc. Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd. Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
http://www.cocos2d-x.org https://adxe.org
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -502,6 +503,17 @@ public:
*/ */
const std::thread::id& getCocos2dThreadId() const { return _cocos2d_thread_id; } const std::thread::id& getCocos2dThreadId() const { return _cocos2d_thread_id; }
/** Enable node tree children indexer map, the concept is like database INDEX
* Notes:
* If enable is true: will cost more memory to speed up getChildByTag & getChildByName
*/
void setChildrenIndexerEnabled(bool enable) { _childrenIndexerEnabled = enable; }
/**
* returns whether or not node tree children indexer map enabled
*/
bool isChildrenIndexerEnabled() const { return _childrenIndexerEnabled; }
/** /**
* returns whether or not the Director is in a valid state * returns whether or not the Director is in a valid state
*/ */
@ -638,12 +650,14 @@ protected:
bool _isStatusLabelUpdated = true; bool _isStatusLabelUpdated = true;
/* cocos2d thread id */
std::thread::id _cocos2d_thread_id;
/* whether or not the director is in a valid state */ /* whether or not the director is in a valid state */
bool _invalid = false; bool _invalid = false;
bool _childrenIndexerEnabled = false;
/* cocos2d thread id */
std::thread::id _cocos2d_thread_id;
// GLView will recreate stats labels to fit visible rect // GLView will recreate stats labels to fit visible rect
friend class GLView; friend class GLView;
}; };

View File

@ -45,9 +45,9 @@ Value::Value() : _type(Type::NONE)
memset(&_field, 0, sizeof(_field)); memset(&_field, 0, sizeof(_field));
} }
Value::Value(unsigned char v) : _type(Type::INTEGER) Value::Value(unsigned char v) : _type(Type::BYTE)
{ {
_field.uintVal = v; _field.byteVal = v;
} }
Value::Value(int v) : _type(Type::INTEGER) Value::Value(int v) : _type(Type::INTEGER)
@ -57,7 +57,7 @@ Value::Value(int v) : _type(Type::INTEGER)
Value::Value(unsigned int v) : _type(Type::UNSIGNED) Value::Value(unsigned int v) : _type(Type::UNSIGNED)
{ {
_field.uintVal = v; _field.unsignedVal = v;
} }
Value::Value(float v) : _type(Type::FLOAT) Value::Value(float v) : _type(Type::FLOAT)
@ -143,11 +143,14 @@ Value& Value::operator=(const Value& other)
switch (other._type) switch (other._type)
{ {
case Type::BYTE:
_field.byteVal = other._field.byteVal;
break;
case Type::INTEGER: case Type::INTEGER:
_field.intVal = other._field.intVal; _field.intVal = other._field.intVal;
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
_field.uintVal = other._field.uintVal; _field.unsignedVal = other._field.unsignedVal;
break; break;
case Type::FLOAT: case Type::FLOAT:
_field.floatVal = other._field.floatVal; _field.floatVal = other._field.floatVal;
@ -200,11 +203,14 @@ Value& Value::operator=(Value&& other)
clear(); clear();
switch (other._type) switch (other._type)
{ {
case Type::BYTE:
_field.byteVal = other._field.byteVal;
break;
case Type::INTEGER: case Type::INTEGER:
_field.intVal = other._field.intVal; _field.intVal = other._field.intVal;
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
_field.uintVal = other._field.uintVal; _field.unsignedVal = other._field.unsignedVal;
break; break;
case Type::FLOAT: case Type::FLOAT:
_field.floatVal = other._field.floatVal; _field.floatVal = other._field.floatVal;
@ -241,8 +247,8 @@ Value& Value::operator=(Value&& other)
Value& Value::operator=(unsigned char v) Value& Value::operator=(unsigned char v)
{ {
reset(Type::UNSIGNED); reset(Type::BYTE);
_field.uintVal = v; _field.byteVal = v;
return *this; return *this;
} }
@ -256,7 +262,7 @@ Value& Value::operator=(int v)
Value& Value::operator=(unsigned int v) Value& Value::operator=(unsigned int v)
{ {
reset(Type::UNSIGNED); reset(Type::UNSIGNED);
_field.uintVal = v; _field.unsignedVal = v;
return *this; return *this;
} }
@ -368,10 +374,12 @@ bool Value::operator==(const Value& v) const
return true; return true;
switch (_type) switch (_type)
{ {
case Type::BYTE:
return v._field.byteVal == this->_field.byteVal;
case Type::INTEGER: case Type::INTEGER:
return v._field.intVal == this->_field.intVal; return v._field.intVal == this->_field.intVal;
case Type::UNSIGNED: case Type::UNSIGNED:
return v._field.uintVal == this->_field.uintVal; return v._field.unsignedVal == this->_field.unsignedVal;
case Type::BOOLEAN: case Type::BOOLEAN:
return v._field.boolVal == this->_field.boolVal; return v._field.boolVal == this->_field.boolVal;
case Type::STRING: case Type::STRING:
@ -439,11 +447,13 @@ unsigned char Value::asByte(unsigned char defaultValue) const
switch (_type) switch (_type)
{ {
case Type::BYTE:
return _field.byteVal;
case Type::INTEGER: case Type::INTEGER:
return static_cast<unsigned char>(_field.intVal); return static_cast<unsigned char>(_field.intVal);
case Type::UNSIGNED: case Type::UNSIGNED:
return static_cast<unsigned char>(_field.uintVal); return static_cast<unsigned char>(_field.unsignedVal);
case Type::STRING: case Type::STRING:
return static_cast<unsigned char>(atoi(_field.strVal->c_str())); return static_cast<unsigned char>(atoi(_field.strVal->c_str()));
@ -472,8 +482,11 @@ int Value::asInt(int defaultValue) const
return _field.intVal; return _field.intVal;
case Type::UNSIGNED: case Type::UNSIGNED:
CCASSERT(_field.uintVal < INT_MAX, "Can only convert values < INT_MAX"); CCASSERT(_field.unsignedVal < INT_MAX, "Can only convert values < INT_MAX");
return (int)_field.uintVal; return (int)_field.unsignedVal;
case Type::BYTE:
return _field.byteVal;
case Type::STRING: case Type::STRING:
return atoi(_field.strVal->c_str()); return atoi(_field.strVal->c_str());
@ -499,12 +512,15 @@ unsigned int Value::asUnsignedInt(unsigned int defaultValue) const
switch (_type) switch (_type)
{ {
case Type::UNSIGNED: case Type::UNSIGNED:
return _field.uintVal; return _field.unsignedVal;
case Type::INTEGER: case Type::INTEGER:
CCASSERT(_field.intVal >= 0, "Only values >= 0 can be converted to unsigned"); CCASSERT(_field.intVal >= 0, "Only values >= 0 can be converted to unsigned");
return static_cast<unsigned int>(_field.intVal); return static_cast<unsigned int>(_field.intVal);
case Type::BYTE:
return static_cast<unsigned int>(_field.byteVal);
case Type::STRING: case Type::STRING:
// NOTE: strtoul is required (need to augment on unsupported platforms) // NOTE: strtoul is required (need to augment on unsupported platforms)
return static_cast<unsigned int>(strtoul(_field.strVal->c_str(), nullptr, 10)); return static_cast<unsigned int>(strtoul(_field.strVal->c_str(), nullptr, 10));
@ -532,6 +548,9 @@ float Value::asFloat(float defaultValue) const
case Type::FLOAT: case Type::FLOAT:
return _field.floatVal; return _field.floatVal;
case Type::BYTE:
return static_cast<float>(_field.byteVal);
case Type::STRING: case Type::STRING:
return static_cast<float>(utils::atof(_field.strVal->c_str())); return static_cast<float>(utils::atof(_field.strVal->c_str()));
@ -539,7 +558,7 @@ float Value::asFloat(float defaultValue) const
return static_cast<float>(_field.intVal); return static_cast<float>(_field.intVal);
case Type::UNSIGNED: case Type::UNSIGNED:
return static_cast<float>(_field.uintVal); return static_cast<float>(_field.unsignedVal);
case Type::DOUBLE: case Type::DOUBLE:
return static_cast<float>(_field.doubleVal); return static_cast<float>(_field.doubleVal);
@ -561,6 +580,9 @@ double Value::asDouble(double defaultValue) const
case Type::DOUBLE: case Type::DOUBLE:
return _field.doubleVal; return _field.doubleVal;
case Type::BYTE:
return static_cast<double>(_field.byteVal);
case Type::STRING: case Type::STRING:
return static_cast<double>(utils::atof(_field.strVal->c_str())); return static_cast<double>(utils::atof(_field.strVal->c_str()));
@ -568,7 +590,7 @@ double Value::asDouble(double defaultValue) const
return static_cast<double>(_field.intVal); return static_cast<double>(_field.intVal);
case Type::UNSIGNED: case Type::UNSIGNED:
return static_cast<double>(_field.uintVal); return static_cast<double>(_field.unsignedVal);
case Type::FLOAT: case Type::FLOAT:
return static_cast<double>(_field.floatVal); return static_cast<double>(_field.floatVal);
@ -590,6 +612,9 @@ bool Value::asBool(bool defaultValue) const
case Type::BOOLEAN: case Type::BOOLEAN:
return _field.boolVal; return _field.boolVal;
case Type::BYTE:
return _field.byteVal == 0 ? false : true;
case Type::STRING: case Type::STRING:
return (*_field.strVal == "0" || *_field.strVal == "false") ? false : true; return (*_field.strVal == "0" || *_field.strVal == "false") ? false : true;
@ -597,7 +622,7 @@ bool Value::asBool(bool defaultValue) const
return _field.intVal == 0 ? false : true; return _field.intVal == 0 ? false : true;
case Type::UNSIGNED: case Type::UNSIGNED:
return _field.uintVal == 0 ? false : true; return _field.unsignedVal == 0 ? false : true;
case Type::FLOAT: case Type::FLOAT:
return _field.floatVal == 0.0f ? false : true; return _field.floatVal == 0.0f ? false : true;
@ -629,11 +654,14 @@ std::string Value::asString() const
size_t n = 0; size_t n = 0;
switch (_type) switch (_type)
{ {
case Type::BYTE:
ret = std::to_string(_field.byteVal);
break;
case Type::INTEGER: case Type::INTEGER:
ret = std::to_string(_field.intVal); ret = std::to_string(_field.intVal);
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
ret = std::to_string(_field.uintVal); ret = std::to_string(_field.unsignedVal);
break; break;
case Type::FLOAT: case Type::FLOAT:
ret.resize(NUMBER_MAX_DIGITS); ret.resize(NUMBER_MAX_DIGITS);
@ -763,6 +791,7 @@ static std::string visit(const Value& v, int depth)
switch (v.getType()) switch (v.getType())
{ {
case Value::Type::NONE: case Value::Type::NONE:
case Value::Type::BYTE:
case Value::Type::INTEGER: case Value::Type::INTEGER:
case Value::Type::UNSIGNED: case Value::Type::UNSIGNED:
case Value::Type::FLOAT: case Value::Type::FLOAT:
@ -800,11 +829,14 @@ void Value::clear()
// Free memory the old value allocated // Free memory the old value allocated
switch (_type) switch (_type)
{ {
case Type::BYTE:
_field.byteVal = 0;
break;
case Type::INTEGER: case Type::INTEGER:
_field.intVal = 0; _field.intVal = 0;
break; break;
case Type::UNSIGNED: case Type::UNSIGNED:
_field.uintVal = 0u; _field.unsignedVal = 0u;
break; break;
case Type::FLOAT: case Type::FLOAT:
_field.floatVal = 0.0f; _field.floatVal = 0.0f;

View File

@ -203,6 +203,8 @@ public:
{ {
/// no value is wrapped, an empty Value /// no value is wrapped, an empty Value
NONE = 0, NONE = 0,
/// wrap byte
BYTE,
/// wrap integer /// wrap integer
INTEGER, INTEGER,
/// wrap unsigned /// wrap unsigned
@ -235,8 +237,9 @@ private:
union union
{ {
unsigned char byteVal;
int intVal; int intVal;
unsigned int uintVal; unsigned int unsignedVal;
float floatVal; float floatVal;
double doubleVal; double doubleVal;
bool boolVal; bool boolVal;