? Performance Tutorial

Optimize your Ifá-Lang programs with CPU parallelism, GPU acceleration, and efficient data structures.

Profiling

Measure before optimizing:

ise benchmark(name, func) {
    ayanmo start = Iwori.now_ns();
    ayanmo result = func();
    ayanmo elapsed = (Iwori.now_ns() - start) / 1000000;
    Irosu.fo(name + ": " + elapsed + " ms");
    padap? result;
}

// Usage
benchmark("Process data", || {
    process_large_dataset(data);
});

CPU Parallelism

Use the CPU infrastructure for parallel operations:

Parallel Map

// Sequential (slow)
ayanmo results = [];
fun item ninu large_list {
    ayanmo results = Ogunda.push(results, expensive_operation(item));
}

// Parallel (fast)
ayanmo results = CPU.par_map(large_list, expensive_operation);

Parallel Reduce

// Sum 1 million numbers
ayanmo numbers = Obara.range(1, 1000001);

// Parallel sum
ayanmo total = CPU.par_reduce(numbers, 0, |x| x, |a, b| a + b);

Thread Pool Configuration

// Use all cores (default)
CPU.configure(CPU.num_threads());

// Or limit to specific count
CPU.configure(4);  // Use 4 threads

GPU Acceleration

For heavy numerical work, use GPU compute:

// Initialize GPU
ayanmo gpu = GPU.new();

// Matrix multiplication on GPU
ayanmo a = create_matrix(1024, 1024);
ayanmo b = create_matrix(1024, 1024);

ayanmo buf_a = GPU.create_buffer(a);
ayanmo buf_b = GPU.create_buffer(b);

// Compute on GPU (massively parallel)
ayanmo buf_c = GPU.matmul(buf_a, buf_b, 1024, 1024, 1024);

// Read result back
ayanmo c = GPU.read_buffer(buf_c);

GPU Memory Pooling

// Create memory pool for repeated allocations
ayanmo pool = GPU.create_slab_pool();

fun i ninu Obara.range(0, 1000) {
    ayanmo buf = pool.alloc(4096);  // Fast allocation
    // ... use buffer ...
    pool.free(buf);  // Return to pool
}

Efficient Data Structures

List vs. Map Lookup

// Slow: O(n) lookup
ayanmo users = [{"id": 1, "name": "Alice"}, ...];
ayanmo user = Ogunda.find(users, |u| u["id"] == 5);  // Scans entire list

// Fast: O(1) lookup
ayanmo user_map = {};
fun u ninu users {
    user_map[u["id"]] = u;
}
ayanmo user = user_map[5];  // Direct access

Lazy Processing

// Avoid: Creates intermediate lists
ayanmo result = Ogunda.map(data, transform);
ayanmo result = Ogunda.filter(result, is_valid);
ayanmo result = Ogunda.take(result, 10);

// Better: Single pass with early exit
ayanmo result = [];
fun item ninu data {
    ayanmo transformed = transform(item);
    ti (is_valid(transformed)) {
        ayanmo result = Ogunda.push(result, transformed);
        ti (Ogunda.len(result) >= 10) { da; }
    }
}

Memory Optimization

Resource Cleanup with ?b?

ise process_file(path) {
    ?b? file = Odi.open(path);  // Automatically closed when scope ends
    
    ayanmo content = file.read();
    padap? process(content);
}  // File closed here, even if error occurs

Check Memory Usage

ise memory_aware_process(data) {
    ayanmo stats = Kernel.memory_stats();
    ayanmo available_mb = stats.available / 1024 / 1024;
    
    ti (available_mb < 100) {
        Irosu.kigbe("Low memory, using smaller batches");
        padap? process_in_batches(data, 100);
    }
    
    padap? process_all(data);
}

Common Bottlenecks