package runimport ()// MaxReusedValueSize is the size of the largest value which EncodeOf, and the decoding of a value by its type,// copy to a value they reuse. Every runtime context in the pool keeps such a value, so a large one is left to the// GC.constMaxReusedValueSize = 4096// EncodeOf encodes the value at v, as Encode encodes it, without the copy of the value to the heap which an// interface value of it would be: it is copied to a value in the heap which is reused ( see// encoder.OpcodeSet.TakeValue ). A value which an interface value holds directly is encoded by Encode.func [ any]( *encoder.RuntimeContext, *) ([]byte, error) {if .Option.Flag&encoder.MarshalFuncsOption != 0 {// the code sets of the functions of a call, whose pool of values the runtime keeps for a GC after it is used, // would keep the functions alive.returnEncode(, *) } := reflect.TypeOf().Elem()switch .Kind() {casereflect.Interface:// the type to encode is the one of the value which the interface value holds.returnEncode(, *)casereflect.Ptr, reflect.Map:// the value is stored directly in an interface value, which needs no allocation. // The other types stored directly are found by their code set below.returnEncode(, *) } , := encoder.CompileToGetCodeSet(, uintptr(runtime.TypePtr()))if != nil {returnnil, }if !.IfaceIndir {// the value is stored directly in an interface value, which needs no allocation.returnEncode(, *) }if .Size() > MaxReusedValueSize {returnEncode(, *) } := .TakeValue() *(*)() = * .Init(, .CodeLength) , := Code(, .Buf[:0], )// the pool must not keep what the value refers to alive.var *(*)() = if != nil {returnnil, } .Buf = return , nil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.