Skip to content

Commit 49375e7

Browse files
committed
use block allocation for commands
1 parent d6e352a commit 49375e7

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

cocos/2d/renderer/RenderCommandPool.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,25 @@ class RenderCommandPool
2727
// {
2828
// CCLOG("All RenderCommand should not be used when Pool is released!");
2929
// }
30-
for (typename std::list<T*>::iterator iter = _freePool.begin(); iter != _freePool.end(); ++iter)
30+
_freePool.clear();
31+
for (typename std::list<T*>::iterator iter = _allocatedPoolBlocks.begin(); iter != _allocatedPoolBlocks.end(); ++iter)
3132
{
32-
delete *iter;
33+
delete[] *iter;
3334
*iter = nullptr;
3435
}
35-
_freePool.clear();
36+
_allocatedPoolBlocks.clear();
3637
}
3738

3839
public:
3940
T* generateCommand()
4041
{
4142
T* result = nullptr;
42-
if(0 == _freePool.size())
43+
if(_freePool.empty())
4344
{
44-
result = new T();
45-
}
46-
else
47-
{
48-
result = _freePool.front();
49-
_freePool.pop_front();
45+
AllocateCommands();
5046
}
47+
result = _freePool.front();
48+
_freePool.pop_front();
5149
//_usedPool.insert(result);
5250
return result;
5351
}
@@ -64,8 +62,19 @@ class RenderCommandPool
6462
//_usedPool.erase(ptr);
6563

6664
}
67-
6865
private:
66+
void AllocateCommands()
67+
{
68+
static const int COMMANDS_ALLOCATE_BLOCK_SIZE = 32;
69+
T* commands = new T[COMMANDS_ALLOCATE_BLOCK_SIZE];
70+
_allocatedPoolBlocks.push_back(commands);
71+
for(int index = 0; index < COMMANDS_ALLOCATE_BLOCK_SIZE; ++index)
72+
{
73+
_freePool.push_back(commands+index);
74+
}
75+
}
76+
private:
77+
std::list<T*> _allocatedPoolBlocks;
6978
std::list<T*> _freePool;
7079
//std::set<T*> _usedPool;
7180
};

0 commit comments

Comments
 (0)