GuessSize guesses the size of the underlying value of 'i'. If the underlying value is not a simple builtin (or []byte), GuessSize defaults to 512.
(i any)
| 853 | // a simple builtin (or []byte), GuessSize defaults |
| 854 | // to 512. |
| 855 | func GuessSize(i any) int { |
| 856 | if i == nil { |
| 857 | return NilSize |
| 858 | } |
| 859 | |
| 860 | switch i := i.(type) { |
| 861 | case Sizer: |
| 862 | return i.Msgsize() |
| 863 | case Extension: |
| 864 | return ExtensionPrefixSize + i.Len() |
| 865 | case float64: |
| 866 | return Float64Size |
| 867 | case float32: |
| 868 | return Float32Size |
| 869 | case uint8, uint16, uint32, uint64, uint: |
| 870 | return UintSize |
| 871 | case int8, int16, int32, int64, int: |
| 872 | return IntSize |
| 873 | case []byte: |
| 874 | return BytesPrefixSize + len(i) |
| 875 | case string: |
| 876 | return StringPrefixSize + len(i) |
| 877 | case complex64: |
| 878 | return Complex64Size |
| 879 | case complex128: |
| 880 | return Complex128Size |
| 881 | case bool: |
| 882 | return BoolSize |
| 883 | case map[string]any: |
| 884 | s := MapHeaderSize |
| 885 | for key, val := range i { |
| 886 | s += StringPrefixSize + len(key) + GuessSize(val) |
| 887 | } |
| 888 | return s |
| 889 | case map[string]string: |
| 890 | s := MapHeaderSize |
| 891 | for key, val := range i { |
| 892 | s += 2*StringPrefixSize + len(key) + len(val) |
| 893 | } |
| 894 | return s |
| 895 | default: |
| 896 | return 512 |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // Temporary buffer for reading/writing binary data. |
| 901 | var bytesPool = sync.Pool{New: func() any { return make([]byte, 0, 1024) }} |