ModularM
Modular14mo ago
7 replies
duck_tape

Passing a Slice to a function

What is happening when I pass a slice of a list to a function?

With these examples (very contrived, but reflective of what I'm seeing in larger real code), passing a slice of a list to a function of signature borrowed items: List[UInt8] is waaaaayyyy slower than any other way. Is it allocating a new copy on a slice? Should I be looking into Span's instead? Or is this an area still being looked at https://github.com/modularml/mojo/issues/3653 ?

The signature of __getitem__ for
List
makes it look like it returns a
ref
to itself though, which seemingly wouldn't need to allocate?

fn count_items(borrowed items: List[UInt8]) -> Int:
    return len(items)


fn count_items_list(borrowed items: List[UInt8], offset: Int) -> Int:
    return len(items) - offset


fn count_items_tensor(borrowed items: Tensor[DType.uint8], offset: Int) -> Int:
    """Mock function that would work on the tensor based on the offset instead of a slice
    """
    return items.num_elements() - offset

fn main():
    var item_list = List[UInt8]()
    for i in range(10000):
        item_list.append(i)

    fn test_passing_slice() raises:
        var sum = 0
        for i in range(10000):
            sum += count_items(item_list[i:])

    fn test_passing_list() raises:
        var sum = 0
        for i in range(10000):
            sum += count_items_list(item_list, i)

    var item_tensor = Tensor(item_list)

    fn test_passing_tensor() raises:
        var sum = 0
        for i in range(10000):
            sum += count_items_tensor(item_tensor, i)
    # Custom benchmark code that needs to be changed to the stdlib benchmark code
GitHub
Review Mojo's priorities I have read the roadmap and priorities and I believe this request falls within the priorities. What is your request? Once we have Iterators designed or right now using ...
[Feature Request] [stdlib] [proposal] Have all `fn __getitem__(self...
Was this page helpful?