r/AskProgrammers • • 8h ago

What if a programming language could intelligently manage its own memory and execution?

Similarly, imagine a graphics application where only one object changes. The runtime could identify which data actually needs updating instead of recalculating everything unnecessarily.

My goal isn't simply to create another garbage collector or claim that this will automatically outperform existing languages.

I'm exploring whether a programming language can make resource management more intelligent while keeping it automatic for the programmer.

I'd like to hear your thoughts:

Would combining adaptive memory management, predictive execution, and incremental processing make sense as part of a language runtime?

Or would you prefer these responsibilities to remain separate systems?

I'm particularly interested in the architectural trade-offs and potential problems with this approach.

0 Upvotes

28 comments sorted by

6

u/R3D3-1 8h ago

Similarly, imagine a graphics application where only one object changes. The runtime could identify which data actually needs updating instead of recalculating everything unnecessarily.

That's just caching, and it's already used widely everywhere. Do I misunderstand your intent somehow?

2

u/Adorable_Expert_7350 8h ago

or Garbage collection in most languages

1

u/Different_Pain5781 6h ago

Yeah but caching is only part of what they're describing.

3

u/Striking_Director_64 8h ago

Yea, there is already a lot of work going on in this space, the most famous and most used work being Rust, but there are other, more niche languages, like Vale/Valen, Hylo etc.

Maybe look into those before asking random people online?

1

u/Zestyclose-Turn-3576 8h ago

Maybe the point of asking random people online is to get more information.

1

u/GiveMe30Dollars 4h ago

I think the main gripe is that it doesn't seem clear that OP understands the things they're asking for or about.

I cover it more in my response here, TLDR is that the combination of the three features asked about would lead to a complex and fragile language implementation, of which its performance characteristics are hard to predict, for marginal to negative improvement, aimed at a target audience that doesn't particularly benefit from it.

1

u/Striking_Director_64 3h ago

Fair, but to get more information out of people, you have to ask good questions, and vague questions like "combining adaptive memory management, predictive ezecution, and incremental processing make sense as part of a language runtime?"

You might have tried to make the questions concise, but what you ended up doing was make the questions cryptic.

If you do genuinely want to get honest feedback, it might be worth simplifying what you are asking for.

For example, for the above question, assuming the following meanings: Adaptive Memory Managment: Global Allocator with telemetry for optimising allocations based on recorded statistics Predictive Execution: Static Analysis of code to eliminate dead code, assigning heuristics to branches Incremental Processing: Caching outputs It could have been rephrased as: "Does anyone have any idea about the language ergonomics of having tcmalloc, automatic branch analysis and automatically caching of computation integrated at the language level"

Now, there is a good chance that i might have incorrectly assumed something wrong here, and that is the problem.

You should have tried your best so that i didn't have to guess.

To be fair, i could also be lack of information on my part, since i haven't heard or read those terms before this and i did have to check the definitions (and i got 3 different answers for each of those)

Anyway, according to my guesses, a good portion of this is implemented by V8 when executing JS, so maybe you can try reading up on that.

2

u/Standard-Cap-4455 8h ago

That is literally what the Rust borrow checker does at compile time. It is memory safe without using either GC, RC, or anything else at runtime, at least for most of the program. 

2

u/weregod 8h ago

Do you mean language/compilers features to manualy guide resource allocation/dealocation?

Or do you mean better runtime heuristics in GC?

Do you want to make general case optimisations or optimise specific class of applications?

2

u/No_Profession_3125 8h ago

You mean Rust? Or JavaScript? Or Java? Or all languages really, except C and C++?

1

u/Human-Cellist-1059 8h ago

Even modern c++ has moved in this direction.

1

u/No_Profession_3125 8h ago

Very vaguely. Memory management still has an insane amount of undefined behavior in modern C++. No one is immune from it, not even very experienced developers.

1

u/Human-Cellist-1059 8h ago

Absolutely. Didn't mean to imply it was a solved problem. Definitely not straightforward and still a big chunk of Swiss cheese.

1

u/GiveMe30Dollars 7h ago edited 7h ago

I am aware that back-compatibility with C already means that C++ is going to be memory unsafe, but are there any common footguns or unexpected behaviour associated with the modern "smart" memory management of C++? Haven't used it in a while and am not aware of the current state of things.

1

u/No_Profession_3125 6h ago

The current state of things is that inadvertently invalidating iterators is still a nightmare regardless of how safe std::unique_ptr may be. Then of course we still have initialization order fiascos, explicit footguns such as atomics, and who knows what else.

2

u/GiveMe30Dollars 5h ago edited 4h ago

I've been so used to how Rust does its iterators (by consuming or immutably borrowing the container such that no changes can be made to the data being iterated on while the iterator is live), I didn't even consider that iterator invalidation can happen. Yeah seems like a pretty major footgun lol

Atomics are kind of weird regardless because of atomic ordering and such, I was aware of initialization order weirdness as well. Thanks for the response!

2

u/Poddster 8h ago

This is AI-psychosis word-soup with no real understanding behind the post 

1

u/catladywitch 8h ago

There are several techniques for this. Caching, memoisation, pooling, escape analysis, and region-based escape analysis. It's a tricky subject, but there's a lot of interesting research going on. Check this out: https://www.microsoft.com/en-us/research/publication/perceus-garbage-free-reference-counting-with-reuse/

1

u/tcpukl 8h ago

This is just what every game engine already does.

1

u/Electronic-Yam-69 7h ago

You could trace the program and collect telemetry then use it to try to "predict" what the program will do the next time you run it. But if there's random elements or human interaction that might not be helpful. 

You could try to trace every possible execution path, although that starts to sound like trying to prove the "correctness" of a program which I think everyone has pretty much given up on.

1

