PeaBox Lab › PeaLang › Learn › Chapter 33 › Lesson 2

Part V — The Modern Experience Layer · Chapter 33 · Lesson 2 of 2

Caches, connection pools, profiling and fast hashing

The second half of v2.1.0 is the unglamorous kind of performance: avoiding repeated work, avoiding copies, and knowing exactly where the time goes before you decide what to optimize.

mmap I/O

mmap_write("/tmp/data.bin", big_record)
let head = mmap_read("/tmp/data.bin", 0, 4096)
let offsets = mmap_search("/tmp/data.bin", b"END_OF_RECORD")

Memory-mapped reads avoid the copy-then-parse dance for large files: the kernel hands you the pages, you read them in place. The difference is sharpest for log scanning, large JSONL ingestion and anything where the working set outruns the allocator. One caveat that catches everyone once: mmap'd data reflects the file as it exists now — snapshot semantics are not a thing; if the file can be replaced mid-run, checksum first.

AdvancedCache — four policies, one API

let lru  = AdvancedCache(500, "lru")    # recency
let lfu  = AdvancedCache(500, "lfu")    # frequency
let fifo = AdvancedCache(500, "fifo")   # simple, predictable
let ttl  = AdvancedCache(500, "ttl", { "ttl_seconds": 300 })

lru.put("user:42", record)
let hit = lru.get("user:42")

AdvancedCache is the cache the stdlib always needed. Policy selection should come from measured eviction behavior, not habit:

Connection pooling

let pool = ConnectionPool({
  "size": 10,
  "factory": open_pg_connection
})

ConnectionPool bounds concurrent connections, hands them out fairly and recycles broken ones. Pair it with the per-call limits from Chapter 14: a pool without limits is a queue with extra steps and a database connection leak at the end of it.

Profiling without guessing

let prof = Profiler()
prof.tick()
do_work()
prof.tick()
print prof.report()

let stats = profile_function(embed_and_store, payload)

Profiler gives you cumulative counters with named ticks — cheap enough to leave in and read later. profile_function times one callable with arguments and returns a dict: duration, call count, rough attribution. Run it on the real workload with realistic arguments; synthetic microbenchmarks lie politely, and they always lie in the direction you wanted to hear.

Batching and parallel chunks

let bs = optimize_batch_size(total_items, item_cost_ms, target_ms)
let chunks = parallel_chunks(records, 8)

optimize_batch_size picks the largest batch that fits a latency budget given per-item cost — letting you trade throughput for tail latency explicitly instead of by accident. parallel_chunks splits work into N evenly sized chunks ready for the structured-concurrency primitives from Chapter 13. The combination — right size, right split — is most of the optimization work once the design is right.

fast_hash

let h = fast_hash({ "user": 42, "role": "admin" })

Small, stable, fast SHA-256 content hashes for cache keys, deduping and content-addressable storage. Do not use it for passwords — use a slow KDF for that — and do not confuse it with a checksum used for security: fast_hash is for correctness tooling, not access control.

Guided practice

← Previous Next →