Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

LHT129
Copy link
Collaborator

@LHT129 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

@LHT129 LHT129 self-assigned this Nov 11, 2024
@LHT129 LHT129 added kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) and removed size/L labels Nov 11, 2024
@LHT129 LHT129 marked this pull request as ready for review November 12, 2024 08:23
@LHT129 LHT129 requested a review from jiaweizone as a code owner November 12, 2024 08:23
@LHT129 LHT129 changed the title [WIP]introduce simple resource_pool introduce simple resource_pool Nov 13, 2024
@LHT129 LHT129 mentioned this pull request Nov 13, 2024
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)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memory allocation may fail

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have safe allocator


public:
template <typename... Args>
explicit ResourceObjectPool(uint64_t init_size, Args... args) {
Copy link
Collaborator

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?

Copy link
Collaborator Author

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

@LHT129 LHT129 force-pushed the visit_table branch 3 times, most recently from 18ce304 to b47a6c4 Compare November 20, 2024 07:23
src/utils/resource_object_pool.h Show resolved Hide resolved
}

std::shared_ptr<T>
GetOne() {
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

void
ReleaseOne(std::shared_ptr<T>& obj) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return

Copy link
Collaborator Author

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};
Copy link
Collaborator

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() ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes may not equal

Copy link
Collaborator

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>

src/utils/visited_list.h Show resolved Hide resolved
@LHT129 LHT129 force-pushed the visit_table branch 3 times, most recently from d1fcb50 to 819012a Compare November 27, 2024 09:07
@LHT129 LHT129 force-pushed the visit_table branch 4 times, most recently from 518e82c to 80b1462 Compare December 5, 2024 05:02
@LHT129 LHT129 force-pushed the visit_table branch 3 times, most recently from b11eea9 to dbcc027 Compare December 9, 2024 07:30
Copy link

codecov bot commented Jan 15, 2025

Codecov Report

Attention: Patch coverage is 92.72727% with 4 lines in your changes missing coverage. Please review.

@@            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     
Flag Coverage Δ
cpp 90.21% <92.72%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
common 86.55% <ø> (ø)
datacell 90.10% <ø> (ø)
index 90.60% <ø> (-0.12%) ⬇️
simd 100.00% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0f368e7...1ea9076. Read the comment docs.

src/utils/resource_object.h Outdated Show resolved Hide resolved
src/utils/resource_object.h Show resolved Hide resolved
src/utils/resource_object_pool.h Outdated Show resolved Hide resolved
src/utils/resource_object_pool.h Outdated Show resolved Hide resolved
src/utils/resource_object_pool.h Outdated Show resolved Hide resolved
src/utils/resource_object_pool.h Outdated Show resolved Hide resolved
src/utils/resource_object_pool.h Outdated Show resolved Hide resolved
src/utils/visited_list.h Outdated Show resolved Hide resolved
src/utils/visited_list_test.cpp Outdated Show resolved Hide resolved
@LHT129 LHT129 force-pushed the visit_table branch 2 times, most recently from 535630d to 9fd743c Compare January 17, 2025 06:59
Copy link
Collaborator

@wxyucs wxyucs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

}
}

std::vector<std::shared_ptr<T>> pool_{};
Copy link
Collaborator

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_();
Copy link
Collaborator

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

Copy link
Collaborator Author

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};
Copy link
Collaborator

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) {
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) size/L version/0.13
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants