-
Notifications
You must be signed in to change notification settings - Fork 23
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
introduce simple resource_pool #123
base: main
Are you sure you want to change the base?
Conversation
LHT129
commented
Nov 11, 2024
- for visited_list, give an implement of visited_list_pool
- we will have some other object like aio_context...
- currently hgraph use visitlist from hnswlib, now make it global
explicit VisitedList(InnerIdType max_size, Allocator* allocator) | ||
: max_size_(max_size), allocator_(allocator) { | ||
this->list_ = reinterpret_cast<VisitedListType*>( | ||
allocator_->Allocate((uint64_t)max_size * sizeof(VisitedListType))); |
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.
memory allocation may fail
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.
we have safe allocator
src/utils/resource_object_pool.h
Outdated
|
||
public: | ||
template <typename... Args> | ||
explicit ResourceObjectPool(uint64_t init_size, Args... args) { |
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.
Q: Do we need to set a maximum size for the pool to prevent creating too many resources in high concurrency scenarios?
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 are to strategy to manage the size, fixed or flexible, currently is flexible. maybe add fixed soon, if necessary
18ce304
to
b47a6c4
Compare
src/utils/resource_object_pool.h
Outdated
} | ||
|
||
std::shared_ptr<T> | ||
GetOne() { |
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.
Here is a Take / Return
semantics ?
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/utils/resource_object_pool.h
Outdated
} | ||
|
||
void | ||
ReleaseOne(std::shared_ptr<T>& obj) { |
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.
Return
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
} | ||
|
||
std::vector<std::shared_ptr<T>> pool_{}; | ||
size_t pool_size_{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.
pool_size_
is not necessary, can replace with pool_.size() ?
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.
sometimes may not equal
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.
Can you tell me more about it ?
for thread safe need use std::atomic<size_t>
d1fcb50
to
819012a
Compare
518e82c
to
80b1462
Compare
b11eea9
to
dbcc027
Compare
62110ce
to
308d8c1
Compare
Codecov ReportAttention: Patch coverage is @@ Coverage Diff @@
## main #123 +/- ##
==========================================
- Coverage 90.24% 90.21% -0.04%
==========================================
Files 118 121 +3
Lines 7372 7427 +55
==========================================
+ Hits 6653 6700 +47
- Misses 719 727 +8
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
|
535630d
to
9fd743c
Compare
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.
lgtm
src/utils/resource_object_pool.h
Outdated
} | ||
} | ||
|
||
std::vector<std::shared_ptr<T>> 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.
use Deque
- for visited_list, give an implement of visited_list_pool - we will have some other object like aio_context... - currently hgraph use visitlist from hnswlib, now make it global Signed-off-by: LHT129 <[email protected]>
TakeOne() { | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
if (pool_.empty()) { | ||
return this->constructor_(); |
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.
minor: here can release the lock mutex_
and then construct the 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.
good idea
} | ||
|
||
std::vector<std::shared_ptr<T>> pool_{}; | ||
size_t pool_size_{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.
Can you tell me more about it ?
for thread safe need use std::atomic<size_t>
} | ||
|
||
void | ||
SetConstructor(ConstructFuncType func) { |
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 there are objects that have already been taken, SetConstructor
will have a behavior problem