tqchen commented on code in PR #438:
URL: https://github.com/apache/tvm-ffi/pull/438#discussion_r2806747880


##########
src/ffi/extra/deep_copy.cc:
##########
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * \file src/ffi/extra/deep_copy.cc
+ *
+ * \brief Reflection-based deep copy utilities.
+ */
+#include <tvm/ffi/any.h>
+#include <tvm/ffi/container/array.h>
+#include <tvm/ffi/container/list.h>
+#include <tvm/ffi/container/map.h>
+#include <tvm/ffi/container/shape.h>
+#include <tvm/ffi/error.h>
+#include <tvm/ffi/extra/deep_copy.h>
+#include <tvm/ffi/reflection/accessor.h>
+#include <tvm/ffi/reflection/registry.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace ffi {
+
+/*!
+ * \brief Deep copier with memoization.
+ *
+ * - Arrays / Maps: Resolve() recurses to rebuild with resolved children.
+ * - Copyable objects: shallow-copied immediately into copy_map_ (so cyclic
+ *   back-references resolve), then queued for field resolution.
+ * - The queue is drained iteratively by Run(), bounding recursion depth
+ *   to container nesting rather than object-graph depth.
+ * - Shared references are preserved: the same original maps to the same copy.
+ */
+class ObjectDeepCopier {
+ public:
+  explicit ObjectDeepCopier(reflection::TypeAttrColumn* column) : 
column_(column) {}
+
+  Any Run(const Any& value) {
+    if (value.type_index() < TypeIndex::kTVMFFIStaticObjectBegin) return value;
+    Any result = Resolve(value);
+    // NOLINTNEXTLINE(modernize-loop-convert): queue grows during iteration
+    for (size_t i = 0; i < resolve_queue_.size(); ++i) {
+      ResolveFields(resolve_queue_[i]);
+    }
+    return result;
+  }
+
+ private:
+  /*! \brief Resolve a value: pass through primitives, copy/rebuild objects. */
+  Any Resolve(const Any& value) {
+    if (value.type_index() < TypeIndex::kTVMFFIStaticObjectBegin) {
+      return value;
+    }
+    const Object* obj = value.as<Object>();
+    if (auto it = copy_map_.find(obj); it != copy_map_.end()) {
+      return it->second;
+    }
+    int32_t ti = obj->type_index();
+    // Strings and bytes are immutable — return as-is.
+    if (ti == TypeIndex::kTVMFFIStr || ti == TypeIndex::kTVMFFIBytes) {

Review Comment:
   this also applies to kTVMFFIShape which is immutable and can return as it is



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