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?
GitHub
[Feature Request] [stdlib] [proposal] Have all `fn getitem(self...
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 ...
5 Replies
Yes it allocates the way I have it written: https://github.com/modularml/mojo/blob/61a97c6f995ed8a471d9613b908f8f5302283ac4/stdlib/src/collections/list.mojo#L755
I should be using a ref to the list I think, if I want to avoid a copy: https://github.com/modularml/mojo/blob/61a97c6f995ed8a471d9613b908f8f5302283ac4/stdlib/src/collections/list.mojo#L780
GitHub
mojo/stdlib/src/collections/list.mojo at 61a97c6f995ed8a471d9613b90...
The Mojo Programming Language. Contribute to modularml/mojo development by creating an account on GitHub.
Congrats @duck_tape, you just advanced to level 2!
I'm still wrong, that
__getitem__
for the Reference to a list is for the scalar access. There does not appear to be a way to get a slice of a list without allocating.Answer:
Create a Span from the list:
Span(items)
and then slice into that. But the function you pass to seems to also need to specify a Span as the argument otherwise it is maybe coercing back to a list and being slow. https://docs.modular.com/mojo/stdlib/utils/span/Span/#__init__Span | Modular Docs
A non owning view of contiguous data.
I’ve been running into blerps as well iterating, parsing, and slicing thru lists. How do the constructors work with lists?