News for hackers. Stuff that matters.
Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Saturday May 30, 2020 @05:24PM (#23364865)
I am suggesting they apply good practices. I'd never imagine that compilers were actually doing what was stated -- sounds awful.

I understand it's tradeoffs and we all have real-world limitations to contend with -- but again, of all the corners that could be cut that's exactly the one I didn't imagine they would.

Nasty.

Re:Rust: Dropping heavy things in another thread can make yo... by correnos on Saturday May 30, 2020 @07:08PM (#23365651)
Of the three compilers I've worked on in-depth, only one of them had a "normal" memory management scheme.

One of them was unburdened by any thought of freeing stuff, and relied entirely on the application exiting for cleanup. This was very convenient to work with, and never ended up posing an issue.

Another used a series of allocation arenas, where certain arenas would be cleared at certain points in the compiler pipeline. This made for both speedy alloc/freeing and avoided leaks, since you weren't at risk of "forgetting" a data structure. It was also a major headache to keep track of exactly what the longest lifetime of a long-lived datastructure might be, and to pick an arena that won't be cleared in the meantime. Unfortunately the programs compiled with this compiler were large enough that we certainly couldn't have gotten away with just leaking memory; we sometimes OOMed as-is!

The third used standard C++ memory management. This compiler was quite simple, and the vast majority of its data used stack-based lifetimes. For a more complex compiler this would've become a headache.

I think that all of these compilers chose the correct allocation strategy for what they were doing. "Good practices" aren't as universal as we might like to believe, they depend entirely on the context in which a tool is designed to operate. And yes, we can guard to some extent against that context changing, but for the most part that's why we keep getting paid.

Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Saturday May 30, 2020 @07:53PM (#23365930)
Judging by the downvotes I am getting (with zero explanation as to why), I'd say that many misunderstood the "good practices" part and took offence, as if I said there's only one good practice.

I was taught -- including at the start of my career when I used exclusively C/C++ (about 18.5y ago) -- to take care of all resources I was using and not rely on runtimes.

I understand and appreciate different usages but to me doing a proper cleanup was the sane default for most programmers. And that's all what I was saying.

Obviously, as one digs deeper in a specialised area where more and more efficiency is demanded then they have to reach for tools that most of us wouldn't normally. That's quite normal and was always interesting for me to read about.

Re:Rust: Dropping heavy things in another thread can make yo... by grayclhn on Saturday May 30, 2020 @09:36PM (#23366463)
I didn't downovte, but asides like, "sounds awful" and "nasty" definitely read as judgemental. Your other comments on this thread make you seem much more open minded than this one did.
Re:Rust: Dropping heavy things in another thread can make yo... by thebean11 on Saturday May 30, 2020 @06:07PM (#23365209)
Can you articulate why it's a bad practice? If it works better than alternatives and it's documented, not really sure what the issue is.

I don't think it's even that uncommon. I believe some HFT firms run Java with a huge amount of RAM and GC disabled, and get around it by just rebooting the software occasionally.

To me writing software like that is fair game, I don't see the point in being dogmatic about "how things should be done".

Re:Rust: Dropping heavy things in another thread can make yo... by nitrogen on Wednesday June 03, 2020 @04:19PM (#23408148)
This thread has a lot of branches, so I am just picking one for this general reply:

I recall reading somewhere, years ago, that some OSes couldn't be relied upon to release unfreed memory when a process terminated. In those contexts, fastidious freeing would be important even in short-lived processes.

In my own C code I tend to free everything so that I get a clean trace from Valgrind and don't risk masking legitimate bugs, but I typically write long-running daemons.

Re:Rust: Dropping heavy things in another thread can make yo... by jfkebwjsbx on Sunday May 31, 2020 @01:00AM (#23367322)
It is bad practice if your code cannot turn on deallocation for debugging purposes or library-like usage.
Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Saturday May 30, 2020 @06:25PM (#23365347)
Mostly because I look at it from the angle of one-off / general purpose / CLI programs. If one such has to run for 10-30 seconds and its memory just keeps growing and growing with the idea of throwing it all away at the end and letting the OS handle it, it might become disruptive for other programs on the machine.

For specialised apps and servers it's of course a perfectly good practice.

Re:Rust: Dropping heavy things in another thread can make yo... by rcxdude on Saturday May 30, 2020 @05:31PM (#23364944)
Deallocation at the end of a program's execution can substantially add to its runtime, and it's entirely waste. It's a much more common strategy than you might think.
Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Saturday May 30, 2020 @05:37PM (#23364990)
You are right, I indeed didn't know it was that common.

But still, in a world where languages and runtimes are also judged by their ability to run in lambda/serverless setups, I'd think this practice will start being obsolete, wouldn't you think?

(What I mean is that I imagine that any serverless function that runs in severely constrained and measured environments like the AWS Lambda would gain a significant edge over the competition if it did an eager cleanup. Should allow more of them to work in parallel?)

Re:Rust: Dropping heavy things in another thread can make yo... by setr on Saturday May 30, 2020 @07:32PM (#23365793)
I don't understand; a lambda function executes and closes, and any leak gets handled by lambda runtime (by freeing the whole lambda function)-- exactly the same as running a cli tool. It seems to me that the server-less context is actually exactly where you'd employ this strategy: the programs by definition cannot be long-lived, so unless they generate so much garbage as to oom in that timeline, there is no need to deallocate anything yourself.

The leak to watch out for is reduced to the persisted data (eg in S3) between executions

Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Saturday May 30, 2020 @07:49PM (#23365903)
Yeah, this is what I was saying -- that it could produce too much garbage if no manual free-ing of memory is done.

I suppose, judging by the downvotes, that some find that perspective naive so I'll just cut it here because no productive discussion is happening. :(

Re:Rust: Dropping heavy things in another thread can make yo... by setr on Sunday May 31, 2020 @10:31AM (#23369796)
the purpose/pattern is to ignore deallocation when the program finishes -- not during runtime. If you have 30MB of space, and your program will only ever use 20MB, why bother?

You only need to care when you're at risk of OOM'ing during runtime. If you're generating too much garbage, you either need to start cleaning up after yourself, or you need to generate less of it -- but it doesn't matter which choice you take from the perspective of the OS or the server-less function.

The only reason I can think of for server-less to impact the validity of this pattern is from it's lower memory capacity -- but otherwise it's the same constraints. And anyways, it's probably not nice to generate a few GB of garbage even if the user has the space

Re:Rust: Dropping heavy things in another thread can make yo... by folmar on Saturday May 30, 2020 @06:21PM (#23365315)
Most compiler are not designed to run in daemon mode, specifically it's non-issue since their startup is normally fast. And the compiler and runtime are a different thing.
Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Saturday May 30, 2020 @06:26PM (#23365357)
I realise that, but nowadays language servers are a pretty normal practice in no small amount of areas.
Re:Rust: Dropping heavy things in another thread can make yo... by smabie on Saturday May 30, 2020 @09:48PM (#23366522)
You mean like LSP? Why would a compiler and a language server be integrated in the first place? Clearly a LSP server wouldn't use a no-freeing strategy, but there's no reason why it would cause issue with a compiler. A compiler must terminate, assuming parsing isn't turing complete (like Perl) and that the source code is finite. This is why it's okay for it to leak memory.
Re:Rust: Dropping heavy things in another thread can make yo... by steveklabnik on Sunday May 31, 2020 @09:07AM (#23369331)
> Why would a compiler and a language server be integrated in the first place?

Because a language server needs to do a lot of the same work as a compiler.

This is an eventual hope for rustc. For now, the latest language server and it share a bunch of libraries, but language servers are effectively compiler frontends.

Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Sunday May 31, 2020 @08:23AM (#23369099)
Yep, I guess I conflated compilers and LSP. I imagined compilers would have daemon modes. Seems like I was wrong.
Re:Rust: Dropping heavy things in another thread can make yo... by michaelcampbell on Sunday May 31, 2020 @08:16AM (#23369056)
> I'd never imagine that compilers were actually doing what was stated -- sounds awful.

And how many millions of iterations have been done successfully in that "awful" system?

The very fact that you never imagined it I think says a lot.

Re:Rust: Dropping heavy things in another thread can make yo... by pdimitar on Sunday May 31, 2020 @08:20AM (#23369079)
Well, I have been taught to take good manual care of all used resources. It is kind of a cognitive shock when you see all your training hand-waved away with "let the OS handle my mess of allocated objects that I'll never call `free` on". It's kind of disappointing on some level. :)

As I acknowledged in other comments of mine downthread, I understand that different situations require different tradeoffs. It's just that forgoing memory deallocation wasn't one of them in my head.