-
Notifications
You must be signed in to change notification settings - Fork 43
Use string_view in address methods
#392
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
base: branch-0.44
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,9 @@ | ||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * SPDX-FileCopyrightText: Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES. | ||||||||||||||||||||||||
| * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. | ||||||||||||||||||||||||
| * SPDX-License-Identifier: BSD-3-Clause | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| #include <memory> | ||||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||||
| #include <string_view> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #include <ucxx/address.h> | ||||||||||||||||||||||||
| #include <ucxx/utils/ucx.h> | ||||||||||||||||||||||||
|
|
@@ -38,21 +38,21 @@ std::shared_ptr<Address> createAddressFromWorker(std::shared_ptr<Worker> worker) | |||||||||||||||||||||||
| return std::shared_ptr<Address>(new Address(worker, address, length)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| std::shared_ptr<Address> createAddressFromString(std::string addressString) | ||||||||||||||||||||||||
| std::shared_ptr<Address> createAddressFromString(std::string_view addressString) | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| ucp_address_t* address = reinterpret_cast<ucp_address_t*>(new char[addressString.length()]); | ||||||||||||||||||||||||
| size_t length = addressString.length(); | ||||||||||||||||||||||||
| memcpy(address, addressString.c_str(), length); | ||||||||||||||||||||||||
| ucp_address_t* address = reinterpret_cast<ucp_address_t*>(new char[length]); | ||||||||||||||||||||||||
| memcpy(address, addressString.data(), length); | ||||||||||||||||||||||||
| return std::shared_ptr<Address>(new Address(nullptr, address, length)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ucp_address_t* Address::getHandle() const { return _handle; } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| size_t Address::getLength() const { return _length; } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| std::string Address::getString() const | ||||||||||||||||||||||||
| std::string_view Address::getString() const | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| return std::string{reinterpret_cast<char*>(_handle), _length}; | ||||||||||||||||||||||||
| return std::string_view{reinterpret_cast<const char*>(_handle), _length}; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
-53
to
56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAK, this is horrible because now I have to remember to tie the lifetime of the returned object to the I cannot believe this is worth it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good point, we can make it the user's responsibility to ensure the lifetime, this is already the case for the UCX handles. I don't have a strong objection either way, I'm a little more inclined towards Lawrence's point simply because that would avoid the potential to dereference an invalid pointer, whereas if attempting to use the address (that has become invalid because the worker is not available anymore) would simply result in an error from UCX/exception from UCXX. @wence- @jakirkham do you have strong opinions?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT there are only two uses of this method. Both involve the creation of a Cython
In the Cython
ucxx/python/ucxx/ucxx/_lib/libucxx.pyx Lines 395 to 405 in 4cd6354
In both cases we are doing a fair bit of intermediate object creation and copying. The goal of this PR was to cutdown on that Though would acknowledge that
That said, think it would be good if we could reduce the object creation and copying. Ideally deferring copying until the final object is constructed Side note: |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } // namespace ucxx | ||||||||||||||||||||||||
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.
This is fine.
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.
For additional context, this is used in one place in Cython where we already have something that could be
std::string_viewcompliant (a Pythonbytesobject). Currently in the construction we...bytesobject into astd::stringstd::stringto thecreateAddressFromStringfunction call (noconst std::string¤tly)std::stringto achar[]ucxx/python/ucxx/ucxx/_lib/libucxx.pyx
Lines 407 to 416 in 4cd6354
getStringcall at the end doing one more copy