package run

import (
	

	
	
)

// 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.
const MaxReusedValueSize = 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.
		return Encode(, *)
	}
	 := reflect.TypeOf().Elem()
	switch .Kind() {
	case reflect.Interface:
		// the type to encode is the one of the value which the interface value holds.
		return Encode(, *)
	case reflect.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.
		return Encode(, *)
	}
	,  := encoder.CompileToGetCodeSet(, uintptr(runtime.TypePtr()))
	if  != nil {
		return nil, 
	}
	if !.IfaceIndir {
		// the value is stored directly in an interface value, which needs no allocation.
		return Encode(, *)
	}
	if .Size() > MaxReusedValueSize {
		return Encode(, *)
	}

	 := .TakeValue()
	*(*)() = *
	.Init(, .CodeLength)
	,  := Code(, .Buf[:0], )
	// the pool must not keep what the value refers to alive.
	var  
	*(*)() = 
	if  != nil {
		return nil, 
	}
	.Buf = 
	return , nil
}