go: proposal: Go 2: Add Automatic Free Block

Author background

  • Would you consider yourself a novice, intermediate, or experienced Go programmer? Experienced Go Programmer
  • What other languages do you have experience with? C.

Related proposals

  • Has this idea, or one like it, been proposed before?
    • If so, how does this proposal differ? I don’t know.
  • Does this affect error handling?
    • If so, how does this differ from previous error handling proposals? no.
  • Is this about generics?
    • If so, how does this relate to the accepted design and other generics proposals? no.

Proposal

  • What is the proposed change?

Add automatic Free blocks to go language.

  • Who does this proposal help, and why? People who require zero GC or do not have STW.
  • Please describe as precisely as possible the change to the language.

The syntax is:

AutoFree N {
//any stmt
}

N is the number of memory bytes that the memory pool is prepared to wait for Alloc.

Semantic refers to the memory allocated on the heap, which will automatically be free after the end of the block.

  • What would change in the language spec? I don’t quite understand how to write the Go language specification for EBNF.
  • Please also describe the change informally, as in a class teaching Go.

By using automatic Free blocks, heap allocation within the block can be automatically released after the block ends.

N is the number of bytes waiting for Alloc to be pre allocated by the memory pool.

When all heap allocations are in the automatic Free block, GOGC=0 can be used to turn off GC to avoid STW The syntax is:

AutoFree N {
// any stmt
}
  • Is this change backward compatible? no.
    • Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit. Show example code before and after the change.

    • Before GC is required.

    • After GC can be turned off because heap allocation can be automatically released.

  • Orthogonality: how does this change interact or overlap with existing features? I don’t know.
  • Is the goal of this change a performance improvement? yes.
    • If so, what quantifiable improvement should we expect? Can eliminate the need for GC, STW disappears.
    • How would we measure it? I don’t know.

Costs

  • Would this change make Go easier or harder to learn, and why? It’s even harder because of the new features.
  • What is the cost of this proposal? (Every language change has a cost). I don’t know.
  • How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected? I don’t know.
  • What is the compile time cost? I don’t know.
  • What is the run time cost? I don’t know.
  • Can you describe a possible implementation?

AutoFree N {is rewritten to create a memory pool,} is rewritten to free memory in the memory pool, and the heap allocation between {and} is allocated in the memory pool.

  • Do you have a prototype? (This is not required.) There is no prototype for GO, there are prototypes in other languages, but the document is not written in English.

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Reactions: 14
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Adding any API that could introduce a silent use-after-free bug is a non-starter.

The arena package is explicitly designed to ensure that it is not silent about use-after-free bugs. And even then, we’re not sure we want to go down that road by promoting it to non-experimental.

I have already read proposal #51317 and it requires manual New and Free.

It requires New and Free of the arena, not of each individual object. New is like your AutoFree N { and Free is like the trailing `}'.

No change in consensus.

Adding any API that could introduce a silent use-after-free bug is a non-starter.

The arena package is explicitly designed to ensure that it is not silent about use-after-free bugs. And even then, we’re not sure we want to go down that road by promoting it to non-experimental.

I’ve thought of an implementation that won’t be silent about use-after-free bugs! Do something with goroutine’s g. Add memPool and localArena fields to g. The AutoFree statement sets the memPool field and prepares N bytes for allocation, and sets the localArena field for heap allocation within the AutoFree block. The child goroutine opened by the go statement in the AutoFree block inherits the memPool field and localArena field of the parent goroutine. Heap allocation within the AutoFree block uses g’s localArena field. If the localArena field requires memory, obtain it from the memPool field. After the AutoFree block ends, the memory held by the memPool field is released. After the AutoFree block ends, if heap allocation from the in-block launched goroutine is found, or if use-after-free bugs are found, the program should react in the same way that arena packages are handled.