Will var I interface {} = 3 have additional heap memory allocation in go 1.15


First of all, let’s talk about the conclusion: No.

In the latest version of go 1.15, an improvement is mentioned in the runtime section:

Converting a small integer value into an interface value no longer causes allocation.

It means,Converting small integers to interface values no longer requires memory allocation. A small integer is a number between 0 and 255.

So how is this realized?

The specific code is here: Runtime: prevent allocation when converting small ints to interfaces

In general, go defines a special static array, which consists of 256 integers (0 – 256)

When memory must be allocated to store an integer on the heap and convert it to part of the interface, it first checks whether it can only return pointers to the appropriate elements in the array

Go 1.15 release notes: https://golang.org/doc/go1.15

See this article for specific tests: interview question: will var I interface {} = 3 in go 1.15 have additional heap memory allocation?

Welcome to my personal website:

Li peiguan blog: lpgit.com