Skip to content

[FEA]: Understand if fancy iterator unpacking is a sound default #11916

Open

Description

@gevtushenko

Is this a duplicate?

Area

Thrust

Is your feature request related to a problem? Please describe.

It's not clear if we can unpack iterators and load efficiently in all cases. For instance, in some algorithms this will lead to loading from one iterator twice. Is this sound?

Describe the solution you'd like

Answering if unpacking is performant or not is outside of the scope for this issue.

This issue can be closed with comment on the issue with a list of iterators for which we can do unpacking automatically without breaking functionality (correctness) and a list of iterators for which we need user to proclaim it's safe to do.

Describe alternatives you've considered

No response

Additional context

No response

Activity

  1. miscco commented on Oct 9, 2026

    @miscco
    Contributor

    I have been looking into this and I believe that it can be implemented sound but not without nontrivial code changes.

    Using a trait that detects whether a type is unwrappable is relatively straightforward to implement in a recursive fashion so that we can easily unwrap nested iterators.

    Importantly we can leverage that recursive exploration to also extract the base iterator through the base() const& member method that all our iterators as well as the C++ standard iterators have.

    Because we only ever unwrap if the ultimate base iterator is a pointer unwrapping by const& is valid as it ultimately only copies a pointer which is free and has no side effects.

    The interesting part is how to "rebase" a pointer onto a new base. This is slightly more complicated and presents some issues that we need to consider.

    1. We cannot simply copy the iterator with the new pointer because the iterator might be nested
    2. We must assure that we do not invalidate other parts of the iterator, e.g copying a an iterator should not be necessary

    For now I went with adding a private __rebase(T*) & member function on the fancy iterators. This allows us to recursively call that member until we arrive at a base iterator and simply assign the pointer.

    It also allows us to mutate the iterator in place without changing any other part of the iterator. At the same time we can simply just copy + rebase if the algorithm requires that so that is still valid.

    The catch are transformations that rely on argument addresses. Those are rare, but we need to consider those

    For now this is intended as an opt-in so we can control which iterators we unwrap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

  • Status
    Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions