Plast interview

17. No-Exception Paths

In low-latency trading, we often compile with exceptions disabled to avoid unwinding and i-cache bloat. You must design fast parsing/validation paths that never throw yet still communicate precise failure.

bool to_int(const char* p, size_t n, int& out) noexcept {
  int x=0; for (size_t i=0;i<n; ++i) {
    unsigned d = unsigned(p[i])-'0';
    if (d>9) return false;
    x = x*10 + int(d);
  }
  out=x; return true;
}

Part 1.

Assuming exceptions are disabled, explain the correctness and optimization implications of declaring this function noexcept. How should errors be reported upstream without using exceptions?

Part 2.

(1) Does noexcept alter ABI here, or primarily inform codegen and optimization?

(2) How would you encode rich error information without heap allocation?

(3) Under what circumstances could std::terminate still be invoked?

(4) Compare out-parameters versus return-status for hot-path predictability and clarity.

(5) How do unwind tables and i-cache pressure influence latency-sensitive code?

Answer

Answer (Part 1)

noexcept promises that no exceptions escape; if a callee throws across this boundary, the program calls std::terminate. With exceptions disabled, compilers assume no unwinding, enabling tighter code, fewer landing pads, and better inlining. Propagate errors via a trivial status (e.g., bool/enum) plus out-parameters, or a small POD aggregating code and value, checked immediately by callers.

Answer (Part 2)

(1) It typically doesn’t change the ABI; it primarily guides codegen and permits omitting unwinding metadata. It can also improve inlining and simplify control flow.

(2) Use small enums/integers with out-parameters or a trivially copyable struct carrying code and result. Log or translate codes in cold paths to avoid allocations.

(3) Any exception crossing the noexcept boundary triggers termination. Library code violating noexcept or explicit termination calls will also terminate.

(4) Returning status localizes the failure branch and is often predictable. Out-parameters reduce copies for multiple results but can hurt readability if overused.

(5) Eliminating unwind tables and landing pads shrinks the text segment. Smaller binaries reduce i-cache misses and improve branch target prediction on hot paths.