007 simple loop

? Unknown Status unknown.

Code

// PERFORMANCE TEST: Koru label loops vs Zig while loops
// Goal: Prove Koru loops (#/@) compile to same code as Zig while loops
// Based on working test: 203_labels_and_jumps
// Loop: Count to 10 million (runs ~10ms for better benchmark accuracy)
// Baseline: Zig while loop
// Threshold: 1.05x (5% slower max)

const std = @import("std");

// Event for counting/summing
~event count { i: u64, sum: u64 }
| next { i: u64, sum: u64 }
| done { result: u64 }

~proc count {
    const new_i = i + 1;
    const new_sum = sum + i;

    if (new_i < 10_000_000) {
        return .{ .next = .{ .i = new_i, .sum = new_sum } };
    } else {
        return .{ .done = .{ .result = new_sum } };
    }
}

// Print result
~event print { result: u64 }
| done {}

~proc print {
    std.debug.print("Sum: {}\n", .{result});
    return .{ .done = .{} };
}

// Loop: count to 1 million
~#loop count(i: 0, sum: 0)
| next n |> @loop(i: n.i, sum: n.sum)
| done d |> print(result: d.result)
    | done |> _
input.kz

Expected Output

Sum: 49999995000000

Imported Files

// MINIMAL TEST: Does label loop work at all?
// Just count to 10 and print each iteration

const std = @import("std");

// Count event - loop controller
~event count { i: u64 }
| next { i: u64 }
| done {}

~proc count {
    if (i < 10) {
        return .{ .next = .{ .i = i + 1 } };
    } else {
        return .{ .done = .{} };
    }
}

// Print event
~event print { i: u64 }
| done {}

~proc print {
    std.debug.print("i = {}\n", .{i});
    return .{ .done = .{} };
}

// Loop: count and print each iteration
~#loop count(i: 0)
| next n |> print(i: n.i)
    | done |> @loop(i: n.i)
| done |> _
input_minimal.kz

Test Configuration

MUST_RUN

Post-validation Script:

#!/bin/bash
# Post-validation: Check performance is within threshold

set -e

if [ ! -f "results.json" ]; then
    echo "⚠️  No benchmark results found (results.json missing)"
    echo "   Running benchmark..."
    bash benchmark.sh
fi

if [ ! -f "results.json" ]; then
    echo "❌ FAIL: Benchmark did not produce results.json"
    exit 1
fi

# Check if jq is installed
if ! command -v jq &> /dev/null; then
    echo "⚠️  jq not installed (needed to parse benchmark results)"
    echo "   Install with: brew install jq (macOS) or apt install jq (Linux)"
    echo "   Skipping performance validation..."
    exit 0
fi

THRESHOLD=$(cat THRESHOLD)

# Parse results (hyperfine format)
BASELINE_TIME=$(jq -r '.results[0].mean' results.json)
KORU_TIME=$(jq -r '.results[1].mean' results.json)

# Calculate ratio (Koru / Baseline)
RATIO=$(echo "scale=4; $KORU_TIME / $BASELINE_TIME" | bc -l)

echo ""
echo "Performance Results:"
echo "  Baseline (Zig): ${BASELINE_TIME}s"
echo "  Koru:           ${KORU_TIME}s"
echo "  Ratio:          ${RATIO}x"
echo "  Threshold:      ${THRESHOLD}x"
echo ""

# Compare to threshold
if (( $(echo "$RATIO > $THRESHOLD" | bc -l) )); then
    echo "❌ PERFORMANCE REGRESSION!"
    echo "   Koru is ${RATIO}x slower than baseline"
    echo "   Threshold is ${THRESHOLD}x"
    echo "   Regression: $(echo "scale=1; ($RATIO - 1) * 100" | bc -l)%"
    exit 1
elif (( $(echo "$RATIO < 0.95" | bc -l) )); then
    echo "✅ PERFORMANCE IMPROVED!"
    echo "   Koru is FASTER than baseline (${RATIO}x)"
else
    echo "✅ Performance within threshold"
    echo "   Overhead: $(echo "scale=1; ($RATIO - 1) * 100" | bc -l)%"
fi

exit 0