DEV Community

Seth T.T
Seth T.T

Posted on

Tech Story: C++ compiler behavior in a technical interview

Interviewer: Given this example C++ function which assigns a local vector a into another local vector b then return b, would the compiler optimize the copy (a -> b) into a move?

My Response

I can't tell you the answer, because I don't know. But I can tell you how I would figure it out resourcefully, and we can talk about the effectiveness of my approach.

Here, we're dealing with copies into returned local values, and asking whether the compiler would optimize the copy into a move.

Gut feelings

Automatically, my brain is going to pull memories from past programming experience and consumed media which may be relevant to the example. Any information relevant to answering the problem will be used to make a "gut prediction," which provides two outputs:

  • (A) an expected outcome (yes, the optimization happens)
  • (B) my confidence in the prediction (not confident at all xD)

After a couple of seconds, my brain will retro-fit an explanation onto those two data points, and that is when the "slow thinking" brain can analyze my prediction and make decisions. Here are the explanations my brain generated:

  • (A) Yes, the optimization happens because I've seen the C++ term "Return Value Optimization" thrown around various forums, and that seems relevant to the problem. I also don't remember seeing examples of using std::move in return statements, so I lack reasons to think the answer is actually "no."

  • (B) My understanding of C++ has a lot of "unknown unknowns" r.e. its complex language semantics and optimization rules. I know that otherwise-trivial structs and classes do become non-trivial if any of their instance variables are non-trivial. This makes struct triviality a "viral" data property that can butterfly-effect how the compiler might optimize a piece of code. I don't know if other "viral properties" exist which can also change compiler behavior, and without knowing that it becomes impossible to reason about how the compiler will behave. So, my brain throws its squishy hands in the air and says "impossible to predict."

Basically, I don't make any assumptions about C++ because I know better. So... how do I figure out the answer?

Use Resources

Step 1: Google. Always. Starting with what I know, I'd probably google "return value optimization" + "move semantics" as a query to find literature that relates those two topics. Stack Overflow is a moderated source-of-truth for popular questions, and might have a good answer if the question is popular enough to have received moderator attention.

But let's assume Stack Overflow is down for maintenance or my internet is disconnected and I can't Google the answer. How would I figure this out? Simple: compare the assembly.

Analyze Evidence

Step 2: I would compile the example program using zero optimization (-O0) and full optimization (-O3) and make the compiler emit assembly code (-S) instead of an executable. Comparing the assembly side-by-side, we can evaluate any code differences and make a prediction about whether the copy was optimized into a move. I say 'prediction' and not 'answer' in case the assembly is difficult to interpret or in case other optimizations are occluding the evidence we're looking for.

Learn the Rules

But this still only gives us the answer in this particular case. If we change the code, or look at an example of a real production codebase, the answer might not be the same, and unless we know exactly what rules the compiler follows to optimize copies, you'll always have to re-compile the code and evaluate the assembly to know for sure the optimization happened.

That's where my brain goes. Cheers!

Top comments (1)

Collapse
 
programmerraja profile image
Boopathi

Interesting approach to tackling interview questions! It's a good reminder to think about the compiler's optimization capabilities, especially when dealing with move semantics and return values. Analyzing assembly is a great way to get concrete evidence of how the compiler behaves.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.