trsl logo

Accessors

Accessors are usually called at least once for each element of the accessed collection. Hence, they should be designed carefully in every situation where performance is an issue. For example, one can gain a lot by having the compiler inline accessors.

Accessor Types

This section describes different types of accessor. In the following, we assume elements to be Particles.

 class Particle
 {
 public:
   Particle(double weight, double x, double y) :
     x_(x), y_(y), weight_(weight) {}
   
   void setWeight(const double weight) { weight_ = weight; }
   double getWeight() const { return weight_; };
 
 private:
   double x_;
   double y_;
   double weight_;
 };

Functor

 struct functor_weight_accessor {
   double operator() (const Particle& p) const
   { return p.getWeight(); }
 };
Accessor Type
functor_weight_accessor
Accessor object, to pass to the object that needs access
functor_weight_accessor()

An example of functor accessor is trsl::weight_accessor, which always returns 1 — useful if elements have uniform weights.

Function Pointer

 double function_pointer_weight_accessor(const Particle& p)
 { return p.getWeight(); }
Accessor Type
std::pointer_to_unary_function<const Particle&, double>
Accessor object, to pass to the object that needs access
std::ptr_fun(function_pointer_weight_accessor)

Method Pointer

Accessor Type
std::const_mem_fun_ref_t<double, Particle>
Accessor object, to pass to the object that needs access
std::mem_fun_ref(&Particle::getWeight)

Method pointer is currently the default in trsl::is_picked_systematic (but subject to change, see Discussion). The implementation used in TRSL is not std::const_mem_fun_ref_t however, but trsl::mp_weight_accessor which allows a default construction without implying segfault when called — that comes with a price, see trsl::mp_weight_accessor.

Accessor Type
trsl::mp_weight_accessor<double, Particle>
Accessor object, to pass to the object that needs access
&Particle::getWeight (implicit conversion)

The pointer to getWeight implicitly constructs a trsl::mp_weight_accessor. (Why does the std counterpart have an explicit constructor?)

Discussion

I had the impression that GCC was able to inline fuctions from pointers. Performance tests on accessors (see tests/accessor_efficiency.cpp) tend to contradict this impression. With -03 optimization, the functor accessor is twice as fast as other accessors. Please comment if you know more about this.

© Copyright 2007-2011 Renaud Detry.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt.)
Revised Wed Apr 6 16:30:07 2011.
SourceForge.net Logo