Skip to content

Commit a293a9f

Browse files
committed
Use string_view in address methods
When setting or getting the underlying string data used for an `Address` object, avoid extra copying by using `string_view`. During initialization a copy is performed into the `Address` object's local stroage, which makes sense. Though the input to the function need not copy to `string`. It can just be viewed while the data is copied into the object's local memory. Further when converting the `Address` to a string, a view can be used to avoid a copy during the return. If the caller would like to persist the string longer than the lifetime of the `Address` object, they can copy it at that point. Though we can save them a copy before then. Especially if they will use the string while the `Address` object is present.
1 parent 0d29ad8 commit a293a9f

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

cpp/include/ucxx/address.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#pragma once
66

77
#include <memory>
8-
#include <string>
8+
#include <string_view>
99

1010
#include <ucp/api/ucp.h>
1111

@@ -75,7 +75,7 @@ class Address : public Component {
7575
*
7676
* @returns The `shared_ptr<ucxx::Address>` object.
7777
*/
78-
friend std::shared_ptr<Address> createAddressFromString(std::string addressString);
78+
friend std::shared_ptr<Address> createAddressFromString(std::string_view addressString);
7979

8080
/**
8181
* @brief Get the underlying `ucp_address_t*` handle.
@@ -112,7 +112,7 @@ class Address : public Component {
112112
*
113113
* @returns The underlying `ucp_address_t` handle.
114114
*/
115-
[[nodiscard]] std::string getString() const;
115+
[[nodiscard]] std::string_view getString() const;
116116
};
117117

118118
} // namespace ucxx

cpp/src/address.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: BSD-3-Clause
44
*/
55
#include <memory>
6-
#include <string>
6+
#include <string_view>
77

88
#include <ucxx/address.h>
99
#include <ucxx/utils/ucx.h>
@@ -38,7 +38,7 @@ std::shared_ptr<Address> createAddressFromWorker(std::shared_ptr<Worker> worker)
3838
return std::shared_ptr<Address>(new Address(worker, address, length));
3939
}
4040

41-
std::shared_ptr<Address> createAddressFromString(std::string addressString)
41+
std::shared_ptr<Address> createAddressFromString(std::string_view addressString)
4242
{
4343
ucp_address_t* address = reinterpret_cast<ucp_address_t*>(new char[addressString.length()]);
4444
size_t length = addressString.length();
@@ -50,9 +50,9 @@ ucp_address_t* Address::getHandle() const { return _handle; }
5050

5151
size_t Address::getLength() const { return _length; }
5252

53-
std::string Address::getString() const
53+
std::string_view Address::getString() const
5454
{
55-
return std::string{reinterpret_cast<char*>(_handle), _length};
55+
return std::string_view{reinterpret_cast<const char*>(_handle), _length};
5656
}
5757

5858
} // namespace ucxx

0 commit comments

Comments
 (0)