性能优化

This commit is contained in:
Nomango 2018-07-22 21:22:27 +08:00
parent e3cce89552
commit 26ed76ae2b
18 changed files with 98 additions and 168 deletions

View File

@ -16,7 +16,7 @@ e2d::Animate::Animate(Animation * animation)
e2d::Animate::~Animate() e2d::Animate::~Animate()
{ {
GC::safeRelease(_animation); GC::getInstance()->safeRelease(_animation);
} }
e2d::Animation * e2d::Animate::getAnimation() const e2d::Animation * e2d::Animate::getAnimation() const
@ -28,9 +28,9 @@ void e2d::Animate::setAnimation(Animation * animation)
{ {
if (animation && animation != _animation) if (animation && animation != _animation)
{ {
GC::safeRelease(_animation); if (_animation) _animation->release();
_animation = animation; _animation = animation;
GC::retain(_animation); _animation->retain();
} }
} }

View File

@ -26,7 +26,7 @@ e2d::Animation::~Animation()
{ {
for (auto frame : _frames) for (auto frame : _frames)
{ {
GC::safeRelease(frame); GC::getInstance()->safeRelease(frame);
} }
} }
@ -40,7 +40,7 @@ void e2d::Animation::add(Image * frame)
if (frame) if (frame)
{ {
_frames.push_back(frame); _frames.push_back(frame);
GC::retain(frame); frame->retain();
} }
} }

View File

@ -11,13 +11,13 @@ e2d::Loop::Loop(Action * action, int times /* = -1 */)
if (action) if (action)
{ {
_action = action; _action = action;
GC::retain(_action); _action->retain();
} }
} }
e2d::Loop::~Loop() e2d::Loop::~Loop()
{ {
GC::safeRelease(_action); GC::getInstance()->safeRelease(_action);
} }
e2d::Loop * e2d::Loop::clone() const e2d::Loop * e2d::Loop::clone() const

View File

@ -15,7 +15,7 @@ e2d::Sequence::~Sequence()
{ {
for (auto action : _actions) for (auto action : _actions)
{ {
GC::safeRelease(action); GC::getInstance()->safeRelease(action);
} }
} }
@ -78,7 +78,7 @@ void e2d::Sequence::add(Action * action)
if (action) if (action)
{ {
_actions.push_back(action); _actions.push_back(action);
GC::retain(action); action->retain();
} }
} }

View File

@ -13,7 +13,7 @@ e2d::Spawn::~Spawn()
{ {
for (auto action : _actions) for (auto action : _actions)
{ {
GC::safeRelease(action); GC::getInstance()->safeRelease(action);
} }
} }
@ -76,7 +76,7 @@ void e2d::Spawn::add(Action * action)
if (action) if (action)
{ {
_actions.push_back(action); _actions.push_back(action);
GC::retain(action); action->retain();
} }
} }

View File

