(tvm-ffi) branch main updated: [FFI][PERF] Strip cv in IsObjectInstance so as() folds to true (#760)

Mon, 07 Sep 2026 14:22:18 -0700

This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git


The following commit(s) were added to refs/heads/main by this push:
     new 286c65bb [FFI][PERF] Strip cv in IsObjectInstance so as<Object>() 
folds to true (#760)
286c65bb is described below

commit 286c65bbde9e62d7d1d94a5289a3b7a8d0176fee
Author: Tianqi Chen <[email protected]>
AuthorDate: Mon Sep 7 17:13:07 2026 -0400

    [FFI][PERF] Strip cv in IsObjectInstance so as<Object>() folds to true 
(#760)
    
    `AnyView::as<Object>()` is `as<const T*>()`, so it instantiates
    `IsObjectInstance<const Object>` and the `std::is_same_v<TargetType,
    Object>` short-circuit misses on the `const`. Every derived object then
    falls through to a `TVMFFIGetTypeInfo` call and an ancestor walk to
    answer a question that is always yes.
    
    One line: strip cv on the check. Zero runtime cost; `as<Object>()` on a
    derived type now compiles to a plain pointer read with no call.
---
 include/tvm/ffi/object.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/tvm/ffi/object.h b/include/tvm/ffi/object.h
index 8c442ab1..5ca3c043 100644
--- a/include/tvm/ffi/object.h
+++ b/include/tvm/ffi/object.h
@@ -1202,7 +1202,7 @@ template <typename TargetType>
 TVM_FFI_INLINE bool IsObjectInstance(int32_t object_type_index) {
   static_assert(std::is_base_of_v<Object, TargetType>);
   // Everything is a subclass of object.
-  if constexpr (std::is_same_v<TargetType, Object>) {
+  if constexpr (std::is_same_v<std::remove_cv_t<TargetType>, Object>) {
     return true;
   } else if constexpr (TargetType::_type_final) {
     // if the target type is a final type

Reply via email to