Skip to content

Commit c17f4ef

Browse files
[SYCL][NFC] Avoid double allocation in device_impl::get_device_info_string (#18099)
Before the change: two allocations plus copy between them, after: one allocation plus zero-init of it so should be a strict improvement.
1 parent b3a4387 commit c17f4ef

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

sycl/source/detail/device_info.hpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,16 @@ device_impl::get_device_info_string(ur_device_info_t InfoCode) const {
208208
if (resultSize == 0) {
209209
return std::string();
210210
}
211-
std::unique_ptr<char[]> result(new char[resultSize]);
211+
std::string result;
212+
// C++23's `resize_and_overwrite` would be better...
213+
//
214+
// UR counts null terminator in the size, std::string doesn't. Adjust by "-1"
215+
// for that.
216+
result.resize(resultSize - 1);
212217
getAdapter()->call<UrApiKind::urDeviceGetInfo>(
213-
getHandleRef(), InfoCode, resultSize, result.get(), nullptr);
218+
getHandleRef(), InfoCode, resultSize, result.data(), nullptr);
214219

215-
return std::string(result.get());
220+
return result;
216221
}
217222

218223
// Specialization for string return type, variable return size

0 commit comments

Comments
 (0)