@ -11,17 +11,7 @@ void * operator new(size_t size, e2d::autorelease_t const &) E2D_NOEXCEPT
void* p = ::operator new(size, std::nothrow); void* p = ::operator new(size, std::nothrow);
if (p) if (p)
{ {
GC::autorelease(static_cast<Ref*>(p)); GC::getInstance()->autorelease(static_cast<Ref*>(p));
}
return p;
}
void* operator new[](size_t size, e2d::autorelease_t const&) E2D_NOEXCEPT
{
void* p = ::operator new[](size, std::nothrow);
if (p)
{
GC::autoreleaseArray(static_cast<Ref*>(p));
} }
return p; return p;
} }
@ -31,11 +21,6 @@ void operator delete(void * block, e2d::autorelease_t const &) E2D_NOEXCEPT
::operator delete (block, std::nothrow); ::operator delete (block, std::nothrow);
} }
void operator delete[](void* block, e2d::autorelease_t const&) E2D_NOEXCEPT
{
::operator delete[](block, std::nothrow);
}
// GC 机制,用于销毁所有单例 // GC 机制,用于销毁所有单例
GC GC::_instance; GC GC::_instance;
@ -50,7 +35,7 @@ e2d::GC::GC()
e2d::GC::~GC() e2d::GC::~GC()
{ {
// 删除所有对象 // 删除所有对象
GC::clear(); this->clear();
// 清除图片缓存 // 清除图片缓存
Image::clearCache(); Image::clearCache();
// 删除所有单例 // 删除所有单例
@ -68,23 +53,16 @@ e2d::GC::~GC()
void e2d::GC::flush() void e2d::GC::flush()
{ {
if (!_instance._notifyed) if (!_notifyed)
return; return;
_instance._notifyed = false; _notifyed = false;
for (auto iter = _instance._pool.begin(); iter != _instance._pool.end();) for (auto iter = _pool.begin(); iter != _pool.end();)
{ {
if ((*iter).first->getRefCount() <= 0) if ((*iter)->getRefCount() <= 0)
{ {
if ((*iter).second) delete (*iter);
{ iter = _pool.erase(iter);
delete[] (*iter).first;
}
else
{
delete (*iter).first;
}
iter = _instance._pool.erase(iter);
} }
else else
{ {
@ -95,72 +73,45 @@ void e2d::GC::flush()
void e2d::GC::clear() void e2d::GC::clear()
{ {
_instance._cleanup = true; _cleanup = true;
SceneManager::getInstance()->clear(); SceneManager::getInstance()->clear();
Timer::getInstance()->clearAllTasks(); Timer::getInstance()->clearAllTasks();
ActionManager::getInstance()->clearAll(); ActionManager::getInstance()->clearAll();
for (auto pair : _instance._pool) for (auto ref : _pool)
{ {
if (pair.second) delete ref;
}
_pool.clear();
_cleanup = false;
}
e2d::GC * e2d::GC::getInstance()
{ {
delete[] pair.first; return &_instance;
}
else
{
delete pair.first;
}
}
_instance._pool.clear();
_instance._cleanup = false;
} }
void e2d::GC::autorelease(Ref * ref) void e2d::GC::autorelease(Ref * ref)
{ {
if (ref) if (ref)
{ {
auto iter = _instance._pool.find(ref); _pool.insert(ref);
if (iter == _instance._pool.end())
{
_instance._pool.insert(std::make_pair(ref, false));
}
} }
} }
void e2d::GC::autoreleaseArray(Ref * ref) void e2d::GC::safeRelease(Ref* ref)
{ {
if (_cleanup)
return;
if (ref) if (ref)
{ {
auto iter = _instance._pool.find(ref); auto iter = _pool.find(ref);
if (iter == _instance._pool.end()) if (iter != _pool.end())
{ {
_instance._pool.insert(std::make_pair(ref, true)); (*iter)->release();
} _notifyed = true;
}
}
void e2d::GC::retain(Ref * ref)
{
if (ref && !_instance._cleanup)
{
auto iter = _instance._pool.find(ref);
if (iter != _instance._pool.end())
{
(*iter).first->retain();
}
}
}
void e2d::GC::release(Ref * ref)
{
if (ref && !_instance._cleanup)
{
auto iter = _instance._pool.find(ref);
if (iter != _instance._pool.end())
{
(*iter).first->release();
_instance._notifyed = true;
} }
} }
} }

View File

@ -20,7 +20,7 @@ e2d::Game::Game()
e2d::Game::~Game() e2d::Game::~Game()
{ {
GC::safeRelease(_config); GC::getInstance()->safeRelease(_config);
CoUninitialize(); CoUninitialize();
} }
@ -43,6 +43,7 @@ void e2d::Game::destroyInstance()
void e2d::Game::start() void e2d::Game::start()
{ {
auto gc = GC::getInstance();
auto input = Input::getInstance(); auto input = Input::getInstance();
auto window = Window::getInstance(); auto window = Window::getInstance();
auto renderer = Renderer::getInstance(); auto renderer = Renderer::getInstance();
@ -80,7 +81,7 @@ void e2d::Game::start()
if (frameInterval < interval) if (frameInterval < interval)
{ {
_last = _now; _last += interval;
input->update(); input->update();
timer->update(); timer->update();
@ -89,7 +90,7 @@ void e2d::Game::start()
_config->_update(); _config->_update();
renderer->render(); renderer->render();
window->poll(); window->poll();
GC::flush(); gc->flush();
} }
else else
{ {
@ -127,10 +128,10 @@ void e2d::Game::setConfig(Config* config)
{ {
if (config && _config != config) if (config && _config != config)
{ {
GC::release(_config); if (_config) _config->release();
_config = config; _config = config;
_config->_unconfigured = true; _config->_unconfigured = true;
GC::retain(_config); _config->retain();
} }
} }
@ -153,7 +154,7 @@ void e2d::Game::quit()
void e2d::Game::cleanup() void e2d::Game::cleanup()
{ {
GC::clear(); GC::getInstance()->clear();
Image::clearCache(); Image::clearCache();
Player::getInstance()->clearCache(); Player::getInstance()->clearCache();
} }

View File

@ -7,13 +7,13 @@ e2d::Scene::Scene()
, _root(nullptr) , _root(nullptr)
{ {
_root = new (e2d::autorelease) Node(); _root = new (e2d::autorelease) Node();
GC::retain(_root); _root->retain();
_root->_setParentScene(this); _root->_setParentScene(this);
} }
e2d::Scene::~Scene() e2d::Scene::~Scene()
{ {
GC::safeRelease(_root); GC::getInstance()->safeRelease(_root);
} }
void e2d::Scene::render() void e2d::Scene::render()

View File

@ -20,7 +20,13 @@ time_t e2d::Time::getTimeStamp() const
e2d::Time e2d::Time::operator+(Duration const & other) const e2d::Time e2d::Time::operator+(Duration const & other) const
{ {
return std::move(Time(_timePoint - milliseconds(other.milliseconds()))); return std::move(Time(_timePoint + milliseconds(other.milliseconds())));
}
e2d::Time & e2d::Time::operator+=(Duration const & other)
{
_timePoint += milliseconds(other.milliseconds());
return (*this);
} }
e2d::Duration e2d::Time::operator-(Time const & other) const e2d::Duration e2d::Time::operator-(Time const & other) const

View File

@ -41,7 +41,7 @@ void e2d::VoiceCallback::OnBufferEnd(void * pBufferContext)
if (_music) if (_music)
{ {
GC::release(_music); _music->release();
} }
} }
@ -49,7 +49,7 @@ void e2d::VoiceCallback::OnBufferStart(void * pBufferContext)
{ {
if (_music) if (_music)
{ {
GC::retain(_music); _music->retain();
} }
} }

View File

@ -43,7 +43,7 @@ void e2d::ActionManager::update()
// »ñÈ¡¶¯×÷ÔËÐÐ״̬ // »ñÈ¡¶¯×÷ÔËÐÐ״̬
if (action->_isDone()) if (action->_isDone())
{ {
GC::release(action); action->release();
action->_target = nullptr; action->_target = nullptr;
_runningActions.erase(_runningActions.begin() + i); _runningActions.erase(_runningActions.begin() + i);
} }
@ -136,7 +136,7 @@ void e2d::ActionManager::start(Action * action, Node * target, bool paused)
auto iter = std::find(_runningActions.begin(), _runningActions.end(), action); auto iter = std::find(_runningActions.begin(), _runningActions.end(), action);
if (iter == _runningActions.end()) if (iter == _runningActions.end())
{ {
GC::retain(action); action->retain();
action->_startWithTarget(target); action->_startWithTarget(target);
action->_running = !paused; action->_running = !paused;
_runningActions.push_back(action); _runningActions.push_back(action);
@ -197,10 +197,10 @@ void e2d::ActionManager::clearAllBindedWith(Node * target)
{ {
for (size_t i = 0; i < _runningActions.size();) for (size_t i = 0; i < _runningActions.size();)
{ {
auto a = _runningActions[i]; auto action = _runningActions[i];
if (a->getTarget() == target) if (action->getTarget() == target)
{ {
GC::safeRelease(a); action->release();
_runningActions.erase(_runningActions.begin() + i); _runningActions.erase(_runningActions.begin() + i);
} }
else else
@ -217,7 +217,7 @@ void e2d::ActionManager::clearAll()
{ {
for (auto action : _runningActions) for (auto action : _runningActions)
{ {
GC::release(action); action->release();
} }
_runningActions.clear(); _runningActions.clear();
} }

View File

@ -41,7 +41,7 @@ void e2d::SceneManager::push(Scene * scene, Transition * transition /* = nullptr
// 保存下一场景的指针 // 保存下一场景的指针
_nextScene = scene; _nextScene = scene;
GC::retain(_nextScene); _nextScene->retain();
// 设置切换场景动作 // 设置切换场景动作
if (transition) if (transition)
@ -49,10 +49,10 @@ void e2d::SceneManager::push(Scene * scene, Transition * transition /* = nullptr
if (_transition) if (_transition)
{ {
_transition->_stop(); _transition->_stop();
GC::release(_transition); _transition->release();
} }
_transition = transition; _transition = transition;
GC::retain(transition); transition->retain();
transition->_init(_currScene, _nextScene); transition->_init(_currScene, _nextScene);
transition->_update(); transition->_update();
} }
@ -85,7 +85,7 @@ void e2d::SceneManager::pop(Transition * transition /* = nullptr */)
// 设置切换场景动作 // 设置切换场景动作
if (transition) if (transition)
{ {
GC::retain(transition); transition->retain();
transition->_init(_currScene, _nextScene); transition->_init(_currScene, _nextScene);
transition->_update(); transition->_update();
_transition = transition; _transition = transition;
@ -134,7 +134,7 @@ void e2d::SceneManager::update()
if (_transition->isDone()) if (_transition->isDone())
{ {
GC::release(_transition); _transition->release();
_transition = nullptr; _transition = nullptr;
} }
else else
@ -158,15 +158,15 @@ void e2d::SceneManager::update()
} }
else else
{ {
GC::release(_currScene); _currScene->release();
} }
} }
// 执行下一场景的 onEnter 函数 // 执行下一场景的 onEnter 函数
_nextScene->onEnter(); _nextScene->onEnter();
_currScene = _nextScene; // 切换场景 _currScene = _nextScene;
_nextScene = nullptr; // 下一场景置空 _nextScene = nullptr;
} }
} }

View File

@ -81,7 +81,7 @@ e2d::Node::~Node()
ActionManager::getInstance()->clearAllBindedWith(this); ActionManager::getInstance()->clearAllBindedWith(this);
for (auto child : _children) for (auto child : _children)
{ {
GC::release(child); GC::getInstance()->safeRelease(child);
} }
} }
@ -713,7 +713,7 @@ void e2d::Node::addChild(Node * child, int order /* = 0 */)
} }
} }
GC::retain(child); child->retain();
_children.push_back(child); _children.push_back(child);
child->setOrder(order); child->setOrder(order);
child->_parent = this; child->_parent = this;
@ -820,7 +820,7 @@ bool e2d::Node::removeChild(Node * child)
child->_setParentScene(nullptr); child->_setParentScene(nullptr);
} }
GC::release(child); child->release();
return true; return true;
} }
} }
@ -851,7 +851,7 @@ void e2d::Node::removeChildren(const String& childName)
{ {
child->_setParentScene(nullptr); child->_setParentScene(nullptr);
} }
GC::release(child); child->release();
} }
} }
} }
@ -861,7 +861,7 @@ void e2d::Node::removeAllChildren()
// 所有节点的引用计数减一 // 所有节点的引用计数减一
for (auto child : _children) for (auto child : _children)
{ {
GC::release(child); child->release();
} }
// 清空储存节点的容器 // 清空储存节点的容器
_children.clear(); _children.clear();

