r/rust • • 12d ago

🧠 educational Memory-bounded systems and backpressure in Rust - Théodore's blog

https://totodore.github.io/memory-backpressure/

I struggled quite a bit during last year on these problems so I decided to write a little blog post with what I learned regarding memory bounding and memory back pressure in rust.

60 Upvotes

5 comments sorted by

3

u/dumindunuwan 11d ago

Why nobody mentioned about mimalloc here?

8

u/Elariondakta 11d ago

Originally I was using mimalloc because it is easier to integrate than jemalloc. However I didn't find any ways to expose metrics like you can do with jemalloc-ctl.

Some also use tcmalloc. They all have their pros and cons I guess. I did not dig enough into each one to compare them correctly.

22

u/valarauca14 11d ago edited 11d ago

Lowkey bad advice for THP handling. Linux Distros & Cloud Providers (often) enable THP because it is a net performance gain. THP is massive for cache locality because it decreases TLB pressure. There is a very real trade off with disabling it.

RDB's design is really weird and uses fork to snapshot the current running memory. This is really weird, bad, and if you want to know more Pavlo et. all wrote a whole series of papers on why memory map hacks like this shouldn't be used in modern DBs. Partially because it causes the exact issues OP/RDB is blaming THP for, no joke Pavlo et. All -> Section 3.1 discusses it directly and the problem presents as section 3.2.

Your application probably isn't a in-memory-database, so measure twice and cut once.

7

u/Aubrives 11d ago

In the context of macOS, one would want to plug into the memory pressure signals to trigger shrinking of caches / encourage allocators to return unused pages to the OS. Similar mechanisms exist on iOS.

See DISPATCH_SOURCE_TYPE_MEMORYPRESSURE (https://developer.apple.com/documentation/dispatch/dispatch_source_type_memorypressure)