gemini-code-assist[bot] commented on code in PR #443:
URL: https://github.com/apache/tvm-ffi/pull/443#discussion_r2800377362
##########
python/tvm_ffi/container.py:
##########
@@ -209,6 +224,146 @@ def __radd__(self, other: Iterable[T]) -> Array[T]:
return type(self)(itertools.chain(other, self))
+@register_object("ffi.List")
+class List(core.Object, MutableSequence[T]):
+ """Mutable list container that represents a mutable sequence in the FFI."""
+
+ # tvm-ffi-stubgen(begin): object/ffi.List
+ # fmt: off
+ # fmt: on
+ # tvm-ffi-stubgen(end)
+
+ def __init__(self, input_list: Iterable[T] = ()) -> None:
+ """Construct a List from a Python sequence."""
+ self.__init_handle_by_constructor__(_ffi_api.List, *input_list)
+
+ @overload
+ def __getitem__(self, idx: SupportsIndex, /) -> T: ...
+
+ @overload
+ def __getitem__(self, idx: slice, /) -> list[T]: ...
+
+ def __getitem__(self, idx: SupportsIndex | slice, /) -> T | list[T]: #
ty: ignore[invalid-method-override]
+ """Return one element or a list for a slice."""
+ length = len(self)
+ return getitem_helper(self, _ffi_api.ListGetItem, length, idx)
+
+ @overload
+ def __setitem__(self, index: SupportsIndex, value: T) -> None: ...
+
+ @overload
+ def __setitem__(self, index: slice[int | None], value: Iterable[T]) ->
None: ...
+
+ def __setitem__(self, index: SupportsIndex | slice[int | None], value: T |
Iterable[T]) -> None:
+ """Set one element or assign a slice."""
+ if isinstance(index, slice):
+ replacement = list(cast(Iterable[T], value))
+ length = len(self)
+ start, stop, step = index.indices(length)
+ if step != 1:
+ target_indices = list(range(start, stop, step))
+ if len(replacement) != len(target_indices):
+ raise ValueError(
+ "attempt to assign sequence of size "
+ f"{len(replacement)} to extended slice of size
{len(target_indices)}"
+ )
+ for i, item in zip(target_indices, replacement):
+ _ffi_api.ListSetItem(self, i, item)
+ return
+ stop = max(stop, start)
+ _ffi_api.ListReplaceSlice(self, start, stop,
type(self)(replacement))
+ return
+
+ normalized_index = normalize_index(len(self), index)
+ _ffi_api.ListSetItem(self, normalized_index, cast(T, value))
+
+ @overload
+ def __delitem__(self, index: SupportsIndex) -> None: ...
+
+ @overload
+ def __delitem__(self, index: slice[int | None]) -> None: ...
+
+ def __delitem__(self, index: SupportsIndex | slice[int | None]) -> None:
+ """Delete one element or a slice."""
+ if isinstance(index, slice):
+ length = len(self)
+ start, stop, step = index.indices(length)
+ if step == 1:
+ stop = max(stop, start)
+ _ffi_api.ListEraseRange(self, start, stop)
+ else:
+ indices = (
+ reversed(range(start, stop, step)) if step > 0 else
range(start, stop, step)
+ )
+ for i in indices:
+ _ffi_api.ListErase(self, i)
+ return
+ normalized_index = normalize_index(len(self), index)
+ _ffi_api.ListErase(self, normalized_index)
+
+ def insert(self, index: int, value: T) -> None:
+ """Insert value before index."""
+ length = len(self)
+ if index < 0:
+ index = max(0, index + length)
+ else:
+ index = min(index, length)
+ _ffi_api.ListInsert(self, index, value)
+
+ def append(self, value: T) -> None:
+ """Append one value to the tail."""
+ _ffi_api.ListAppend(self, value)
+
+ def clear(self) -> None:
+ """Remove all elements from the list."""
+ _ffi_api.ListClear(self)
+
+ def pop(self, index: int = -1) -> T:
+ """Remove and return item at index (default last)."""
+ length = len(self)
+ if length == 0:
+ raise IndexError("pop from empty list")
+ normalized_index = normalize_index(length, index)
+ return cast(T, _ffi_api.ListPop(self, normalized_index))
+
+ def extend(self, values: Iterable[T]) -> None:
+ """Append elements from an iterable."""
+ for value in values:
+ _ffi_api.ListAppend(self, value)
Review Comment:

The current implementation of `extend` calls `ListAppend` in a loop. This
can be inefficient as it may lead to multiple reallocations of the underlying
buffer if the list needs to grow. A more efficient approach is to use slice
assignment, which will perform at most one reallocation for the entire
extension.
```suggestion
def extend(self, values: Iterable[T]) -> None:
"""Append elements from an iterable."""
self[len(self) : len(self)] = values
```
--
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]