| 1 |
package json |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
|
| 6 |
"github.com/goccy/go-json/internal/encoder" |
| 7 |
"github.com/goccy/go-json/internal/runtime" |
| 8 |
) |
| 9 |
|
| 10 |
// MarshalOf returns the JSON encoding of v, as Marshal does. |
| 11 |
// |
| 12 |
// Marshal takes its argument as an interface value, so a value which is not a pointer is copied to the heap |
| 13 |
// for every call. MarshalOf takes the value by its type, and it copies the value to a value in the heap which is |
| 14 |
// reused, so it encodes a value without an allocation other than the one of the result. |
| 15 |
// |
| 16 |
// Which one to use depends on what is passed: |
| 17 |
// - A value which is not a pointer ( a struct, an int, a string, ... ): MarshalOf(v) is faster than Marshal(v), |
| 18 |
// because it saves the allocation. It also lets v stay on the stack of the caller, which Marshal(&v) doesn't. |
| 19 |
// - A pointer or a map: they are the same, because such a value is stored in an interface value without |
| 20 |
// an allocation. |
| 21 |
// - A large value which is already referred to by a pointer p: Marshal(p) is the fastest. MarshalOf(*p) copies |
| 22 |
// the whole value, which costs more as the value gets larger. |
| 23 |
func MarshalOf[T any](v T, optFuncs ...EncodeOptionFunc) ([]byte, error) { |
| 24 |
ctx := encoder.TakeRuntimeContext() |
| 25 |
|
| 26 |
ctx.Option.Flag = 0 |
| 27 |
ctx.Option.Flag |= (encoder.HTMLEscapeOption | encoder.NormalizeUTF8Option) |
| 28 |
for _, optFunc := range optFuncs { |
| 29 |
optFunc(ctx.Option) |
| 30 |
} |
| 31 |
|
| 32 |
buf, err := encodeOf(ctx, &v) |
| 33 |
if err != nil { |
| 34 |
encoder.ReleaseRuntimeContext(ctx) |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
|
| 38 |
// see marshal for the reason why the result is copied this way. |
| 39 |
buf = buf[:len(buf)-1] |
| 40 |
copied := make([]byte, len(buf)) |
| 41 |
copy(copied, buf) |
| 42 |
|
| 43 |
encoder.ReleaseRuntimeContext(ctx) |
| 44 |
return copied, nil |
| 45 |
} |
| 46 |
|
| 47 |
// maxReusedValueSize is the size of the largest value which MarshalOf copies to a value it reuses. |
| 48 |
// Every runtime context in the pool keeps such a value, so a large one is left to the GC. |
| 49 |
const maxReusedValueSize = 4096 |
| 50 |
|
| 51 |
func encodeOf[T any](ctx *encoder.RuntimeContext, v *T) ([]byte, error) { |
| 52 |
typ := reflect.TypeOf(v).Elem() |
| 53 |
switch typ.Kind() { |
| 54 |
case reflect.Interface: |
| 55 |
// the type to encode is the one of the value which the interface value holds. |
| 56 |
return encode(ctx, *v) |
| 57 |
case reflect.Ptr, reflect.Map: |
| 58 |
// the value is stored directly in an interface value, which needs no allocation. |
| 59 |
// The other types stored directly are found by their code set below. |
| 60 |
return encode(ctx, *v) |
| 61 |
} |
| 62 |
codeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(runtime.TypePtr(typ))) |
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
if !codeSet.IfaceIndir { |
| 67 |
// the value is stored directly in an interface value, which needs no allocation. |
| 68 |
return encode(ctx, *v) |
| 69 |
} |
| 70 |
if typ.Size() > maxReusedValueSize { |
| 71 |
return encode(ctx, *v) |
| 72 |
} |
| 73 |
|
| 74 |
p := codeSet.TakeValue(ctx) |
| 75 |
*(*T)(p) = *v |
| 76 |
ctx.Init(p, codeSet.CodeLength) |
| 77 |
buf, err := encodeRunCode(ctx, ctx.Buf[:0], codeSet) |
| 78 |
// the pool must not keep what the value refers to alive. |
| 79 |
var zero T |
| 80 |
*(*T)(p) = zero |
| 81 |
if err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
ctx.Buf = buf |
| 85 |
return buf, nil |
| 86 |
} |