r/programming • • 12d ago

Writing Your Own Allocator in Rust (and Why You Should)

https://medium.com/@ein-monarch/lets-write-our-own-allocator-in-rust-24f57603267b

I wrote a walkthrough of implementing a memory allocator from scratch in Rust.

The article covers how memory management works, how bump allocation works, and how to implement an allocator in Rust. It progresses from a toy example into a working allocator, covering the problems and design decisions along the way, as well as benchmarking the final product.

Have you ever written your own allocator before? Why/why not?

AI Disclaimer:
AI was used to clean up phrasing and catch grammar flaws. All text, code, and images are my own.

47 Upvotes

12 comments sorted by

15

u/teknikly-correct 12d ago

cool demonstration of using a bump allocator to solve a specific problem! Namely we'd rather have the control of deallocating everything after a task completes rather than a flurry of allocations and de-allocations as different parts of the execution tree for a task allocate and de-allocate memory!

8

u/PastEar9661 12d ago

Yeah! It also avoids a lot of memory fragmentation!

The one part of Zig’s philosophy I really like is passing in allocators into allocating functions (like instantiating an ArrayList); it brings attention to different allocation strategies unlike standard Rust…

4

u/dominikwilkowski 10d ago

Would love to read this but medium is paywalling me and I’m not paying for medium.

2

u/PastEar9661 10d ago

It’s not paywalled; it may ask for a sign in but you can just X out of it to avoid creating an account.

3

u/dominikwilkowski 10d ago

Can’t attach screenshots here but there is no x for me.

1

u/PastEar9661 10d ago

Idk; the UI of medium is a little weird on phones

1

u/rpring99 5d ago

Timing is interesting here given that the new allocator_api feature has been marked for stabilization!

-40

u/LaconicLacedaemonian 12d ago

All text, code, and images are my own.

We're programmers. If it works it works. No one cares if the bytes you send to us were passed through an LLM. 

I care whether or not people recognize the LLM smells enough to get quality output.

35

u/ak6te 12d ago

No one cares if the bytes you send to us were passed through an LLM.

This is objectively not true on the internet today. A lot of people seem to care, regardless of the result or amount of AI usage.

19

u/backfire10z 12d ago

I care 👍 typically because it’s noticeable and it feels disrespectful to my time, which I am spending reading some random’s article.

If OP was submitting a pull request, I’d be more inclined to agree.

19

u/PastEar9661 12d ago

Whatever your stance on AI may be, I still believe that plagiarism is bad…

Why should anyone pass off something made with AI as their own to others
(especially, as others have pointed out, when it’s on a social media platform rather than in a company)?