gemini-code-assist[bot] commented on code in PR #305:
URL: https://github.com/apache/tvm-ffi/pull/305#discussion_r2582907979
##########
include/tvm/ffi/function.h:
##########
@@ -983,13 +986,16 @@ inline int32_t TypeKeyToIndex(std::string_view type_key) {
* This symbol is only exported when TVM_FFI_DLL_EXPORT_INCLUDE_METADATA
is defined.
*/
#if TVM_FFI_DLL_EXPORT_INCLUDE_METADATA
+// Implementation note: we specifically use TVMFFIStringFromByteArray
+// so the returned string metadata is allocated in the libtvm_ffi and long
lived.
#define TVM_FFI_DLL_EXPORT_TYPED_FUNC_DOC(ExportName, DocString)
\
extern "C" {
\
TVM_FFI_DLL_EXPORT int __tvm_ffi__doc_##ExportName(void* self, const
TVMFFIAny* args, \
int32_t num_args,
TVMFFIAny* result) { \
TVM_FFI_SAFE_CALL_BEGIN();
\
- ::tvm::ffi::String str(DocString);
\
- ::tvm::ffi::TypeTraits<::tvm::ffi::String>::MoveToAny(std::move(str),
result); \
+ std::string data = DocString;
\
+ TVMFFIByteArray data_array{data.data(), data.size()};
\
+ return TVMFFIStringFromByteArray(&data_array, result);
\
Review Comment:

For efficiency, we can avoid creating an intermediate `std::string` here,
which involves a memory allocation and copy. Since `DocString` is a C-style
string literal and `std::string_view` is available in this file, we can use it
to create a zero-cost view of the string and pass that to `TVMFFIByteArray`.
```c
std::string_view data(DocString);\n TVMFFIByteArray
data_array{data.data(), data.size()};\n return
TVMFFIStringFromByteArray(&data_array, result);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]