c++ - Which technique for breaking out of a 'visitor' loop is easier for compilers to analyze? -
c++ - Which technique for breaking out of a 'visitor' loop is easier for compilers to analyze? -
i'm generating code intended c++ compiler. i'm generating class take visitor object, phone call take method on several objects. however, want visitor have ability 'break', is, visitor should have way of indicating wants stop rest of take calls because lost interest.
i see 2 ways accomplish this, , assuming compiler has total visibility of dispatching class methods , visitor's methods (so inlining possible), i'm curious way easier compiler optimize. obviously, different compilers produce different results, i'd code generator produce code that requires to the lowest degree amount of sophistication compiler produce fast code.
the first way generate dovisiting method expects take method on visitor indicating whether should continue:
template<class visitort> void dovisiting(visitort& visitor) { if(visitor.accept(object1)) { if(visitor.accept(object2)) { if(visitor.accept(object3)) { visitor.accept(object4); } } } } one advantage of way if take methods hardcoded homecoming false or true, expect compiler's constant propogation kick in prune if checks , take calls. think reasonable assumption because constant propogation pretty much optimizing compiler has implement.
the sec way not pay attending homecoming type, , count on visitor maintain bool internally indicating whether wants take rest of iterations. @ top of take method check bool decide whether processing:
struct myvisitor { bool stop; void accept(object_type1& o) { if(stop) return; // work } void accept(object_type2& o) { if(stop) return; // work if(some_break_worthy_condition) // if returning bool instead of void, // have homecoming false here. stop = true; } // .. other take methods }; in first method ifs outside take calls, , sec way ifs within them. sec involves storing bit of state. intuition latter requires more sophisticated analysis, maybe underestimate compilers.
it goes without saying i'm interested in other suggestions compilers handle improve ;)
on first alternative unoptimized simplified code this:
call visitor.accept o1 branch on result false end phone call visitor.accept o2 branch on result false end phone call visitor.accept o3 branch on result false end phone call visitor.accept o4 so how can compiler optimize that? well, have idea. seems pretty efficient already. need dig deeper.
how difficulty "call vistor.accept" part. visitor not pointer, target address of phone call instruction known compiler @ compile time. , can made simple phone call instruction. need place argument ox somewhere called function can find it. compiler farther optimize putting code myvisitor-struct inline code above. no phone call statement needed @ all, increment file size, increasing risk code not fit in cpu cache anymore etc. there give-and-take out there whether suitable thing compiler do, since calls fast on modern cpus.
what other unoptimized simplified code like?
call visitor.accept o1 phone call visitor.accept o2 ... in every phone call visitor next additionally happen:
branch on stop true end [some do-stuff-code] so in code visitor's take method called 4 times , branch statement called 4 times in case. in first method outlined, may possible first branch takes effect , many fewer instructions, while never have chance gain advantage.
so can compiler in sec example? might expect compiler figure out if stop variable set true, can jump on remaining phone call statements. but, needs sophisticated compiler. if compiler can figure out, may still not anything, because in multi-threaded environment have no thought if flow of instructions certainly free side effects.
thus believe first illustration faster compiler optimization or without, disclaimer: not compiler optimization professional. maybe else come extremely smart :-).
if farther interested on compilers can you, may interested in talk know compiler felix von leitner 2007.
c++ optimization compiler-construction code-generation visitor
Comments
Post a Comment