4 ms·
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 lettin
by saagarjha 6y ago
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?
- Areading314 6y agoRight there is no reason to pass ownership to a function like this.
- nickm12 6y agoI 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.
- pjmlp 6y agoNot 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.
- ashtonkem 6y agoIt’s a contrived example to demonstrate the technique.
- epage 6y agoI 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.
- Reelin 6y ago> 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.
- seventh-chord 6y agoKilling 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.
- gpderetta 6y agoexit(2) will only call destructors of static objects. Quick_exit not even those.
- estebank 6y ago> 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.
- Reelin 6y agoI 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.
- tedunangst 6y agoIt would be helpful to see an example from a real application, too.
- masklinn 6y agoA 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.
- burpsnard 6y agoI 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..