News for hackers. Stuff that matters.
Re:Why Discord is switching from Go to Rust by tsimionescu on Tuesday February 04, 2020 @04:50PM (#22241388)
> I can't speak for how their Rust cache manages memory, but the thing to be careful of in non-GC runtimes (especially non-copying GC) is memory fragmentation.

As far as I know, a mark-and-sweep collector like Go's doesn't have any advantage over malloc/free when it comes to memory fragmentation. Am I missing some way in which Go's GC helps with fragmentation?

Re:Why Discord is switching from Go to Rust by lossolo on Tuesday February 04, 2020 @05:52PM (#22241982)
Go GC implementation uses memory allocator that was based on TCMalloc (but derived from it quite a bit). They use a free list of multiple fixed allocatable size-classes, which helps in reducing fragmentation. That's why Go GC is non-copying.
Re:Why Discord is switching from Go to Rust by haimez on Tuesday February 04, 2020 @07:31PM (#22242784)
I’m not sure I follow. GC implementations that don’t copy (relocate) are inherently subject to the performance cost of “fragmentation” (in the sense of scattering memory accesses over non-adjacent regions). This is a very high price to pay when you’re dealing with modern hardware.
Re:Why Discord is switching from Go to Rust by andoriyu on Tuesday February 04, 2020 @08:31PM (#22243157)
Allocator underneath is keeping track of freed memory, so next allocation has high chance of being squeezed into memory region that has been used before. It's obviously not as good as say GC that relocates after sweep, but at least it doesn't leave gaping holes.
Re:Why Discord is switching from Go to Rust by haimez on Tuesday February 04, 2020 @09:57PM (#22243610)
Indeed, but it also doesn’t maintain locality of access nearly as well for young objects (the most commonly manipulated ones) and even older ones that survive.
Re:Why Discord is switching from Go to Rust by jstrong on Tuesday February 04, 2020 @11:28PM (#22244025)
one related point: the article mentions utilizing rust's BTreeMap, which manages its heap allocations with cache efficiency in mind: https://doc.rust-lang.org/std/collections/struct.BTreeMap.ht....

The guts of BTreeMap's memory management code is here: https://github.com/rust-lang/rust/blob/master/src/liballoc/c.... (warning: it is some of the most gnarly rust code I've ever come across, very dense, complex, and heavy on raw pointers. this is not a criticism at all, just in terms of readability). Anecdotally I've had very good results using BTreeMap in my own projects.

In terms of how the "global" allocator impacts performance, I'd expect it to play a bigger role in terms of Strings (I mean, it's a chat program), and possibly in terms of how the async code "desugars" in storing futures and callbacks on the heap (just guessing, I'm not an expert on the rust async internals).

Re:Why Discord is switching from Go to Rust by senderista on Wednesday February 05, 2020 @12:58AM (#22244377)
I was a bit surprised that BTreeMap turned out to be more memory-efficient than HashMap; can anyone shed some light on this?

One of these days I’ll get around to turning my Rust set implementation[0] into a full-blown map (it’s already <50% the size of BTreeSet for ints)...

[0] https://github.com/senderista/rotated-array-set

Re:Why Discord is switching from Go to Rust by tsimionescu on Wednesday February 05, 2020 @01:28AM (#22244489)
In the current context, fragmentation refers more to the problem of consuming extra memory through fragmentation, which malloc implementations like the one Go (or Rust, or glibc) uses can often mitigate.
Re:Why Discord is switching from Go to Rust by tsimionescu on Wednesday February 05, 2020 @01:24AM (#22244469)
Sure, but the malloc implementation in Rust probably does something similar.

What I wanted to understand is what is the difference in fragmentation between a non-copying, non-compacting GC and a non-GC runtime.