2

By "runtime metaprogramming" I mean changing the actual code at runtime.

For example, take this code:

while (true) {
   if (flag) {
      //do stuff
   }
   //do other stuff
}

Let's say something happens so that flag will always be false/true, so there is no need to keep checking its value. It would be nice to just "get rid" of the if statement. Obviously there might just be better design in the code before execution to handle this, but this is just an example.

10
  • 1
    There is usually no need to optimize code on that level. For this specific issue, modern processors use branch prediction so that in most cases a check for a constant flag will be skipped by the processor directly. See Wikipedia on Branch Prediction Commented Dec 10, 2022 at 2:46
  • 2
    No, it doesn't. Commented Dec 10, 2022 at 2:49
  • "Let's say something happens so that flag will always be false/true" If that could happen, then who's to say that something else couldn't happen to change the state of flag? "It would be nice to just "get rid" of the if statement." Um, why? Commented Dec 10, 2022 at 2:50
  • 1
    If I were writing a GUI application, clicking on a button would emit a signal that would cause a handler to become activated. I wouldn't have some while(true) { // Do stuff ... if(buttonClicked()) { // Do other stuff } } thing Commented Dec 10, 2022 at 3:02
  • 1
    The language doesn't support this directly, but it is possible to write code that does the sort of thing that you want; e.g. create an abstract class ITask with a virtual void doSomething() = 0; method, and create various subclasses that do various things, and finally create vector/list of these objects that the while(true)-loop iterates over, calling doSomething() on each in turn -- when you no longer want to do a task, you remove it from the vector/list and now the checks no longer occur for that task. Commented Dec 10, 2022 at 5:17

2 Answers 2

4

C++ does not have facilities for modifying code at runtime like that. This is because C++ code is compiled to machine code, and modifying machine code is very difficult, insecure, and non-portable. If you're interested, see the Wikipedia article on Self-modifying code.

In theory, if you really needed to, you could hoist the test for flag further back, by writing three sets of functions (one where flag is known to be always true, one where it's always false, and one where it could be either) and switch between those sets of functions at an earlier time. However, the code complexity and performance impact of maintaining three separate copies of the functions will not be worth the microscopic speedup from removing one easily-predicted branch.

If you're concerned about the performance of your application, you will find better opportunities for optimization elsewhere.

Sign up to request clarification or add additional context in comments.

Comments

1

Does C++ have any runtime metaprogramming functionality?

The abstract machine that a compiler has to have in mind when optimizing and organizing instructions does not involve the volatile nature of someone flipping a bit somewhere in memory that it can't control. That "somewhere" may not even exist after the compiler has done its job.

In the case of the true check in your loop:
None of the compilers I know will actually produce code that checks if there is a true there. It's just a an infinite loop.

Why? The compiler has to produce a program acting "as-if" it did all you instructed it to do. If you instruct it to check if true is true, the "as-if" rule tells the compiler that it can with a 100% certainty rely on the fact that you want this loop to go on forever. No runtime check necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.