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


##########
web/src/webgpu.ts:
##########
@@ -697,6 +697,13 @@ export class WebGPUContext {
       bindGroupLayouts: [bindGroupLayout]
     });
 
+    // Pre-allocate typed array views for pod args (reused across dispatches)
+    const maxPodArgs = podArgIndices.length + 1; // +1 for packGridDimX
+    const podArgsArrayBuffer = new ArrayBuffer(maxPodArgs * 4);
+    const i32ViewCached = new Int32Array(podArgsArrayBuffer);
+    const u32ViewCached = new Uint32Array(podArgsArrayBuffer);
+    const f32ViewCached = new Float32Array(podArgsArrayBuffer);

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   To further reduce per-dispatch overhead and avoid magic numbers, consider 
hoisting the total byte size calculation and using 
`Int32Array.BYTES_PER_ELEMENT`. This constant can then be reused in 
`submitShader` for the uniform pool request and the bind group entry size. 
Also, updated the comment to match the variable name `packDimX` used in the 
implementation.
   
   ```suggestion
       const maxPodArgs = podArgIndices.length + 1; // +1 for packDimX
       const podArgBytes = maxPodArgs * Int32Array.BYTES_PER_ELEMENT;
       const podArgsArrayBuffer = new ArrayBuffer(podArgBytes);
       const i32ViewCached = new Int32Array(podArgsArrayBuffer);
       const u32ViewCached = new Uint32Array(podArgsArrayBuffer);
       const f32ViewCached = new Float32Array(podArgsArrayBuffer);
   ```



##########
web/src/webgpu.ts:
##########
@@ -759,32 +766,28 @@ export class WebGPUContext {
         const sizeOfI32 = 4;
         const bufBytes = (podArgIndices.length + 1) * sizeOfI32;
         const podArgBuffer = this.getUniformFromPool(bufBytes);
-        const i32View = new Int32Array(podArgIndices.length + 1);
-        const u32View = new Uint32Array(i32View.buffer);
-        const f32View = new Float32Array(i32View.buffer);
 
         for (let i = 0; i < podArgIndices.length; ++i) {
           const value = args[podArgIndices[i]];
           const dtype = finfo.arg_types[podArgIndices[i]];
           if (dtype.startsWith("int")) {
-            i32View[i] = value;
+            i32ViewCached[i] = value;
           } else if (dtype.startsWith("uint")) {
-            u32View[i] = value;
+            u32ViewCached[i] = value;
           } else if (dtype.startsWith("float")) {
-            f32View[i] = value;
+            f32ViewCached[i] = value;

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The `dtype.startsWith` string operations are executed for every POD argument 
on every dispatch. Since the argument types are fixed for each shader, consider 
pre-calculating an array of type indicators (e.g., an enum or numeric 
constants) in the `createShadeInternal` scope. This would allow replacing the 
string operations with a faster numeric check in the `submitShader` loop, which 
is beneficial for workloads with many small dispatches.



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