--- Final Solver (used for performance test) ---
import numpy as np
from numba import njit
from typing import Any


@njit(cache=True, fastmath=True)
def _project_simplex_compact(y):
    """
    Find simplex projection threshold by iterative compaction,
    then project and return the result array.
    Uses max(y)-1 as initial theta bound to minimise passes.
    """
    n = len(y)
    buf = np.empty(n, dtype=np.float64)

    # Pass 1: scan y for total and max (read-only, no writes).
    total = 0.0
    ymax = -1e300
    for i in range(n):
        v = y[i]
        total += v
        if v > ymax:
            ymax = v

    theta = ymax - 1.0

    # Pass 2: compact candidates > theta from y into buf prefix.
    write = 0
    new_total = 0.0
    for i in range(n):
        val = y[i]
        if val > theta:
            buf[write] = val
            write += 1
            new_total += val

    n_active = write
    total = new_total

    # Pass 3+: compact buf[0:n_active] in-place.
    while True:
        theta = (total - 1.0) / n_active

        write = 0
        new_total = 0.0
        for i in range(n_active):
            val = buf[i]
            if val > theta:
                buf[write] = val
                write += 1
                new_total += val

        if write == n_active:
            break

        n_active = write
        total = new_total

    # Project result directly.
    out = np.empty(n, dtype=np.float64)
    for i in range(n):
        v = y[i] - theta
        out[i] = v if v > 0.0 else 0.0
    return out


class Solver:
    def __init__(self):
        y = np.array([1.0, 2.0, 3.0, 0.5], dtype=np.float64)
        _project_simplex_compact(y)

    def solve(self, problem: dict[str, Any], **kwargs) -> dict[str, Any]:
        y_raw = problem["y"]
        n = len(y_raw)

        if n == 0:
            return {"solution": np.array([], dtype=np.float64)}
        if n == 1:
            return {"solution": np.array([1.0])}

        if isinstance(y_raw, np.ndarray):
            y = np.asarray(y_raw, dtype=np.float64)
        else:
            y = np.fromiter(y_raw, dtype=np.float64, count=n)

        x = _project_simplex_compact(y)
        return {"solution": x}--- End Solver ---
Running performance test...
============================= test session starts ==============================
platform linux -- Python 3.12.13, pytest-9.0.2, pluggy-1.6.0
rootdir: /tests
plugins: anyio-4.13.0, jaxtyping-0.3.9
collected 3 items

../tests/test_outputs.py .
--- Performance Summary ---
Validity: True
Total Baseline Time: 6.4380s
Total Solver Time:   1.3852s
Raw Speedup:         4.6477 x
Final Reward (Score): 4.6477
---------------------------
..

==================================== PASSES ====================================
=========================== short test summary info ============================
PASSED ../tests/test_outputs.py::test_solver_exists
PASSED ../tests/test_outputs.py::test_solver_validity
PASSED ../tests/test_outputs.py::test_solver_speedup
======================== 3 passed in 239.56s (0:03:59) =========================
