4 ms·
Hi. How is cyclic references handled? One of the maintainers said it would be removed and made not possible. Also garbage collection. What are you doing for th
by himujjal 6y ago
Hi. How is cyclic references handled? One of the maintainers said it would be removed and made not possible.
Also garbage collection. What are you doing for that? ARC?
- amedvednikov 6y agoFrom the home page: V avoids doing unnecessary allocations in the first place by using value types, string buffers, promoting a simple abstraction-free code style. Most objects (~90-100%) are freed by V's autofree engine: the compiler inserts necessary free calls automatically during compilation. Remaining small percentage of objects is freed via reference counting. The developer doesn't need to change anything in their code. "It just works", like in Python, Go, or Java, except there's no heavy GC tracing everything or expensive RC for each object. For developers willing to have more low level control, autofree can be disabled with -noautofree. Note: right now autofree is hidden behind the -autofree flag. It will be enabled by default in V 0.3. --- So yes, for the ~0-10% of objects not freed by V's autofree, ARC will be used. We will implement weak references and probably have a simple cycle detector, so pretty much a minimal GC. Unlike in Go or Swift, it won't be used on all objects, but only on the small percentage that couldn't be handled by autofree. In the YouTube demo I posted, 100% of all allocations in the text editor is handled by autofree.
- YorickPeterse 6y agoHow exactly does V know when/where to release memory?
- amedvednikov 6y agoSee my reply to the sibling comment.
- dgb23 6y agoThis sounds interesting. I would love to understand how autofree decides what it can handle, and when ARC comes in.
- amedvednikov 6y agoCheck this test out: https://github.com/vlang/v/blob/master/vlib/v/tests/valgrind/1.strings_and_arrays.v https://github.com/vlang/v/blob/master/vlib/v/tests/valgrind... It has lots of comments and will give you a good picture.
- vlang1dot0 6y agoI think having actual documentation about how the memory system is supposed to work would do a lot more to answer questions than linking to the tests. For example: x = [1, 2, 3] // is this stack or heap allocated? foo(&x) // how does the compiler know if x needs to be ref counted or not? return &x // is this valid or UB or a compilation error?
- CodeHz 6y agoIt looks similar to nim's ARC/ORC gc approach... Just curious, have you ever compared them?
- slavapestov 6y ago> Unlike in Go or Swift, it won't be used on all objects, but only on the small percentage that couldn't be handled by autofree. Swift also has value types, and optimization passes to promote heap-allocated objects and closures to the stack where possible using escape analysis.
- azakai 6y agoSimilar in Go as well: Many things are by value, and it does escape analysis too, allowing a lot of things to not require the GC.
- saagarjha 6y agoHow does V deal with cycles?
- Too 6y agoSorry but again this is one of those "V can do X (except when it doesn't and i get to define what that means)". I don't want to be a hater and I'm genuinely impressed by the project. But it's honestly very hard to trust anything about this when all kinds of claims pop up, always with a big caveat at the end, usually after being questioned in forums and not in the official documentation. What happens today if i don't pass in -autofree? If it was working it would be enabled right? And if it's not working, then how do i code without it? Sure, the language has a 0.x-version number but it's not exactly being advertised as something that is incomplete unless i dig very deep into documentation or twitter-threads. Some kind of roadmap of promised/planned/delivered features would greatly improve the confidence in this being not vaporware.
- gus_massa 6y ago> Remaining small percentage of objects is freed via reference counting. That should free most objects in a simple scenario, but how do you free objects in a cycle without a garbage collector?