View File

@ -40,16 +40,16 @@ e2d::Sprite::Sprite(const String & fileName, const Rect & cropRect)
e2d::Sprite::~Sprite() e2d::Sprite::~Sprite()
{ {
GC::safeRelease(_image); GC::getInstance()->safeRelease(_image);
} }
bool e2d::Sprite::open(Image * image) bool e2d::Sprite::open(Image * image)
{ {
if (image) if (image)
{ {
GC::release(_image); if (_image) _image->release();
_image = image; _image = image;
GC::retain(_image); _image->retain();
Node::setSize(_image->getWidth(), _image->getHeight()); Node::setSize(_image->getWidth(), _image->getHeight());
return true; return true;
@ -62,7 +62,7 @@ bool e2d::Sprite::open(const Resource& res)
if (!_image) if (!_image)
{ {
_image = new (e2d::autorelease) Image(); _image = new (e2d::autorelease) Image();
GC::retain(_image); _image->retain();
} }
if (_image->open(res)) if (_image->open(res))

View File

@ -34,7 +34,7 @@ void e2d::Timer::addTask(Task * task)
auto iter = std::find(_tasks.begin(), _tasks.end(), task); auto iter = std::find(_tasks.begin(), _tasks.end(), task);
if (iter == _tasks.end()) if (iter == _tasks.end())
{ {
GC::retain(task); task->retain();
task->updateTime(); task->updateTime();
_tasks.push_back(task); _tasks.push_back(task);
} }
@ -105,7 +105,7 @@ void e2d::Timer::clearAllTasks()
for (auto task : _tasks) for (auto task : _tasks)
{ {
GC::release(task); task->release();
} }
_tasks.clear(); _tasks.clear();
} }
@ -121,7 +121,7 @@ void e2d::Timer::update()
// 清除已停止的任务 // 清除已停止的任务
if (task->_stopped) if (task->_stopped)
{ {
GC::release(task); task->release();
_tasks.erase(_tasks.begin() + i); _tasks.erase(_tasks.begin() + i);
} }
else else

View File

@ -20,8 +20,8 @@ e2d::Transition::~Transition()
{ {
SafeRelease(_outLayer); SafeRelease(_outLayer);
SafeRelease(_inLayer); SafeRelease(_inLayer);
GC::safeRelease(_outScene); GC::getInstance()->safeRelease(_outScene);
GC::safeRelease(_inScene); GC::getInstance()->safeRelease(_inScene);
} }
bool e2d::Transition::isDone() bool e2d::Transition::isDone()
@ -48,8 +48,8 @@ void e2d::Transition::_init(Scene * prev, Scene * next)
_last = Game::getInstance()->getTotalDuration(); _last = Game::getInstance()->getTotalDuration();
_outScene = prev; _outScene = prev;
_inScene = next; _inScene = next;
GC::retain(_outScene); if (_outScene) _outScene->retain();
GC::retain(_inScene); if (_inScene) _inScene->retain();
_windowSize = Window::getInstance()->getSize(); _windowSize = Window::getInstance()->getSize();
_outLayerParam = _inLayerParam = D2D1::LayerParameters(); _outLayerParam = _inLayerParam = D2D1::LayerParameters();

View File

@ -361,42 +361,24 @@ private:
class GC class GC
{ {
public: public:
// 获取 GC 实例
static GC * getInstance();
// 自动释放 // 自动释放
static void autorelease( void autorelease(
Ref* ref
);
// 自动释放数组
static void autoreleaseArray(
Ref* ref
);
// 保留
static void retain(
Ref* ref
);
// 释放
static void release(
Ref* ref Ref* ref
); );
// 安全地释放对象 // 安全地释放对象
template <typename Type> void safeRelease(
static inline void safeRelease(Type*& p) Ref* ref
{ );
if (p != nullptr)
{
GC::release(p);
p = nullptr;
}
}
// 刷新内存池 // 刷新内存池
static void flush(); void flush();
// 回收内存池中的所有对象 // 回收内存池中的所有对象
static void clear(); void clear();
private: private:
GC(); GC();
@ -408,7 +390,7 @@ private:
private: private:
bool _notifyed; bool _notifyed;
bool _cleanup; bool _cleanup;
std::map<Ref*, bool> _pool; std::set<Ref*> _pool;
static GC _instance; static GC _instance;
}; };

View File

@ -452,9 +452,9 @@ public:
// 获取时间戳 // 获取时间戳
time_t getTimeStamp() const; time_t getTimeStamp() const;
// 计算时间间隔后的时间点
Time operator + (Duration const & other) const; Time operator + (Duration const & other) const;
// 计算两时间点的时间间隔 Time& operator += (Duration const &);
Duration operator - (Time const & other) const; Duration operator - (Time const & other) const;
// 获取当前时间 // 获取当前时间
@ -1161,17 +1161,7 @@ void* operator new(
e2d::autorelease_t const& e2d::autorelease_t const&
) E2D_NOEXCEPT; ) E2D_NOEXCEPT;
void* operator new[](
size_t size,
e2d::autorelease_t const&
) E2D_NOEXCEPT;
void operator delete( void operator delete(
void* block, void* block,
e2d::autorelease_t const& e2d::autorelease_t const&
) E2D_NOEXCEPT; ) E2D_NOEXCEPT;
void operator delete[](
void* block,
e2d::autorelease_t const&
) E2D_NOEXCEPT;