Programming thread

  • 🔧 Chat is under maintenance.
    🧊 New server is in an office waiting on a mexican.
  • Want to keep track of this thread?
    Accounts can bookmark posts, watch threads for updates, and jump back to where you stopped reading.
    Create account
You can absolutely do a LOT of stuff without heap allocations. Statically allocating everything IS a viable solution for many cases (like spaceflight afair)
Ada makes it very easy to program without access types (pointers or references) and dynamic allocations (a language feature) through out and in out parameters. If a subprogram has bounded work to do, making it a finite-state automaton in effect, it's by far easiest to give it all of the working storage it needs while calling it with its other parameters. This is high-level, not just a chunk of memory, so it's not like it becomes less safe. Advantages include being able to reuse the storage between calls, being able to allocate that storage itself as a local variable in the caller, and so on. The ideal Ada program has dynamic allocations at the very tip top, and nowhere else.
 
Ada makes it very easy to program without access types (pointers or references) and dynamic allocations (a language feature) through out and in out parameters. If a subprogram has bounded work to do, making it a finite-state automaton in effect, it's by far easiest to give it all of the working storage it needs while calling it with its other parameters. This is high-level, not just a chunk of memory, so it's not like it becomes less safe. Advantages include being able to reuse the storage between calls, being able to allocate that storage itself as a local variable in the caller, and so on. The ideal Ada program has dynamic allocations at the very tip top, and nowhere else.
I meant it as in, per the NASA guidelines, at least afair, the equivalent of malloc is basically banned entirely.
 
I meant it as in, per the NASA guidelines, at least afair, the equivalent of malloc is basically banned entirely.
That'd be the Lockheed Martin coding standard developed for the F-35 Joint Strike Fighter Air Vehicle software project (Document Number 2RDU00001 Rev C, December 2005). Public archive available from Stroustrop himself here. Based as fuck:

