On Thu, Mar 18, 2021 at 8:36 PM Paul Zhang <
tianx...@gmail.com> wrote:
>
> Can I just understand that for the default case, when the compiler executes the mallocgc() for c.buf, it would use reflect to build the descriptor for the type of element? And if it allocates the both spaces in one function call, it wouldn't build the descriptor for the gc, and gc would find the descriptor later with more time, thus lead to worse performance? Thanks a lot!
As Jan said, please post code as ordinary text or as a link to the Go
playground or the Go sources. The colorized text on black is very
difficult to read. Thanks.
That said, I'm sorry, I don't really understand what you are asking.
When the code you showed calls mallocgc, it passes the type descriptor
for the channel element type. This is called "elem" in the code.
This type descriptor was created by the compiler.
If the runtime code allocated both the channel data structure and the
buffer in a single memory allocation, it would need to have a type
descriptor that combined the channel data structure with the element
type. Not only that, this new type descriptor would change based on
the argument passed to make. In the existing code, that is not
necessary; when mallocgc is passed a type descriptor to allocate a
size that is larger than the type, it understands that it is
allocating an array. That wouldn't work for an allocation that shares
the channel data structure with the channel buffer.
I don't know what you mean when you say "gc would find the descriptor
later with more time." The compiler would have to create the
descriptor at compile time, so that the runtime code could use that
type descriptor to allocate the memory. The gc can't find the
descriptor later.
Ian