// hakmem_elo.h - ELO Rating Strategy Selection (Box Theory) // // Priority 1 optimization from ChatGPT Pro feedback: // ELO-based strategy selection for multi-objective optimization // // Why ELO beats UCB1: // - UCB1 assumes independent arms // - ELO handles transitivity (if A>B and B>C, then A>C) // - Better for multi-objective scoring (CPU + memory + page faults) // // Expected Gain: +10-20% on VM scenario (close gap with mimalloc) #pragma once #include #include // Configuration #define ELO_K_FACTOR 32.0 // ELO K-factor (higher = faster learning) #define ELO_INITIAL_RATING 1500.0 // Starting ELO rating #define ELO_MAX_STRATEGIES 12 // Max candidate strategies #define ELO_EPSILON 0.1 // Epsilon-greedy exploration (10%) #define ELO_MIN_SAMPLES 50 // Min samples before ELO update #define ELO_SURVIVAL_COUNT 6 // Top-M strategies survive // Strategy Candidate typedef struct { int strategy_id; // Unique strategy ID double elo_rating; // Current ELO rating (starts at 1500) uint64_t wins; // Number of wins uint64_t losses; // Number of losses uint64_t draws; // Number of draws uint64_t samples; // Total samples collected size_t threshold_bytes; // mmap threshold for this strategy int active; // 1 if strategy is active } EloStrategyCandidate; // Allocation Statistics (for comparison) typedef struct { uint64_t cpu_ns; // CPU time (nanoseconds) uint64_t page_faults; // Number of page faults uint64_t bytes_live; // Live memory bytes uint64_t alloc_count; // Number of allocations } EloAllocStats; // ELO System API (Box Interface) void hak_elo_init(void); void hak_elo_shutdown(void); // Strategy selection (epsilon-greedy) int hak_elo_select_strategy(void); size_t hak_elo_get_threshold(int strategy_id); // Record allocation result void hak_elo_record_alloc(int strategy_id, size_t size, uint64_t duration_ns); // Trigger ELO evolution (pairwise comparison) void hak_elo_trigger_evolution(void); // Statistics void hak_elo_print_stats(void); void hak_elo_print_leaderboard(void); // Internal helpers (exposed for testing) double hak_elo_compute_score(const EloAllocStats* stats); void hak_elo_update_ratings(EloStrategyCandidate* a, EloStrategyCandidate* b, double score_diff);