// hakmem_batch.h - madvise Batching for TLB Optimization (Box Theory) // // Priority 3 optimization from ChatGPT Pro feedback: // Batch MADV_DONTNEED calls to reduce TLB flush overhead // // Why batching matters: // - mimalloc is 2× faster on VM scenario partly due to batched madvise // - Each madvise() call triggers TLB flush (expensive!) // - Batching 4MB worth of blocks reduces TLB flushes by 50-100× // // Expected Gain: +20-30% on VM scenario #pragma once #include #include // Configuration #define BATCH_THRESHOLD (8 * 1024 * 1024) // 8MB: flush batch when reached (Phase 6.11: increased from 1MB for 2MB allocations) #define BATCH_MIN_SIZE (64 * 1024) // 64KB: only batch large blocks #define BATCH_MAX_BLOCKS 256 // Max blocks in batch // Batch state typedef struct { void* blocks[BATCH_MAX_BLOCKS]; // Pointers to blocks size_t sizes[BATCH_MAX_BLOCKS]; // Sizes of blocks int count; // Number of blocks in batch size_t total_bytes; // Total bytes in batch } DontneedBatch; // Batching API (Box Interface) void hak_batch_init(void); void hak_batch_shutdown(void); // Add block to batch (returns 1 if flushed, 0 if buffered) int hak_batch_add(void* ptr, size_t size); // Force flush all batched blocks void hak_batch_flush(void); // Statistics void hak_batch_print_stats(void); // Query size_t hak_batch_get_pending_bytes(void); int hak_batch_get_pending_count(void); // Page-level DONTNEED batching (for managed pool pages): enqueue whole pages // Flush performs MADV_DONTNEED only (no munmap), safe with retained VA pointers. int hak_batch_add_page(void* page, size_t size);