gemini-code-assist[bot] commented on code in PR #18467:
URL: https://github.com/apache/tvm/pull/18467#discussion_r2539840551


##########
web/emcc/wasm_runtime.cc:
##########
@@ -125,24 +125,24 @@ TVM_FFI_STATIC_INIT_BLOCK() {
       });
 }
 
-void ArrayDecodeStorage(Tensor cpu_arr, std::string bytes, std::string format, 
std::string dtype) {
+void ArrayDecodeStorage(Tensor cpu_arr, TVMFFIByteArray* bytes, std::string 
format, std::string dtype) {

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   For better performance, you can pass `format` and `dtype` by `const` 
reference to avoid unnecessary string copies.
   
   ```suggestion
   void ArrayDecodeStorage(Tensor cpu_arr, TVMFFIByteArray* bytes, const 
std::string& format, const std::string& dtype) {
   ```



##########
web/emcc/wasm_runtime.cc:
##########
@@ -125,24 +125,24 @@ TVM_FFI_STATIC_INIT_BLOCK() {
       });
 }
 
-void ArrayDecodeStorage(Tensor cpu_arr, std::string bytes, std::string format, 
std::string dtype) {
+void ArrayDecodeStorage(Tensor cpu_arr, TVMFFIByteArray* bytes, std::string 
format, std::string dtype) {
+  ICHECK_NE(bytes, nullptr);
+  const char* byte_data = bytes->data;
+  const size_t byte_size = bytes->size;
   if (format == "f32-to-bf16" && dtype == "float32") {
-    std::vector<uint16_t> buffer(bytes.length() / 2);
-    std::memcpy(buffer.data(), bytes.data(), buffer.size() * 2);
-    // decode bf16 to f32
-    const uint16_t* bf16 = reinterpret_cast<const uint16_t*>(buffer.data());
+    const uint16_t* bf16 = reinterpret_cast<const uint16_t*>(byte_data);
     uint32_t* data = static_cast<uint32_t*>(cpu_arr->data);
     ICHECK(cpu_arr.IsContiguous());
     size_t size = 1;
     for (int i = 0; i < cpu_arr->ndim; ++i) {
       size *= cpu_arr->shape[i];
     }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   You can simplify the calculation of the total number of elements by using 
the `Shape().NumElements()` method, which is more concise and less error-prone.
   
   ```suggestion
       size_t size = cpu_arr.Shape().NumElements();
   ```



##########
web/emcc/wasm_runtime.cc:
##########
@@ -151,16 +151,10 @@ TVM_FFI_STATIC_INIT_BLOCK() {
   refl::GlobalDef().def_packed(
       "tvmjs.array.decode_storage", [](ffi::PackedArgs args, ffi::Any* ret) {
         Tensor cpu_arr = args[0].cast<Tensor>();
-        auto bytes = args[1].cast<ffi::Bytes>();
+        TVMFFIByteArray* bytes = args[1].cast<TVMFFIByteArray*>();
         std::string format = args[2].cast<ffi::String>().operator 
std::string();
         std::string dtype = args[3].cast<ffi::String>().operator std::string();

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The conversion from `ffi::String` to `std::string` can be simplified by 
using the implicit conversion operator, making the code more readable.
   
   ```suggestion
           std::string format = args[2];
           std::string dtype = args[3];
   ```



-- 
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]

Reply via email to