AV Rule 206 (MISRA Rule 118 said:
Allocation/deallocation from/to the free store (heap) shall not occur after initialization.

Note that the “placement” operator new(), although not technically dynamic memory, may only be used in low-level memory management routines. See AV Rule 70.1 for object lifetime issues associated with placement operator new().
The whole standard is a very educational read, and I highly recommend perusing it at your leisure. This is how experts write software they expect to disconnect people only when explicitly authorized by the operator.
 
Bruh this is why I think we need to start using ECS style stuff for the heap.

Segmented arrays line up with cache lines very well and can simulate basically any other data structure by having a component for topology; a linked list can have a component for "next" (and previous), a tree could have a parent component, and if it's a fixed n-ary tree, children. Having this in a tight block means that you're not fragmenting because it's in blocks that are stacked continuously and the allocator for the ECS grabs and frees big chunks not one struct at a time. The ECS library is responsible for handling 'freeing' (marking delete until the next time it actually moves things and perhaps freeing, etc) so there's no way a user can forget to properly free().

Yes games did it first. So what? You can use this for real time stuff.
 
Bruh this is why I think we need to start using ECS style stuff for the heap.

Segmented arrays line up with cache lines very well and can simulate basically any other data structure by having a component for topology; a linked list can have a component for "next" (and previous), a tree could have a parent component, and if it's a fixed n-ary tree, children. Having this in a tight block means that you're not fragmenting because it's in blocks that are stacked continuously and the allocator for the ECS grabs and frees big chunks not one struct at a time. The ECS library is responsible for handling 'freeing' (marking delete until the next time it actually moves things and perhaps freeing, etc) so there's no way a user can forget to properly free().

Yes games did it first. So what? You can use this for real time stuff.
I'm not too familiar with ECS systems in practice, but aren't you just kicking the malloc-can down the road with this? And how would this even work? How would the lib know when an entity can be "freed"/invalidated if the user isn't supposed to call some free() equivalent? Is this just reference counting or GC with extra steps or how would this work?

(just giving you the benefit of doubt, I think you have no idea what you're talking about.)
 
I'm not too familiar with ECS systems in practice, but aren't you just kicking the malloc-can down the road with this? And how would this even work? How would the lib know when an entity can be "freed"/invalidated if the user isn't supposed to call some free() equivalent? Is this just reference counting or GC with extra steps or how would this work?

(just giving you the benefit of doubt, I think you have no idea what you're talking about.)
Yesn't.

Whatever the data structure is, if it handles memory the right way, and we test and prove and validate the data structure, we did it. Then everyone uses it. Go yo DS.SaveMyShit(stuff) and DS.GimmeTheThing(handle) or DS.FindThingMatching(property == whatever). Now, any part of the program can call DS.GimmeTheThing and they don't have to worry about it.

Yes, obviously, stale handles or a loop that just spews shit into any data structure are problems, but you're not running into the dangerous shit with pointers to nowhere. A lot of garbage churn is now just data in a segmented array that is as big as it needs to be and just reuses handles with generational changes blah blah blah there's a lot of optimization here. Also, the ECS's backing arrays can be grown in chunks that work with arenas and slabs that make the OS happy, and can be contiguous in the first place.

The alternative is we make a struct and pass pointers around. This is hard. This is a pain in the ass. But you can do it.

You can do it, look at invasive doubly linked lists in the Linux kernel!
People don't like to. Rust's entire thing is bowling with bumpers and fucking with a condom with your pointers.

My point is unless you really really need to... don't! Just shove everything in a segmented array and feed your cachelines. This is how games and real time stuff works. And it works well.
 
Whatever the data structure is, if it handles memory the right way, and we test and prove and validate the data structure, we did it. Then everyone uses it. Go yo DS.SaveMyShit(stuff) and DS.GimmeTheThing(handle) or DS.FindThingMatching(property == whatever). Now, any part of the program can call DS.GimmeTheThing and they don't have to worry about it.

Yes, obviously, stale handles or a loop that just spews shit into any data structure are problems, but you're not running into the dangerous shit with pointers to nowhere.
If your handles can be created and invalidated you still get all the lifetime related bugs like use after free(or gimme after invalidate I guess), you can still run OOM, you still need to run some sort of allocator to figure out which id to use when running DS.SaveMyShit(stuff), you still need to keep track of which ids are vacant. Maybe you get to somewhat reduce error-prone pointer jougling in user code with your ECS systems and depending on use case you trade additional memory usage overhead for less MM overhead and better memory locality but I don't see at all how this in any way solves the fundamental problems malloc causes in mission critical RT code.

Also this statement bugs me:
The ECS library is responsible for handling 'freeing' (marking delete until the next time it actually moves things and perhaps freeing, etc) so there's no way a user can forget to properly free().
The ECS lib still needs to somehow know when an entity can be deleted. How would this work without the user explicitly telling the lib? If it's explicit, weather you call malloc(size)/free(ptr) or DS.SaveMyShit(stuff)/DS.NukeTheThing(handle) doesn't matter, you get the same classes of MM bugs.

My point is unless you really really need to... don't! Just shove everything in a segmented array and feed your cachelines.
Don't get me wrong, I'm not arguing against ECS in general, i'm saying ECS doesn't address the reasons why these coding standards say don't use malloc.
 
Last edited:
If your handles can be created and invalidated you still get all the lifetime related bugs like use after free(or gimme after invalidate I guess), you can still run OOM, you still need to run some sort of allocator to figure out which id to use when running DS.SaveMyShit(stuff), you still need to keep track of which ids are vacant. Maybe you get to somewhat reduce error-prone pointer jougling in user code with your ECS systems and depending on use case you trade additional memory usage overhead for less MM overhead and better memory locality but I don't see at all how this in any way solves the fundamental problems malloc causes in mission critical RT code.
Handle generation bugs are much less bad than use after free/etc.
No, ECS cannot prevent someone from consuming memory or literally running out of it.
Yes, allocation for loading a lot of data at once can be a challenge especially if you don't know the size of what you're loading and how many things are loaded, so you have to do the best you can.
Yes you have to say delete it... fuck, I see your point now!

I don't know how the ECS could know "hey these entities setup by a method that just went out of scope can go away now" without some kind of reference to the scope. Crap. TodayILearned Bevy does "Strong handles". I guess a destructor could say "hey this list of entities can go away:", etc. Scope handles too, huh.

I went on a "wait, FUCK, cache lines" kick a while ago and experimented with "wait, I could just have a component that's the handle of a parent/child/next/prev?!" thing and found myself really really hating heap objects and reference shenanigans. I even recreated some standard library things ala ECS, benched the fuck out of it, thoroughly enjoyed it, then went through the Guava test suite and almost thought about putting it on github, but then I talked about it here and realized I'd phonebook myself.

I then realized Rust can't do a ring buffer and went "... wait a minute."

Also yes a true RT codebase would be pretty much static in any sense I could imagine. Fixed-capacity ECS is probably fine, but I didn't say that. I just went "it'll handle it!!" gesturing at ECS in general.

Admittedly the last time I went into brk/sbrk/malloc's internals was a long time ago.
 
I then realized Rust can't do a ring buffer and went "... wait a minute."
I have no idea what this has to do with anything but It's a good thing std provides a ring buffer (VecDeque) otherwise ring buffers would indeed be a PITA in Rust.

Fixed-capacity ECS is probably fine
Not really since you still have almost all of the dynamic memory problems just that you're dealing with entities and ids rather than byte slices and pointers. What you'd need to is having each id statically assigned. At that point ECS is pretty much just a fancy way of saying "struct of arrays instead of array of structs" though. Not that there's anything wrong with that.

Using ECS is imo orthogonal to dynamic memory allocation.
No ECS/no dynamic memory: classical embedded(although embedded is technically also orrhogonal to dynamic memory AND ECS but you get what I'm saying)
No ECS/dynamic memory: "normal" programming
ECS/no dynamic memory: static "struct of arrays instead of array of structs", essentially just a memroy layout optimization
ECS/dynamic memory: game dev
 
Last edited:
I have no idea what this has to do with anything but It's a good thing std provides a ring buffer (VecDeque) otherwise ring buffers would indeed be a PITA in Rust.
Borrow checker screams if you do a pointer-linked circular one, so you end up on indices, which is the handle thing all over again.


Not really since you still have almost all of the dynamic memory problems just that you're dealing with entities and ids rather than byte slices and pointers. What you'd need to is having each id statically assigned.
I've never done RT as a job. I've done toy crap on an old computer. That said, question?

Isn’t the problem with malloc() that allocation (and free) time is hard to bound, and fragmentation builds up until you can’t get a block even when the total free memory is there?

I would think that a pool of uniform fixed-size slots would handle that. I’ve played with “double the backing array(s)((AoSoAwhatever))” and the heap was fine. Because the heap never had rando objects spewed on it. You just had the backing storage (columns, if we’re using archetype words) growing and shrinking. I played with coefficients less than 2; the big thing was chunking in 16kb sections for cache locality.

My lack of experience probably led to me using the wrong terminology and talking past you, but do you see what I mean?

I'm increasingly just going WHY ARE WE DOING HEAP VOMIT AND REFERENCE TWIDDLING and I can't see why.
 
As an infra guy, this is the very first time I hear about Dokploy.

https://youtube.com/watch?v=mznYKPvhcfw
I must say, pretty insightful vid. Anyone has any experience with it?
Think of Dokploy as your free self hostable alternative to platforms like Heroku, Vercel, and Netlify, leveraging the robustness of Docker and the flexibility of Traefik.
They're just making shit up now, right?

We've already got Kubernetes at home.
 
As an infra guy, this is the very first time I hear about Dokploy.

My son, be not led astray by the worldly fascination with containers on containers. The Lord has brought us raw hardware, rcctl, and jails, and that is all that a true sysadmin needs.

(Yeah, I know I'm fighting a losing battle by still ignoring Docker and all related/similar crap, but the old ways still serve me fine.)
 
I've never done RT as a job.
Me neither, but I'm a practically omniscient super-autist and can correctly intuit what others get wrong even after doing actual research.

My lack of experience probably led to me using the wrong terminology and talking past you, but do you see what I mean?
Your terminology is no worse than mine, don't worry. I understand what you're saying.

Isn’t the problem with malloc() that allocation (and free) time is hard to bound, and fragmentation builds up until you can’t get a block even when the total free memory is there?
Well yeah, that's two of the problems but those are by far not the only one.s The main problem I see, and this doesn't only apply to RT, is that it is very easy to fuck up using malloc/free and you can't prove that you're doing it correctly in the general case with static analysis and the like. The idea in Rust is to constrain this shitfuckery in such a way that the compiler can statically prove that you're not fucking it up, not only malloc/free directly but memory access in general. That doesn't solve memory leaks though for example.

I'm increasingly just going WHY ARE WE DOING HEAP VOMIT AND REFERENCE TWIDDLING and I can't see why.
I'd say because we apparently can't special case allocations and other things regarding MM automatically. Every malloc that the compiler can't optimize out entirely goes the the same function, which does know nothing about the future lifetime of the allocation, weather or not it might need to grow later and a bunch of other things that the compiler knows(or at least can make a reasonably optimized guess). Simple example just to illustrate the point: Let's say we get a large number of small allocation requests of varying sizes. If we're the malloc implementation we have no idea when which of these live how long so best we can do is to try and put allocs of the same size together to avoid gaps and make management easier. If we where the complier we could know that a specific subset of these allocations will remain valid until some time later when all of the subset will be freed at the same time. The compiler could in theroy use a special arena stategy for this specific subset instead of putting it all on the general heap and have malloc deal with the random fragmentation.

I don't actually know if anyone has tried implementing something like this somehow but from my POV i'd say the software stack is pretty much deadlocked. Compilers can't fix this because the compiler doesn't control the malloc impl, they are allowed to optimize out malloc calls in some cases but I don't think they can replace malloc calls with thier own special cased smart malloc. The malloc implementors can't fix this because malloc doesn't have the info available they need to make such decisions . A new language could maybe fix this but I think it wouldn't really be a systems programming language anymore since you'd loose a lot of control over your datastructures. Maybe there is a way but I don't see it as of now.

The reason we're twiddling around so much with pointers/references is that they are an extremly simple, low level and redicoulously powerful primitive. From the hardware POV they are literally just an interger that fits into a register and from a programmers POV you can do anything with just scalars, structs and pointers. The complexity vs usefulness tradeoff for the pointer concept is so rediculously good that everything just goes to shit if you try to get rid of it. You can constrain and auto-manage them if you want(smart pointers, GC, rust's &/&mut types, etc.) but they're still there. There are some execptions for domain specific stuff like prolog or SQL but these don't really do what what you'd do with a "normal" language like C. I don't know how functional languages do references, so maybe someone more "pure" than me can chime in on that.

My son, be not led astray by the worldly fascination with containers on containers. The Lord has brought us raw hardware, rcctl, and jails, and that is all that a true sysadmin needs.
I belive that the reason containers are popular in the first place are that
  • pretty much all id/name allocations(think network ports, file/dir paths, unix socket parths) are descretionary. Ports are not assigend by the OS to the daemon, the daemon tries to claim a port. This is problematic for security, so people put daemons in containers so they can assign ids/names in a way that's not controlled by the daemon itself.
  • software distribution is a mess and people don't want to deal with library version incompabilities between various distros/versions thereof and other distro specific low level nusences, so they pick a base image and package their software as a container and call it a day.
It's perfectly reasonable that most people who run actual services use them AND it's perfectly reasonable that autists who care about systems hate them.
 
Last edited:
If we where the complier we could know that a specific subset of these allocations will remain valid until some time later when all of the subset will be freed at the same time. The compiler could in theroy use a special arena stategy for this specific subset instead of putting it all on the general heap and have malloc deal with the random fragmentation.

I don't actually know if anyone has tried implementing something like this somehow but from my POV i'd say the software stack is pretty much deadlocked. Compilers can't fix this because the compiler doesn't control the malloc impl, they are allowed to optimize out malloc calls in some cases but I don't think they can replace malloc calls with thier own special cased smart malloc.
C compilers are allowed to replace calls to malloc, the same way they're allowed to inline calls to memset, assuming you're not on a freestanding implementation. What you describe generally falls under escape analysis and is a common optimization for many managed languages. Some provide ways to make the compiler's job easier in this regard; Common Lisp has (declare (dynamic-extent ...)) for instance. I don't know if C compilers commonly do it, but if they don't, that's more likely because they expect C programmers to malloc with intention, rather than because it's impossible.
 
Ports are not assigend by the OS to the daemon, the daemon tries to claim a port. This is problematic for security, so people put daemons in containers so they can assign ids/names in a way that's not controlled by the daemon itself.

I'll bite. Why are predictable port numbers a problem?

If it's some service that shouldn't be exposed to the internet, then configuring it so it's only bound to localhost (or making sure it only uses sockets, if that's possible) is the proper solution. "Hiding" it behind a random port number might hide it from lazy skiddies but not from anyone who's really trying.
 
I'll bite. Why are predictable port numbers a problem?

If it's some service that shouldn't be exposed to the internet, then configuring it so it's only bound to localhost (or making sure it only uses sockets, if that's possible) is the proper solution. "Hiding" it behind a random port number might hide it from lazy skiddies but not from anyone who's really trying.
Reading my post again "assigned by the OS" is a exceptional was of phrasing what I meant. I don't mean that ports should be randomly picked by the OS, I mean that the daemons shouldn't have the authority to just claim (available) ports as they please(same with files and other resources), instead they should be given by a higher authority(which would be part of the OS) according to some system configuration. With docker and the like you generally configure that as a port mapping in the Dockerfile and the daemon can't claim any port on its own initative(at least not on the real interface). The security advantage of this is that it brings us closer to the principle of least privilege and makes it harder to misconfigure things.
 
Back
Top Bottom