Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:16:22+00:00 2026-05-23T14:16:22+00:00

I’m currently generating code intended for a C++ compiler. I’m generating a class that

  • 0

I’m currently generating code intended for a C++ compiler. I’m generating a class that will take a visitor object, call the accept method on it with several objects. However, I want the visitor to have the ability to ‘break’, that is, the visitor should have a way of indicating that it wants to stop the rest of the accept calls because its lost interest.

I see two ways to accomplish this, and assuming the compiler has full visibility of the dispatching class methods and the visitor’s methods (so inlining is possible), I’m curious which way is easier for the compiler to optimize. Obviously, different compilers will produce different results, but I’d like my code generator to produce code that requires the least amount of sophistication from the compiler to produce fast code.

The first way is to generate a doVisiting method that expects the Accept method on the visitor indicating whether it 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 this way is that if any the accept methods are hardcoded to return false or true, I expect the compiler’s constant propogation would kick in would prune the if checks and accept calls. I think this is a reasonable assumption because constant propogation is something pretty much any optimizing compiler has to implement.

The second way would be not pay attention to the return type, and count on the visitor to maintain a bool internally indicating whether it wants to accept the rest of the iterations. At the top of its accept method it would check the bool to decide whether to do any processing:

struct myvisitor
{
    bool stop;
    void accept(object_type1& o)
    {
        if(stop)
            return;

        // do work
    }

    void accept(object_type2& o)
    {
        if(stop)
            return;

        // do work

        if(some_break_worthy_condition)
            // if we were returning bool instead of void,
            // we would have a return false here.
            stop = true;
    }

    // .. other accept methods
};

In the first method the ifs are outside the accept calls, and the second way the ifs are inside them. The second also necessarily involves storing a bit of state. My intuition is that the latter requires more sophisticated analysis, but maybe I underestimate compilers.

It also goes without saying I’m interested in other suggestions compilers would handle even better 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:16:23+00:00Added an answer on May 23, 2026 at 2:16 pm

    On the first option your unoptimized simplified code will look something like this:

    call visitor.accept o1
    branch on result is false to end
    call visitor.accept o2
    branch on result is false to end
    call visitor.accept o3
    branch on result is false to end
    call visitor.accept o4
    

    So how can the compiler optimize that? Well, I have now idea. Seems pretty efficient already. So you need to dig deeper.

    How difficulty will be the “call vistor.accept” part. Say that visitor is not a pointer, then the target address of the call instruction is known to the compiler at compile time. And it can be made a simple call instruction. It will just need to place the argument oX somewhere where the called function can find it. The compiler COULD further optimize this by actually putting the code from your myvisitor-struct inline into the code above. Than no call statement would be needed at all, but it would increase the file size, increasing the risk that your code does not fit in your CPU cache anymore etc. so there is certainly some discussion out there whether this is a suitable thing for a compiler to do, especially since calls are very fast on modern CPUs.

    What does your other unoptimized but simplified code look like?

    call visitor.accept o1
    call visitor.accept o2
    ...
    

    in every call to visitor the following will additionally happen:

    branch on stop is true to end
    [Some do-Stuff-code]
    

    So in this code the visitor’s accept method is called 4 times and the branch statement is called 4 times as well in any case. In the first method that you outlined, it may be possible that the first branch takes effect and you will be good with many fewer instructions, while you will never have a chance to gain an advantage.

    So what can the compiler do for you in the second example? You might expect the compiler to figure out that if the stop variable is set to true, than it can jump over all remaining call statements. But, that seriously needs a sophisticated compiler. Even if the compiler can figure it out, he may still not do anything, because in a multi-threaded environment you have no idea if the flow of instructions will surely be free from side effects.

    Thus I believe that the first example will be faster with compiler optimization or without, but disclaimer: I am not a compiler optimization professional. Maybe someone else will come up with something extremely smart :-).

    If your are further interested on what compilers can do for you, you may be interested in the talk Know your compiler by Felix von Leitner 2007.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.