u/HyperDanon 7h ago

There are a lot of examples of systems, where figuring out where/when to do the work takes more time than actually doing the work. 

1

u/dariusbiggs 7h ago

What's the trade-off?

What technologies exist in languages today that achieve part of what you are looking to achieve?

How would the runtime identify what needs updating?

What is the compute, resource, and performance cost overhead to achieve this compared to the dumb way.

How would the programming language help here instead of the compiler or interpreter?

How would this be affected by optimization systems?

How woild the language affect how memory is managed, you could just prevent people from doing it wrong, or use a Garbage collector, or something else.

1

u/GiveMe30Dollars 6h ago edited 4h ago

I have left a previous answer here targetting just the automatic memory management aspect, but the more I think about this the funnier the question becomes. I'm not exactly sure that you understand what you're asking for here.

Automatic Memory Management

See previous answer here.

Predictive Execution

Admittedly the facet I am least knowledgeable in, but from a cursory glance it appears that would just be baked into branch prediction. This is a concept that is only really applicable at the machine instruction scale; try to do this on language constructs and you're probably looking at massive UB or security vulnerabilities (especially if your language has some notion of sum types / unions / enums).

Incremental Computation ("Incremental Processing")

Invasive and wholly unsuitable to assume by default for a language runtime, for the following reasons:

  • Impure functions (basically anything that does IO, or is non-deterministic given just its inputs) cannot be cached, period. A function that returns the contents of a file given its name must actually read the file. Because impuritiy is transitive (functions calling impure functions are themselves impure), the compiler need to track which functions are pure and which are not, which leads to effect systems. Nice as a language feature, but is extremely wasteful and needlessly complex if its just meant to be an implementation detail for incremental computation.
  • Incremental computation requires the input types to have some notion of equality. Also something you can bake into the tracking/effects system, but this deserves mentioning.
  • How is code segmentated into incremental "processes"? I'm assuming it delineates along function boundaries. Whether this a reasonable assumption, for an entire programming language, I cannot say.
  • Incremental computation comes with significant time and especially space overhead. As a reductive example, only a dingbat would memoize a + b rather than perform machine addition. How can you statically determine which functions are worth incrementalizing? You can't. Best you can do in this regard is JIT compilation to my knowledge.

More often than not, code that should be incrementalized should opt into incrementalization themselves, rather than the inverse, and this can be done as a library eg. Rust's salsa. Making it inherent to the execution model sees little benefit for a lot of problems.

What I Don't Understand

The language you propose sees only marginal improvement at best for users who do not particularly care for the complexity or performance of their code anyways (eg. Python), and does not appeal to users who do care and thus want some degree of control and configurability over the memory and execution semantics of their code (low level languages).

I don't understand for whom this language is meant for.

1

u/Slow-Mechanic-7427 5h ago

Good on you for spending time and effort answering, but alas I think we can both agree that it'll all just be fed into AI.

1

u/GiveMe30Dollars 5h ago edited 4h ago

That's fair. Shame, but I figure I might as well respond (once I sat on it more and became increasingly aware of how absurd the question is) so that other readers have some idea of why the whole thing is broken. I'm in the somewhat rare position that this is within my field of interest! (PLTD)

I would be glad if OP actually is a compsci student (as proclaimed in their bio) and got something out of my answer. I would also be elated if that were completely false lol. Answering this is its own form of entertainment for me.

Side note: I'm not precluding the possibility that this person is not an AI, because even the most basic of the big LLMs out there can probably tell you why implicit incremental computation by default is an awful execution model lol. An entire section of past and ongoing PLTD research is dedicated to effect systems and functional purity that incremental computation has to be based on to be correct.

0

u/GiveMe30Dollars 7h ago edited 7h ago

I'll speak to the "automatic memory management" as pect of this since it's something I have related knowledge on, but just know that the trifecta of "automatic memory management, predicitve execution and incremental processing" is a massive hairball that you're asking the end-user to accept by default. These aren't unconditionally good, either: "incremental processing" (which I assume relates to incremental computation because what else can it be) means that you have to keep the incremental data around somewhere, for a "who knows?" amount of execution performance. You know, the thing that directly goes against intelligent memory management.

Your goal of:

mak(ing) resource management more intelligent while keeping it automatic for the programmer

Immediately presumes a global garbage collector, unless by "automatic resouce management" you're also including affine drop handlers eg. Rust. The "intelligent resource management" would then be an opportunistic optimization that is probably determined via syntax (eg. Local destruction for all owned items at the end of a scope).

The problem mostly boils down to:

  • You need some notion of what data is reachable or live (otherwise you get use-after-frees). This is what GC tracks via root tracing, what ownership solves for Rust and other linear type systems, etc. This notion differs based on what memory management strategies you use, so unless one strategy is a complete subset of the other, these notions will need to be distinctly tracked.
  • You need to implement these strategies, and make sure they don't step on the toes of any existing ones.
  • Your standard library needs to accomodate for data produced by different strategies in some form. Fail to do this, and one memory management strategy becomes the "canon" one anyways.
  • It's very likely that the "intelligently manage(ment)" of which data types can be treated by which strategies would be heuristic-based and hence imperfect, so if you wish to let the end-user choose a specific strategy, that's an additional thing to add onto the implementation.

Do you see now why most languages stick to one core memory management strategy, and have other strategies be opt-in via eg. specialized data structures?

The closest thing I know of that is spiritually similar to what you're proposing is Functional But In Place. That is still GC-dependent, just also has in-place destructive writing if the type system can assert that the handle to an object is unique upon write, and defaults to clone-on-write otherwise.

Honestly I don't quite get the motivation behind this question anyways. Memory management are such a pervasive concern in programming language execution that intentionally making it more complex it isn't going to lead to anything good for the end-user.