Open VincentRPS opened 1 day ago
On the topic of why Deferred RC specifically, it allows us to keep Metal 100% pauseless even in the case of not being able to infer a lifetime. If Metal detects a cyclic reference it should error and tell the user to use a Weakref or to manually define the lifetime.
Supersedes #61 for memory management.
High-level Conceptual Detail
The Metal compiler will do its best to at most cost infer the lifetime of all types. Inferring and processing lifetimes would be happening around the second or third pass of the AST as it should be after types and statements are parsed.
Inferring lifetimes should go in some direction like this:
var
, var should live as long as function.new_var
which should live as long as A's variable for itnew_var
as a field in the struct,new_var
should live as long as ANow let's try to give a real life example, such as this:
Module
, field should live as long as functionOr maybe a more complex example, this one translating Rust code from the Metal codebase:
This is how the compiler should view this:
content
which should live as long as pushcontent
which should live as long as CowDeferred RCMetal is aiming to cover as much use cases as possible, if not all, by purely inferring lifetimes. And the fallback will instead be manual lifetimes only.
On cases where lifetimes can't be inferred, we can either error and force the user to file and issue/change their code, or fallback to a Deferred RC garbage collector which will handle variables with no inferrable lifetimes.Ideally too we can have manual lifetime definition as to minimize the cases where the Deferred RC collector would have to be used