CPP Performance Optimization & Profiling 3 — Questions and Answers
Question 1: What does the `-O2` flag passed to g++ primarily enable?
- Debug symbol generation
- A standard set of optimizations that do not involve speed-space tradeoffs (Correct answer)
- Link-time optimization
- Profile-guided optimization
Correct answer: A standard set of optimizations that do not involve speed-space tradeoffs
-O2 enables most speed optimizations that don't significantly increase code size, such as inlining, loop optimizations, and common subexpression elimination.
Question 2: Which technique compiles each translation unit with optimization metadata so the linker can optimize across module boundaries?
- Precompiled headers
- Link-Time Optimization (LTO) (Correct answer)
- Interprocedural analysis at compile time only
- Unity builds
Correct answer: Link-Time Optimization (LTO)
LTO (enabled with -flto in GCC/Clang) defers full optimization to link time, allowing inlining and analysis across object file boundaries.
Question 3: When using `std::vector`, which operation is guaranteed O(1) amortized but may trigger a reallocation?
- insert() at an arbitrary position
- push_back() (Correct answer)
- erase() at an arbitrary position
- operator[] access
Correct answer: push_back()
push_back() is amortized O(1) because reallocation doubles capacity, but occasionally triggers an O(n) copy/move of all elements.
Question 4: What is 'profile-guided optimization' (PGO)?
- Optimization based on static analysis of code paths
- A two-phase process: compile, run with representative input, recompile using profiling data (Correct answer)
- Runtime JIT compilation based on observed behavior
- Optimization hints embedded in source via attributes
Correct answer: A two-phase process: compile, run with representative input, recompile using profiling data
PGO instruments a first build, runs it to collect branch and frequency data, then uses that data in a second build to make better optimization decisions.
Question 5: Which of the following best describes 'instruction-level parallelism' (ILP)?
- Executing multiple threads simultaneously on multiple cores
- Executing multiple independent instructions simultaneously within a single core pipeline (Correct answer)
- Prefetching instructions before they are needed
- Vectorizing loops with SIMD instructions
Correct answer: Executing multiple independent instructions simultaneously within a single core pipeline
ILP refers to the CPU's ability to execute multiple independent instructions simultaneously using techniques like out-of-order execution and superscalar pipelines.
Question 6: What does the `__builtin_expect` intrinsic (GCC/Clang) do?
- Forces a specific branch to be taken at runtime
- Provides the compiler with a hint about the expected value of an expression for branch layout (Correct answer)
- Inserts a hardware prefetch instruction
- Disables branch misprediction penalties
Correct answer: Provides the compiler with a hint about the expected value of an expression for branch layout
__builtin_expect(expr, val) tells the compiler which value expr is most likely to have, influencing code layout to make the likely path fall-through.
Question 7: Which memory allocator strategy best reduces fragmentation in a system that repeatedly allocates and frees fixed-size objects?
- General-purpose malloc
- Pool (slab) allocator (Correct answer)
- Stack allocator
- Buddy system allocator
Correct answer: Pool (slab) allocator
A pool allocator pre-allocates a block of fixed-size slots, eliminating fragmentation and reducing allocation overhead to pointer manipulation.
What does the `-O2` flag passed to g++ primarily enable?