gemini-code-assist[bot] commented on code in PR #18632:
URL: https://github.com/apache/tvm/pull/18632#discussion_r2658730704
##########
src/relax/analysis/udchain.cc:
##########
@@ -121,10 +121,29 @@ ffi::Map<Var, ffi::Array<Var>> DataflowBlockUseDef(const
DataflowBlock& dfb) {
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
- refl::GlobalDef().def("relax.analysis.udchain", DataflowBlockUseDef);
+ refl::GlobalDef()
+ .def("relax.analysis.udchain", DataflowBlockUseDef)
+ .def("relax.analysis.get_used_vars", [](const Expr& expr) {
+ auto used_vars = GetUsedVars(expr);
+ ffi::Array<Var> result;
+ for (const VarNode* var_node : used_vars) {
+ result.push_back(ffi::GetRef<Var>(var_node));
+ }
+ return result;
Review Comment:

For improved performance, especially when `used_vars` is large, consider
building a `std::vector` with pre-reserved capacity and then constructing the
`ffi::Array` from it. This avoids potential reallocations that can occur with
repeated `push_back` calls on `ffi::Array`.
```c
std::vector<Var> result_vec;
result_vec.reserve(used_vars.size());
for (const VarNode* var_node : used_vars) {
result_vec.push_back(ffi::GetRef<Var>(var_node));
}
return ffi::Array<Var>(std::move(result_vec));
```
--
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]