CPP Performance Optimization & Profiling 2 — Questions and Answers
Question 1: Which cache concept refers to the tendency of programs to access memory locations near recently accessed locations?
- Temporal locality
- Spatial locality (Correct answer)
- Cache associativity
- Cache coherence
Correct answer: Spatial locality
Spatial locality means programs tend to access nearby memory addresses, which is why sequential array traversal is cache-friendly.
Question 2: What is the primary purpose of loop unrolling as an optimization technique?
- Reduce memory usage
- Decrease loop overhead and enable instruction-level parallelism (Correct answer)
- Improve branch prediction
- Eliminate recursive calls
Correct answer: Decrease loop overhead and enable instruction-level parallelism
Loop unrolling reduces loop control overhead (increment, compare, branch) and exposes more independent instructions for the CPU pipeline.
Question 3: Which C++ standard attribute hints to the compiler that a branch is expected to be taken frequently?
- [[likely]] (Correct answer)
- [[hot]]
- [[predict]]
- [[branch_hint]]
Correct answer: [[likely]]
[[likely]] (C++20) hints that the branch is expected to be taken, allowing the compiler to optimize code layout for that path.
Question 4: What does 'false sharing' mean in the context of multi-threaded performance?
- Two threads share the same mutex
- Threads access different variables that reside on the same cache line (Correct answer)
- A thread reads stale data written by another thread
- Multiple threads share a single memory allocator
Correct answer: Threads access different variables that reside on the same cache line
False sharing occurs when two threads modify different variables that happen to share a cache line, causing unnecessary cache invalidation traffic.
Question 5: Which profiling technique samples the program counter at regular intervals without modifying source code?
- Instrumentation profiling
- Statistical (sampling) profiling (Correct answer)
- Manual timing with chrono
- Memory leak detection
Correct answer: Statistical (sampling) profiling
Statistical profiling periodically samples the PC to estimate where time is spent, with minimal overhead compared to full instrumentation.
Question 6: What is the effect of declaring a frequently called small function as `inline`?
- Forces the compiler to always inline the function body, eliminating call overhead
- Hints to the compiler to consider inlining, potentially eliminating call overhead (Correct answer)
- Makes the function constexpr-evaluable
- Prevents the function from being exported
Correct answer: Hints to the compiler to consider inlining, potentially eliminating call overhead
`inline` is a hint to the compiler; modern compilers may inline regardless and may ignore the hint if the function is too large.
Question 7: Which data structure layout typically yields better cache performance for iteration: Array of Structures (AoS) or Structure of Arrays (SoA)?
- AoS, because related fields are co-located
- SoA, because iterating one field accesses a contiguous array (Correct answer)
- They always perform identically
- AoS for reads, SoA for writes
Correct answer: SoA, because iterating one field accesses a contiguous array
SoA keeps each field in its own contiguous array, so iterating a single field has perfect spatial locality and no wasted cache line bandwidth.
Which cache concept refers to the tendency of programs to access memory locations near recently accessed locations?