Chris
Chris

Reputation: 1531

Speeding up boost::iterator_facade dereference

I am using iterator_facade to provide iterator support for a class. However, because the iterator_facade::dereference returns by reference, and it is an object with a non-trivial copy-const that is being returned, my performance is suffering upon every dereference (as profiled with VTune) as a result of constantly performing these copies. Is there a way around this?

class time_series::iterator : public boost::iterator_facade<
                         time_series::iterator,
                         time_series::variable_vec,
                         boost::bidirectional_traversal_tag,
                         timestep> //<--- dereference type
{
public:
    iterator();
    iterator(const iterator& src);
    ~iterator();
    iterator& operator=(const iterator& rhs);
private:
     //the following satisfies the reqs for a boost::facade bidirectional iterator
     friend class boost::iterator_core_access;
     friend class time_series;

     const timestep& dereference() const;
     bool equal(iterator const& other) const;
     void increment();
     void decrement();
     std::ptrdiff_t distance_to(iterator const& other) const;

     //iterators for the current step
     timestep _currentStep;// <--returned on dereference, is non-trivial to copy.
};

edit: as per comment

const timestep& time_series::iterator::dereference() const
{
    return _currentStep;
}

Upvotes: 1

Views: 1037

Answers (1)

Anton Savin
Anton Savin

Reputation: 41301

You specified timestep instead of timestep& as Reference template parameter, that's why it's constantly making copies. Change it to timestep& or remove at all.

Also for some reason you store _currentStep in the iterator by value, thus probably making copies every time you increment or decrement.

Read the Boost tutorial on iterator_facade for a reference how to implement facade properly.

Upvotes: 5

Related Questions