Skip to content

Commit 2a8366a

Browse files
committed
refactor(runtime): use VMPILOT_TRY / TRY_ASSIGN on vm_engine hot path
Six identity-propagation sites in the dispatch loop that used to read: auto x_or = f(...); if (!x_or) return tl::make_unexpected(x_or.error()); auto x = *x_or; collapse to VMPILOT_TRY or VMPILOT_TRY_ASSIGN. Covers BlobView::create, pipeline::fetch_decrypt_decode, pipeline::verify_bb_mac, pipeline::enter_basic_block, dispatch_unit, and VmEngine::create. Most of the 120 call sites the retrospective estimated are already gone — schema_parse subsumed the per-field require_* chains. What remains falls into two groups: pure identity propagation (migrated here and in the companion commits) and error-code substitution (e.g. for a generic BadCbor), which stays imperative because the macro forces the original error.
1 parent fca3b2e commit 2a8366a

2 files changed

Lines changed: 11 additions & 18 deletions

File tree

runtime/src/vm_engine.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ VmEngine<Policy, Oram>::create(
4141
m->blob_storage.assign(blob_data, blob_data + blob_size);
4242

4343
// Validate blob via BlobView (pointing into owned storage)
44-
auto blob_or = BlobView::create(m->blob_storage.data(), m->blob_storage.size());
45-
if (!blob_or) return tl::make_unexpected(blob_or.error());
46-
m->blob = *blob_or;
44+
VMPILOT_TRY_ASSIGN(blob, BlobView::create(m->blob_storage.data(), m->blob_storage.size()));
45+
m->blob = blob;
4746

4847
// 2. Key derivation (doc 16 §6: all via BLAKE3_KEYED, not blake3_kdf)
4948
//
@@ -371,9 +370,7 @@ execute_one_instruction(VmExecution& exec, VmEpoch& epoch,
371370
VmOramState& oram, const VmImmutable& imm) noexcept
372371
{
373372
// ── Phases A-C: FETCH + DECRYPT + DECODE ────────────────────────────
374-
auto insn_or = pipeline::fetch_decrypt_decode(imm, exec, epoch);
375-
if (!insn_or) return tl::make_unexpected(insn_or.error());
376-
auto insn = *insn_or;
373+
VMPILOT_TRY_ASSIGN(insn, pipeline::fetch_decrypt_decode(imm, exec, epoch));
377374

378375
// ── Phase C': RESOLVE operands + FPE decode ─────────────────────────
379376
pipeline::resolve_operands(imm, exec, epoch, insn);
@@ -645,14 +642,12 @@ VmEngine<Policy, Oram>::dispatch_unit() noexcept
645642
| (exec_.current_bb_id & m_id);
646643

647644
// ── L.3: Always verify BB MAC ───────────────────────────────
648-
auto mac_r = pipeline::verify_bb_mac(*imm_, exec_, *epoch_);
649-
if (!mac_r) return tl::make_unexpected(mac_r.error());
645+
VMPILOT_TRY(pipeline::verify_bb_mac(*imm_, exec_, *epoch_));
650646

651647
// ── L.4: Always enter_basic_block ───────────────────────────
652648
exec_.branch_taken = false;
653-
auto enter_r = pipeline::enter_basic_block(
654-
exec_, *epoch_, *imm_, target);
655-
if (!enter_r) return tl::make_unexpected(enter_r.error());
649+
VMPILOT_TRY(pipeline::enter_basic_block(
650+
exec_, *epoch_, *imm_, target));
656651

657652
// ── L.5: RET_VM resume — branchless apply via MUX ───────────
658653
{
@@ -687,9 +682,8 @@ tl::expected<VmExecResult, DiagnosticCode>
687682
VmEngine<Policy, Oram>::execute() noexcept
688683
{
689684
while (true) {
690-
auto r = dispatch_unit();
691-
if (!r) return tl::make_unexpected(r.error());
692-
if (*r == VmResult::Halted) {
685+
VMPILOT_TRY_ASSIGN(r, dispatch_unit());
686+
if (r == VmResult::Halted) {
693687
// Decode return value from register 0 using current FPE key.
694688
// After the last instruction's ratchet, regs[0] is encoded
695689
// under exec_.insn_fpe_key. Convenience one-shot FPE_Decode.

runtime/src/vm_runner.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ template<typename Policy, typename Oram>
5454
tl::expected<VmExecResult, DiagnosticCode> VmRunner<Policy, Oram>::run() {
5555
auto blob = build_blob_internal();
5656

57-
auto engine = VmEngine<Policy, Oram>::create(
57+
VMPILOT_TRY_ASSIGN(engine, VmEngine<Policy, Oram>::create(
5858
blob.data(), blob.size(), seed_,
59-
load_base_delta_, init_regs_, num_init_regs_);
60-
if (!engine) return tl::make_unexpected(engine.error());
59+
load_base_delta_, init_regs_, num_init_regs_));
6160

62-
return engine->execute();
61+
return engine.execute();
6362
}
6463

6564
template<typename Policy, typename Oram>

0 commit comments

Comments
 (0)