How autofree works #17419
Replies: 9 comments 10 replies
-
(I do not know whether I can, but I have small note) |
Beta Was this translation helpful? Give feedback.
-
Strongly believe that this kind of post and upcoming article is important to help neutralize some of the confusion and deceptive criticisms placed out there. Among the things that are done: 1) Critics who like pretending they don't know anything about V's memory management options. 2) Pretending not to understand what the words "option" or "optional" means, or that V's GC can be turned off. 3) Ridiculous smears of over any GC usage, as if it means to somehow be "inferior". That V uses an optional GC by default is tremendously helpful and convenient to new and casual programmers. Not only that, (in addition to other safety features that V has), it provides a degree of memory safety which has repeatedly been recommended by various government organizations. Consequently, people can get started being productive with V much more rapidly, because they don't have to worry about manual memory management. They can more rapidly focus on getting results and solutions. When ready, the GC can be turned off, because they are actually dealing with a situation where doing so would be useful or are really that advanced of a programmer. As V is a compiled programming language, whose optional GC can be turned off, means it can go about memory management in a number of ways that is highly competitive to any that exists. Thus arena allocators. Optional GC and arena allocator usage, can be more than enough to offset excessive fixation on autofree until it becomes more developed and advanced. These other options should definitely get more attention in the documentation. V is definitely more than just being about autofree (which is a great feature and option), it's a good and useful programming language. Autofree is more like a "cherry on top". The addition of autofree, with the other memory management options, strengthens the already existing benefits of using V. |
Beta Was this translation helpful? Give feedback.
-
I m sorry if I tell somethig what sound like that I criticise V have GC, GC is amazing tool when person do not need optimalization ..., and I realy llove V s options and their combinations, really amazing job! |
Beta Was this translation helpful? Give feedback.
-
Looking at this. I think this should also be put in V's official Blog (https://blog.vlang.io/), when ready. As it would have a better "aesthetic" and be more "linkable", in addition to be a place where new users can update themselves on things being emphasized for them to know. |
Beta Was this translation helpful? Give feedback.
-
The article seems to lack an explanation of why autofree leads to worse performance compared to enabling GC? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Does not used anywhere else mean it isn't returned or its address taken? What about if it is assigned to something, is an implicit clone inserted for heap structs? (2) seems to be missing the local string allocation's call to fn foo() {
s1 := 'hello'
s2 := 'world'
s := s1 + ' ' + s2
str_copy := s.clone() // inserted by autofree
do_stuff(str_copy)
s.free() // inserted by autofree?
}
Was there any update on this? Presumably at least when a reference to heap memory is reassigned to something else, the original memory is also freed if unused? |
Beta Was this translation helpful? Give feedback.
-
Autofree has been waiting too long, do I have to wait for another year or even longer |
Beta Was this translation helpful? Give feedback.
-
Imho, I think that |
Beta Was this translation helpful? Give feedback.
-
Due to many requests, I'll be writing down the principles of how autofree works. It will give a more transparent view into one of V's memory management models and will make it easier to contribute to the autofree engine, which is still work in progress.
Autofree is one of 4 ways to handle memory in V. The default one is a tracing GC, there's arena allocation with
-prealloc
, and manual memory management via-gc none
.There's a demo video available. Building the Ved editor with autofree and running it on an 8MB text file with 0 leaks:
https://www.youtube.com/watch?v=gmB8ea8uLsM
When I started working on V, I was very anti-GC, expecting them to be slow and use a lot more RAM. I integrated a GC just for a test, and was surprised at how well it worked with V, a very minimal language, that avoids doing unnecessary allocations in the first place by using value types, string buffers, promoting a simple abstraction-free code style. V's standard library doesn't use the GC, so turning it off for you application is not a problem (no need to use a different version of stdlib etc).
What started as a test and a temporary way to allow developers to write leak free programs, became the stable and well working default option. Autofree in fact performed a bit worse than the default GC. The reasons will be explained in this article.
Other things in the compiler were prioritized, that's why autofree still isn't finished yet. But it is important, and it will be finished. Very often using a GC is simply not an option. For examples in drivers and kernels. In fact, our Vinix kernel uses autofree.
Our goal is to make V very flexible and configurable, that's why we provide multiple ways to manage memory.
As the home page explains, autofree frees the variables that the compiler knows it can safely free. For all the remaining cases, the GC is used with an option to use manual memory management (in the future the compiler will point out the variables it can't free if
-gc none
is used).The amount of such variables can vary depending on the code. It should be about 10% on average. In the mentioned Ved demo, 0% of variables are unhandled by autofree.
Let's go through what autofree can handle and what it does.
1. Simple out of scope variables.
If a variable results in an allocation, and this allocation is not used anywhere else, the variable is freed at the end of the block it was created in (i.e. the variable goes out of scope):
In this example I didn't use
s := 'hello ' + 'world'
, because V's optimizer will simplify it tos := 'hello world'
, and simple string literals don't result in an allocation.2. Allocated strings are cloned on assignments.
To ensure that string copies do not point to freed memory, they are always cloned. This is an expensive O(n) operation, and is the main reason autofree can be slower than V's default GC or manual management. It can be optimized by doing copy on write and more analysis by the compiler.
This results in:
This is only about strings, not arrays. Note that V forces cloning arrays on assignment, even without autofree enabled:
more to come soon
Beta Was this translation helpful? Give feedback.
All reactions