-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add command object pool #260
Conversation
src/cmd_object_pool.cc
Outdated
} | ||
|
||
thread_local std::unique_ptr<std::vector<SmallObject>> CmdObjectPool::local_pool = nullptr; | ||
thread_local uint64_t CmdObjectPool::counter = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
放到文件开头定义? 然后就是这变量要不要加一个g_this_之类的前缀,看的有点迷糊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个 tl_
前缀怎么样
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以
// if you don t find it go to the global search | ||
return GetObjectByGlobal(key); | ||
} | ||
void CmdObjectPool::PutObject(std::string &&key, std::unique_ptr<BaseCmd> &&v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个空行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/cmd_object_pool.cc
Outdated
++counter; | ||
if (counter == 0) { | ||
// If the version number reaches 0, reset all version numbers in localPool | ||
for (auto &obj : *local_pool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前的代码逻辑似乎counter不会为0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
counter
是 uint64
类型, 当 counter
累加到最大值后,再+1, 这时候会溢出, 然后成0, 当然这种情况很难出现,或者说几乎不会出现
|
||
// If you can't find it, use the func function to create a corresponding object | ||
auto func = create_object_.find(key); | ||
if (func == create_object_.end()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是否可以减小锁mutex_的粒度,目前来看create_object_不需要锁保护
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对的, 可以减小
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/cmd_object_pool.cc
Outdated
|
||
// std::cout << "smallPool.size:" << local_pool->size() << " smallPool.cap:" << local_pool->capacity() << | ||
// std::endl; | ||
if (local_pool->size() <= local_max_) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是否考虑如果使用次数少于多少也把它移动到全局的命令对象池中?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样做的目的是什么呢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑的是使用次数少到一定程度的命令对象放到全局对象池中,可以供其他的线程使用,减少创建的次数,另一方面就是可以减少这个线程对应的命令对象占用内存大小
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个加了, 现在会在每3次(随便定的)回收的时候, 判断一下, 如果有使用次数是1的命令, 也会进行回收
bool needPush = true; | ||
for (auto &obj : *local_pool) { | ||
if (obj.key_ == key && !obj.object_) { | ||
// if the keys are the same and the pointer is empty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这种情况会在什么前提下发生,按照道理来说不应该是成对出现,为啥会出现命令对象为null的情况
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在的使用场景这部分可以去掉了, 之前想的是在 IO线程中获取命令对象, 然后把命令传到 线程池中使用, 这时候可能会出现多个同名命令的情况,比如 第一个命令是 GET
, 获取了一个 GetCmd
对象, 然后这个命令还没有执行完成, 又来了一个 GET
命令, 这时候就需要再从全局里获取一个 GetCmd
对象, 此时 local_pool
中就有两个 GetCmd
对象了,
现在把命令对象获取和释放, 都放在线程池中了, 可以避免这个问题, 就是还没想好, 这个地方该怎么处理, 要不要放在 IO线程里
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的
@CodiumAI-Agent /improve |
PR Code Suggestions ✨
|
@CodiumAI-Agent /review |
PR Review 🔍
Code feedback:
|
使用对象池来解决命令对象复用, 现在只是一个实现方案的思路验证,还请帮忙多检查
对象复用池在
cmd_object_pool
文件, 这个会代替cmd_table_manager
, 后续相关的文件会删除.