Skip to content

introduce simple resource_pool#123

Merged
LHT129 merged 1 commit intoantgroup:mainfrom
LHT129:visit_table
Jan 21, 2025
Merged

introduce simple resource_pool#123
LHT129 merged 1 commit intoantgroup:mainfrom
LHT129:visit_table

Conversation

@LHT129
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
}

std::shared_ptr<T>
GetOne() {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Return

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

use atomic instead

@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
@LHT129 LHT129 force-pushed the visit_table branch 2 times, most recently from 535630d to 9fd743c Compare January 17, 2025 06:59
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use Deque

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

@LHT129 LHT129 force-pushed the visit_table branch 2 times, most recently from 843ea97 to 1ea9076 Compare January 18, 2025 06:36
TakeOne() {
std::lock_guard<std::mutex> lock(mutex_);
if (pool_.empty()) {
return this->constructor_();
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

you are right, but it's hard to reconstruct all objects which have been token, So maybe delete this function, or use the function after all object have been returned?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

OK, Add check for all objects returned

@LHT129 LHT129 force-pushed the visit_table branch 2 times, most recently from 23e18ce to a273aaa Compare January 19, 2025 02:41
Copy link
Copy Markdown
Collaborator

@inabao inabao left a comment

Choose a reason for hiding this comment

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

LGTM

}

void
SetConstructor(ConstructFuncType func) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

OK, Add check for all objects returned

- 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 <tianlan.lht@antgroup.com>
@LHT129 LHT129 merged commit cefc6b2 into antgroup:main Jan 21, 2025
@LHT129 LHT129 deleted the visit_table branch January 21, 2025 03:20
Roxanne0321 pushed a commit to Roxanne0321/vsag that referenced this pull request Mar 3, 2025
- 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 <tianlan.lht@antgroup.com>
jiacai2050 pushed a commit to jiacai2050/vsag that referenced this pull request Mar 6, 2025
- 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 <tianlan.lht@antgroup.com>
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