Giter Site home page Giter Site logo

Comments (6)

bluss avatar bluss commented on August 29, 2024

Thanks, I see the same thing, also after adding some uses of test::black_box (didn't change bench results).

It mustn't be too surprising since the current Stride is unfortunately using two comparisons per iteration -- comparing begin to NULL and begin to end. A smart enough compiler should see that begin will never be NULL until it has first been equal to end.., but that doesn't happen.

I'm not sure about the best way to fix it. I'm using Stride from pointers, not from a slice or slice iterator, so I'd prefer an independent Stride iterator. Maybe element counting is simpler and faster.

from itertools.

bluss avatar bluss commented on August 29, 2024

Here is a diff that does in fact increase the performance of Stride, but it doesn't catch up fully.

diff --git a/src/stride.rs b/src/stride.rs
index dd4bd84..78afe39 100644
--- a/src/stride.rs
+++ b/src/stride.rs
@@ -195,19 +195,23 @@ macro_rules! stride_iterator {
             #[inline]
             fn next(&mut self) -> Option<$elem>
             {
-                if self.begin.is_null() {
-                    None
-                } else {
-                    unsafe {
-                        let elt: $elem = mem::transmute(self.begin);
-                        if self.begin == self.end {
+                if self.begin == self.end {
+                    if !self.begin.is_null() {
+                        unsafe {
+                            let elt: $elem = mem::transmute(self.begin);
                             self.begin = RawPtr::null();
-                        } else {
-                            self.begin = self.begin.offset(self.stride);
+                            self.end = RawPtr::null();
+                            return Some(elt);
                         }
-                        Some(elt)
+                    } else {
+                        return None;
                     }
                 }
+                unsafe {
+                    let elt: $elem = mem::transmute(self.begin);
+                    self.begin = self.begin.offset(self.stride);
+                    Some(elt)
+                }
             }

             fn size_hint(&self) -> (uint, Option<uint>)

from itertools.

awelkie avatar awelkie commented on August 29, 2024

If the Stride struct uses a RandomAccessIterator, then we can just make a struct that iterates over pointers and implements RandomAccessIterator, and have the Stride iterate over that struct. That way, the RandomAccessIterator-based Stride struct will be able to work on raw pointers (like the current Stride struct does), as well as other types of iterators that don't operate on contiguous memory (like Counter) that the current Stride can't handle. And, since it's not incurring a performance penalty, it seems like a complete win.

from itertools.

bluss avatar bluss commented on August 29, 2024

The method sounds reasonable, but I'm sceptical of the RandomAccessIterator (RAI) trait; it is experimental. It also is not compatible with &mut A iterator elements, because that would make it trivial to duplicate mut pointers.

from itertools.

bluss avatar bluss commented on August 29, 2024

I've updated now Stride to use integers, similar to your impl, and it performs better (probably because the .next() method itself is much smaller / simipler now).

A strider over RAI could be a separate addition.

from itertools.

bluss avatar bluss commented on August 29, 2024

Please file a PR if a RAI solution is created.

from itertools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.