c++ - How to avoid aliasing and improve performance? -


in stack overflow answer demonstrated aliasing in c++ can slow down code. , aliasing in c++ doesn't apply pointers, applies references, , more these types specified standard. particularly, there

an aggregate or union type includes 1 of aforementioned types among members (including, recursively, member of subaggregate or contained union)

so according understanding, if have code below,

 class a{   public:    int val;  };   void foo(vector<a> & array, int & size, & a0) {    for(int i=0;i<size;++i) {     array[i].val = 2*a0.val;    }  } 

and possible a0 can alias 1 of elements in array, possibly alias size because of aforementioned quote, a0 , size have loaded each iteration resulting in performance drop.

  1. then question should code avoid aliasing, , improve performance?
  2. passing const & won't since won't avoid aliasing specified standard. pass a0 value? make copying of a0 don't like, since in practice, class a may complex , copying expensive option.
  3. is there general solution avoid aliasing in c++? if yes, it?

the issue of avoiding aliasing performance issue in c++ seems covered evolution working group issue 72: n4150 alias-set attributes: toward restrict-like aliasing semantics c++, n3988 towards restrict-like aliasing semantics c++ n3635 towards restrict-like semantics c++ , n4150 latest version of proposal. ewg issue not yet resolved apparently considered ready review.

the proposal proposes c restrict qualifiers supported extensions in c++ many compilers have fuzzy areas, proposal says amongst other things:

there no question restrict qualifier benefits compiler optimization in many ways, notably allowing improved code motion , elimination of loads , stores. since introduction of c99 restrict, has been provided c++ extension in many compilers. feature brittle in c++ without clear rules c++ syntax , semantics. introduction of c++11, functors being replaced lambdas , users have begun asking how use restrict in presence of lambdas. given existing compiler support, need provide solution well-defined c++ semantics, before use of common subset of c99 restrict becomes used in combination c++11 constructs.

the proposal notes:

without standardizing , improving existing c99 restrict facility in c++, users typically have jump through considerable hoops effect through code rewriting through temporaries, or factoring , inlining function bodies simulate effect.

so seems there no solution, although current compilers have extensions offer c restrict semantics there lot of grey areas section 3. issues restrict in c++ , c covers of methods used avoid aliasing have flaws.

the proposal mentioned n3538 mentions of techniques , flaws associated these techniques. example:

the simplest technique overcoming aliasing copy potentially aliasing parameter.

void rf2(type& output, const type& input) {     type temp = input;     output += ra1(temp);     output += ra2(temp); } 

this technique both more complex , less efficient passing parameter value. while technique can useful when dealing legacy interfaces, should not primary technique.

this technique apply case.

note interesting take on aliasing , c99 restrict qualifier on redundancy of c99's restrict.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -