Automated cleanup tied to scope, like destructors (__deinit?) or defer #6549
juliusikkala
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
My thinking is that we will need all three of them in the language. Slang
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Lately, I've been using Slang on CPU quite a bit, both for fun (working on an SDL2 + Vulkan minigame with Slang on both CPU & GPU) and academic reasons (fitting various curves, using auto-diff). Having amassed quite a bit of CPU-side code, probably the least "fun" thing about Slang in that context has been the cleanup of resources.
This is pretty much a non-issue on the GPU side, since rarely if ever do shaders care about resource (de)allocation, but on the CPU side, it's unavoidable in non-trivial programs. The best I can come up with current Slang is similar to C but without even the
goto fail;
pattern typical in C codebases. This can lead to lots of error-prone repetition, e.g. something like the following:Because there are no destructors, resources have to be freed manually at all the places where the function may exit. It's also a bit asymmetric since constructors exist and can execute non-trivial code (allocate resources). Here's a couple of ideas I could come up with to help this situation:
OOP-style destructors:
__deinit
would match nicely with__init
. In practice, this would also require either the existence of user-overridable copy construction & assignment operator or banning copying for types which implement__deinit
. Without those, a naively copied structure would quickly lead to double freeing as both copies free the same resource in__deinit
. I'm not super familiar with auto-diff for custom types, but if I were to make thisList<T>
differentiable, a__deinit
would also allow the auto-diff'd code to release the memory of the differentiated type, right?defer
: Although it's a bit more verbose and somewhat more error-prone (and I personally dislike it), something like thedefer
keyword from Go, Zig, Odin, etc. could also be a great quality-of-life improvement for this purpose, without necessarily needing the copy/assignment stuff. It's also a very non-invasive feature, which is nice too for those who don't want to use it or if another destruction method is implemented later on.defer
can also allow passing parameters to the "destructor", which in my case would actually be useful for wrapped Vulkan resources which need aVkDevice
handle for destruction. I can also imagine some use cases fordefer
in compute shaders, e.g. if there's a "mutex" implemented with atomics, one coulddefer
the unlock to the end of the scope. Not sure ifdefer
would help in the auto-diff case, though.GC or reference counting: This is probably not going to fly considering the target audience, but just listing it here for completeness 😉
Beta Was this translation helpful? Give feedback.
All reactions