News for hackers. Stuff that matters.
Re:Rust: Dropping heavy things in another thread can make yo... by saagarjha on Saturday May 30, 2020 @01:12PM (#23362715)
Why would you ever write a get_size function that drops the object you call it on? Surely in an actual, non-contrived usecase spawning another thread and letting the drop occur there would just be plain worse?
Re:Rust: Dropping heavy things in another thread can make yo... by Reelin on Saturday May 30, 2020 @01:26PM (#23362838)
I think the contrived use case is just for illustrative purposes? If I'm understanding correctly, the combination of cleanup code and deallocation can sometimes consume enough time that it's worth dispatching it on another thread. That's hardly specific to Rust though.

As you note that will certainly add some overhead, although that could be minimized by not spawning a fresh thread each time. It could easily reduce latency for a thread the UI is waiting on in many cases.

Re:Rust: Dropping heavy things in another thread can make yo... by tedunangst on Saturday May 30, 2020 @01:48PM (#23363058)
It would be helpful to see an example from a real application, too.
Re:Rust: Dropping heavy things in another thread can make yo... by masklinn on Saturday May 30, 2020 @03:19PM (#23363836)
A very large Vec<String> (say a few million non-empty strings) would do I'd guess, Rust would drop the Vec which would recursively drop each String.
Re:Rust: Dropping heavy things in another thread can make yo... by burpsnard on Saturday May 30, 2020 @07:57PM (#23365948)
I start thinking about c++ 'placement new' and COBOL copy record structures.

And aquire/release objects from resource pools that manage themselves. And message queue handlers. And pass-by-reference, and object pools..

Re:Rust: Dropping heavy things in another thread can make yo... by epage on Saturday May 30, 2020 @01:25PM (#23362835)
I believe this is contrived to prove a point.

And this isn't just a help in these contrived examples. I believe process cleanup (an extreme case of cleaning up objects) is one of cases where garbage collection performs better because it doesn't have to unwind the stack, call cleanup functions that are not in the cache, and make a lot of `free` calls to the allocator.

I vaguely remember reading about Google killing processes rather than having them clean up correctly, relying on the OS to properly clean up any resources of significance.

Now this doesn't mean you should do this in all cases. Profile first, see if you can avoid the large objects, and then look into deferred de-allocations ... if the timing of resource cleanup meets your application's guarantees.

Re:Rust: Dropping heavy things in another thread can make yo... by seventh-chord on Saturday May 30, 2020 @01:38PM (#23362981)
Killing a process without freeing all allocations is, as far as I can tell, routine in C. Especially for memory it makes no sense "freeing" allocations, the whole memory space is getting scrapped anyways. Of course, once you add RAAI the compiler cant reason about which destructors it can skip on program exit, and if programmers are negligent of this you get programs that are slow to close.
Re:Rust: Dropping heavy things in another thread can make yo... by estebank on Saturday May 30, 2020 @03:11PM (#23363770)
> Killing a process without freeing all allocations is, as far as I can tell, routine in C.

Many times by accident :)

> if programmers are negligent of this you get programs that are slow to close.

I wouldn't call that negligence, just not fully optimized.

Re:Rust: Dropping heavy things in another thread can make yo... by gpderetta on Saturday May 30, 2020 @02:32PM (#23363431)
exit(2) will only call destructors of static objects. Quick_exit not even those.
Re:Rust: Dropping heavy things in another thread can make yo... by Reelin on Saturday May 30, 2020 @01:32PM (#23362913)
> killing processes rather than having them clean up correctly, relying on the OS

I recall Firefox preventing cleanup code from running when you quit a few years ago. Prior to that, quitting with a lot of pages open (ie hundreds) could cause it to lock up for quite some time.

Re:Rust: Dropping heavy things in another thread can make yo... by pjmlp on Saturday May 30, 2020 @01:24PM (#23362825)
Not at all, Herb Sutter has a CppCon talk about this kind of optimisations.

It is also the approach taken by C++/WinRT, COM and UWP components get moved into a background cleaning thread, to avoid application pauses on complex data structures reaching zero count.

Re:Rust: Dropping heavy things in another thread can make yo... by nickm12 on Saturday May 30, 2020 @01:23PM (#23362815)
I took this to be a contrived example to illustrate the point. I could imagine a process that creates a big data structure (e.g. parse an xml file), pulls some data out, and then drops the data structure. If you want to use that data sooner, you can push the cleanup off your thread.
Re:Rust: Dropping heavy things in another thread can make yo... by ashtonkem on Saturday May 30, 2020 @01:25PM (#23362832)
It’s a contrived example to demonstrate the technique.
Re:Rust: Dropping heavy things in another thread can make yo... by Areading314 on Saturday May 30, 2020 @01:17PM (#23362764)
Right there is no reason to pass ownership to a function like this.