News for hackers. Stuff that matters.
Re:Rust: Dropping heavy things in another thread can make yo... by orf on Saturday May 30, 2020 @02:32PM (#23363437)
You can change the global allocator in any rust project. You can write your own easy enough, or use one like jemalloc
Re:Rust: Dropping heavy things in another thread can make yo... by josephg on Saturday May 30, 2020 @07:28PM (#23365772)
Sure; but when you’re using arenas and things like that you usually you will want different objects allocated into different pools (or with different lifetime properties). Rust only lets you pick one allocator for the entire process, so you can’t specify “all the children of this data structure go in arena A, and this other allocation goes into a traditional heap”.

It’s more awkward, but I much prefer Zig’s approach here where everything that allocates takes an allocator as a parameter. Usually the allocator is specified at compile time - in which case zig can generate identical code to the rust compiler. But when you want the flexibility, it’s there.

Aside from compilers, this is heavily used in video games where there’s often a lot of objects that get allocated per frame, and can be discarded all together. And in that case rust’s lifetime tracking would be a huge asset. The dovecot email server (C) also makes superb use of a mix of memory containers for different tasks. Given how messy email parsing is, dovecot is an absolute pleasure to read.

Re:Rust: Dropping heavy things in another thread can make yo... by steveklabnik on Saturday May 30, 2020 @07:40PM (#23365843)
Not in an abstract way, but you can do this. The compiler has (had? It’s been a while) two different major arenas, in my understanding. There’s just no non-global allocator trait yet, so it’s not easy to abstract over.
Re:Rust: Dropping heavy things in another thread can make yo... by andrepd on Saturday May 30, 2020 @08:05PM (#23365993)
>everything that allocates takes the allocator as a parameter

This is also the C++ approach

Re:Rust: Dropping heavy things in another thread can make yo... by afiori on Sunday May 31, 2020 @07:04AM (#23368718)
As someone that is interested in the topic and in Zig's "provide your own allocator" approach I have a question: would it be possible to make an allocator wrapper that moves values to be deallocated to a different thread?

As far as I know it would require both Rust's borrowing semantics and Zig's architectural choice.

From purely my own fan-boy perspective Zig approach is something that I would have really liked for Rust to adopt (I have no idea of which one came first chronologically).

Re:Rust: Dropping heavy things in another thread can make yo... by steveklabnik on Sunday May 31, 2020 @09:08AM (#23369337)
It is not an allocator wrapper, but https://docs.rs/defer-drop/1.0.1/defer_drop/index.html
Re:Rust: Dropping heavy things in another thread can make yo... by francasso on Saturday May 30, 2020 @07:26PM (#23365759)
I don't get this. Usually I either don't care about how stuff is allocated, or when I care I want to be specific about what allocator should be used where. In the second situation just setting a global allocator wouldn't cut it most of the time, I would want support for customization when I instantiate a data structure.
Re:Rust: Dropping heavy things in another thread can make yo... by bauerd on Saturday May 30, 2020 @02:53PM (#23363605)
Think with LD_PRELOAD it is always possible to override the allocator?
Re:Rust: Dropping heavy things in another thread can make yo... by jfkebwjsbx on Sunday May 31, 2020 @12:57AM (#23367307)
That wouldn’t help. The point is to use local allocators which have extra information about the data structures and usage of the memory.

This is routinely done in medium to large C++ programs for different reasons (performance, debuggability...).

Re:Rust: Dropping heavy things in another thread can make yo... by dan-robertson on Saturday May 30, 2020 @06:52PM (#23365551)
Only if it is dynamically linked