3 ms·
"by just carefully writing the allocator code such that it's not doing any allocations in the allocator itself" The tautology you've inadvertently described th
by tialaramex 5y ago
"by just carefully writing the allocator code such that it's not doing any allocations in the allocator itself"
The tautology you've inadvertently described there isn't actually an allocator. Isn't that fascinating? I can't tell if you were trying to be clever or if you just didn't see what you were doing.
What's actually going on is that T is sat on top of an entire operating system (often a Unix) which provides it with an allocator. It then uses garbage collection so that the lay user needn't worry about, like many high level languages.
- AnimalMuppet 5y agoNo, it can be done. In Java, say, you have a giant static array. You allocate chunks of that. You can then return to the caller a block of memory without you ever having to call the OS's allocator. But I disagree with lmm's larger point, and I will give my own example of it. Here's an embedded system that controls the wing surfaces on a fighter aircraft. It has to adjust them every 1/40th of a second, or the wings break off (not an exaggeration). So a big part of the development process is proving that you always meet that timing. You do things like count the CPU cycles used by the code that the compiler generates. Then someone says "let's use a garbage-collected language!" Worse, they may even suggest a language that automatically allocates. They might say that you can get garbage collectors and allocators that have guaranteed-worst-case timings. But even then, you have to count the allocations, multiply by the worst-case time of an allocation, and figure in the garbage collection time. And you're faced with a bunch of cynical embedded engineers, asking "Explain to me again how this makes our lives easier?" And you can't, because it doesn't. You can write just about anything in just about any language. But software engineers aren't stupid, and they aren't sheep. The tools typically chosen for an area are usually better for doing actual work in that area.
- tialaramex 5y ago> In Java, say, you have a giant static array. You allocate chunks of that. You can then return to the caller a block of memory without you ever having to call the OS's allocator. Where did this "giant static array" come from? Seems like somebody allocated that to you and I think you already know where they got it. Keep going down the rabbit hole. Somebody is at the bottom of the rabbit hole, and they are not using Java.
- AnimalMuppet 5y agoStatic array. If you wish, it was allocated for you by the JVM, when it started your program. My point is that it wasn't allocated from a call to new(), and if someone calls your function, you don't have to call new() to give them a block. This is in fact an allocator, in the same sense that a C++ arena allocator is an allocator, and in the same sense that malloc is an allocator, and in the same sense that the OS's memory manager is an allocator. They're doing the same things - they're managing a block of memory, and returning sub-blocks of it when requested.
- lmm 5y ago> Somebody is at the bottom of the rabbit hole, and they are not using Java. They're not using C or Rust either, they're writing some kind of machine-specific stub, probably in that machine's assembly language. Yes, a tiny minority of programmers do occasionally have to write the bit-banging stuff that connects hardware to a general-purpose programming language. But that's not a problem that having a different kind of general-purpose programming language helps with.
- tialaramex 5y ago> They're not using C or Rust either, they're writing some kind of machine-specific stub, probably in that machine's assembly language. But that's what's so interesting at the bottom of the rabbit hole. Unlike the languages you've mentioned, the ones used at the bottom of the rabbithole, such as C and Rust, actually provide a mechanism to do exactly that sort of bit-banging. let x: u64; unsafe { asm!("mov {}, 5", out(reg) x); } Isn't that nice? All the surrounding code that isn't doing bit-banging still interoperates with this just fine, so you can do your bit-banging to talk to a memory controller and then... return a freshly allocated chunk of RAM to the rest of the high level code which has whizzy generic types and whatever. Because of Rust's safety rules we're obliged to explicitly flag that we know the compiler can't check our work. If this was serious work, we'd want to provide a comment for humans explaining why this is both necessary and safe, but in this toy example the answer is "No reason at all" so that's redundant. In C, partly due to lack of name spacing, the mechanism is compiler-specific (of course today Rust is effectively wedded to LLVM so for now this is a difference without practical distinction). And there's no safety oversight, you're no more or less at risk when trying to make yet another linked list than when bit-banging. But otherwise it's very similar.
- lmm 5y ago> But software engineers aren't stupid, and they aren't sheep. That hasn't been my experience at all. Maybe 5% of the time there's actually a reason that makes sense. 95% of the time it's a case of we've always done it that way, one guy tried doing it a different way 20 years ago and it didn't work then so now we know better than to try different things.