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


##########
rust/tvm-ffi/src/collections/map.rs:
##########
@@ -0,0 +1,469 @@
+/*
+ * 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.
+ */
+//! Immutable [`Map`] container backed by the C++ `ffi.Map` object.
+//!
+//! The `MapObj` header mirrors C++ `MapBaseObj`, so [`Map::len`] reads `size`
+//! directly (no FFI). The hash-table storage itself is an implementation 
detail,
+//! so lookups and iteration are delegated to the global functions the C++ 
runtime
+//! registers (`ffi.MapGetItem`, `ffi.MapForwardIterFunctor`, ...): there is no
+//! Map-specific C ABI, and a Rust re-implementation would have to replicate 
the
+//! hashing (`AnyHash`/`AnyEqual`) and dense/small probing, just as the Python
+//! bindings also delegate. Element types are erased to `Any`, so `K`/`V` are
+//! validated lazily on access (see [`Map::get`] / [`Map::iter`]) rather than 
by
+//! [`AnyCompatible::check_any_strict`].
+use std::fmt::Debug;
+use std::marker::PhantomData;
+use std::ops::Deref;
+
+use crate::any::TryFromTemp;
+use crate::derive::Object;
+use crate::function::Function;
+use crate::object::{Object, ObjectArc};
+use crate::{Any, AnyCompatible, AnyView, Error, ObjectRefCore, Result};
+use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
+use tvm_ffi_sys::{TVMFFIAny, TVMFFIObject};
+
+/// Container object for [`Map`]. The header fields mirror C++ `MapBaseObj`
+/// (`include/tvm/ffi/container/map_base.h`) so [`Map::len`] can read `size`
+/// without an FFI call; the storage `data` points to stays opaque.
+///
+/// This is a partial mirror by design: `MapBaseObj` has one more trailing 
field
+/// after `slots_` (a `data_deleter_` function pointer), omitted here because 
this
+/// binding is read-only — it only reads fields up to `size`, which precede it,
+/// and never allocates a `MapObj` itself (the C++ runtime allocates the larger
+/// `Small`/`DenseMapBaseObj` subclass), so the trailing field is never 
touched.
+/// The layout also assumes `TVM_FFI_DEBUG_WITH_ABI_CHANGE` is off (the 
default):
+/// with that debug flag set, `MapBaseObj` gains a *leading* `state_marker` 
field
+/// that would shift every offset below.
+#[repr(C)]
+#[derive(Object)]
+#[type_key = "ffi.Map"]
+#[type_index(TypeIndex::kTVMFFIMap)]
+pub struct MapObj {
+    pub object: Object,
+    /// Pointer to the (opaque) key/value storage region (`MapBaseObj::data_`).
+    pub data: *mut core::ffi::c_void,
+    /// Number of entries (`MapBaseObj::size_`).
+    pub size: u64,
+    /// Number of hash slots; the MSB is a small-map tag 
(`MapBaseObj::slots_`).
+    pub slots: u64,
+}
+
+/// Immutable, reference-counted map from `K` to `V`, sharing its underlying
+/// `MapObj` with C++. Cloning is cheap (it bumps the refcount).
+#[repr(C)]
+pub struct Map<K, V> {
+    data: ObjectArc<MapObj>,
+    _marker: PhantomData<(K, V)>,
+}

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Since `MapObj` contains a raw pointer (`*mut core::ffi::c_void`), it is 
automatically `!Send` and `!Sync` by default. Consequently, `Map<K, V>` also 
becomes `!Send` and `!Sync`, which prevents it from being sent or shared across 
thread boundaries (e.g., when using `std::thread::spawn` or parallel iterators).
   
   Since `Map` is an immutable, reference-counted container, it is safe to 
implement `Send` and `Sync` for it, provided that the key and value types also 
satisfy these bounds.
   
   Consider adding explicit `Send` and `Sync` implementations for `Map<K, V>`.
   
   ```suggestion
   pub struct Map<K, V> {
       data: ObjectArc<MapObj>,
       _marker: PhantomData<(K, V)>,
   }
   
   unsafe impl<K: Send + Sync, V: Send + Sync> Send for Map<K, V> {}
   unsafe impl<K: Send + Sync, V: Send + Sync> Sync for Map<K, V> {}
   ```



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