Also garbage collection. What are you doing for that? ARC?
Also garbage collection. What are you doing for that? ARC?
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.
https://github.com/vlang/v/blob/master/vlib/v/tests/valgrind...
It has lots of comments and will give you a good picture.
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?
Swift also has value types, and optimization passes to promote heap-allocated objects and closures to the stack where possible using escape analysis.
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.
That should free most objects in a simple scenario, but how do you free objects in a cycle without a garbage collector?