cijiugechu commented on PR #2840:
URL: https://github.com/apache/iggy/pull/2840#issuecomment-3981582502

   > Hi, thanks for the contribution.
   > 
   > I am pretty sure a simpler solution to that problem would be to provide 
the `Iterator` with a size hint, this should give compiler enough information 
to generate code identical to yours. You can try doing that instead, here is 
example of that optimization being applied for an `Vec<T>`: 
https://godbolt.org/z/4erhYsxsG
   > 
   > The clue is those two lines
   > 
   > ```assembly
   >   mov rdi, qword ptr [rbx + 8]
   >   mov rbx, qword ptr [rdi + 8*rax - 8] ;Load the ptr + (len - 1)
   > ```
   
   The `last` optimization for `Vec` here probably isn’t achieved via any 
`size_hint` magic. In std, `slice`’s `Iterator::last` is implemented by calling 
[`DoubleEndedIterator::next_back`](https://github.com/rust-lang/rust/blob/80381278a08582356c13b0f52af92d27c567c230/library/core/src/slice/iter/macros.rs#L255),
 and `next_back` reaches O(1) access via the [unsafe `pre_dec_end` 
helper](https://github.com/rust-lang/rust/blob/80381278a08582356c13b0f52af92d27c567c230/library/core/src/slice/iter/macros.rs#L119-L137).
 By contrast, our `IggyMessageViewIterator` only implements 
[`Iterator::next`](https://github.com/apache/iggy/blob/effbab6f6a01386b65c115d9aa0d877c28b5dd83/core/common/src/types/message/message_view.rs#L174-L187),
 so `last` falls back to the [default `fold`-style 
loop](https://github.com/rust-lang/rust/blob/80381278a08582356c13b0f52af92d27c567c230/library/core/src/iter/traits/iterator.rs#L267),
 which prevents this optimization.
   


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

Reply via email to