Documentation ¶
Overview ¶
Package defract is a reflect-like package that utilizes heavy caching with unsafe to improve its performance.
Index ¶
- Variables
- func AllocIndirect(typ reflect.Type, ptr unsafe.Pointer) (reflect.Type, unsafe.Pointer)
- func AllocSlice(ptr unsafe.Pointer, size, len int64)
- func BytesToStr(bytes []byte) string
- func CopyString(dst unsafe.Pointer, src []byte)
- func GetStructSchema(v interface{}) string
- func Indirect(typ reflect.Type, ptr unsafe.Pointer) (reflect.Type, unsafe.Pointer)
- func IndirectOnce(ptr unsafe.Pointer) unsafe.Pointer
- func IntLE(i *int) []byte
- func InterfacePtr(v interface{}) unsafe.Pointer
- func IsZero(ptr unsafe.Pointer, size uintptr) bool
- func IsZeroBytes(bytes []byte) bool
- func NumberLE(kind reflect.Kind, ptr unsafe.Pointer) []byte
- func ReadInt64LE(dst []byte) (int64, bool)
- func ReadNumberLE(b []byte, kind reflect.Kind, ptr unsafe.Pointer) bool
- func SliceInfo(ptr unsafe.Pointer) (unsafe.Pointer, int, int)
- func SliceSetLen(ptr unsafe.Pointer, len int64) unsafe.Pointer
- func StrToBytes(str *string) []byte
- func StringInfo(ptr unsafe.Pointer) (unsafe.Pointer, int)
- func UintLE(u *uint) []byte
- func UnderlyingPtr(v interface{}) (reflect.Type, unsafe.Pointer)
- func WithinBytes(outer, inner []byte) bool
- func WriteInt64LE(dst []byte, i int64)
- func ZeroOut(ptr unsafe.Pointer, size uintptr)
- func ZeroOutBytes(bytes []byte)
- type SliceHeader
- type StringHeader
- type StructField
- type StructInfo
Constants ¶
This section is empty.
Variables ¶
var ByteSlice = reflect.TypeOf([]byte(nil))
ByteSlice is the reflect.Type value for a byte slice.
var IsLittleEndian bool
IsLittleEndian is true if the current machine is a Little-Endian machine.
Functions ¶
func AllocIndirect ¶
AllocIndirect allocates a pointer type until the value is reached, and the pointer to that newly-allocated value is returned, along with the underlying type. If the given ptr is not nil, then the memory will be reused.
func AllocSlice ¶
AllocSlice allocates a slice that is len*typsize bytes large. The returned pointer is the data pointer.
func BytesToStr ¶
BytesToStr converts the given bytes to string without copying.
func CopyString ¶
CopyString tries to reduce allocations by reusing the backing array if there's enough length.
func GetStructSchema ¶
func GetStructSchema(v interface{}) string
GetStructSchema gets the struct schema of the given struct value's type. If v isn't a struct, then it panics.
func Indirect ¶
Indirect dereferences the pointer until the type is no longer one. If any of the pointers are nil, then zero-values are returned.
func IndirectOnce ¶
IndirectOnce dereferences ptr once. It returns nil if ptr is nil.
func IntLE ¶
IntLE is a helper function that converts the given int value into bytes, ideally without copying on a 64-bit little-endian machine. The bytes are always in little-endian.
func InterfacePtr ¶
InterfacePtr returns the pointer to the internal value of the given interface.
func IsZero ¶
IsZero returns true if the data at the given pointer is all zero. The function scans the data up to the given length.
func IsZeroBytes ¶
IsZeroBytes is the bytes equivalent of the IsZero function.
func NumberLE ¶
NumberLE returns the little-endian variant of the given number at the pointer. It does not handle variable-length integers, and is meant only for other statically sized integer or floating-point types.
If this method is called on a Big Endian macnine, then a new byte buffer will be allocated from the pool.
func ReadInt64LE ¶
ReadInt64LE reads dst and returns an int64 if dst has enough data. Otherwise, false is returned.
func ReadNumberLE ¶
ReadNumberLE reads the little-endian number of the given byte slice into the pointer. If bound checking fails, false is returned.
func SliceInfo ¶
SliceInfo returns the backing array pointer and length of the slice at the given pointer.
func SliceSetLen ¶
SliceSetLen sets the length of the slice at the given pointer and returns the pointer to the backing array.
func StrToBytes ¶
StrToBytes converts the given string to bytes without copying.
func StringInfo ¶
StringInfo returns the backing array pointer and length of the string at the given pointer. It also works with byte slices.
func UintLE ¶
UintLE is a helper function that converts the given uint64 value into bytes, ideally without copying on a 64-bit little-endian machine. The bytes are always in little-endian.
func UnderlyingPtr ¶
UnderlyingPtr returns the type of and the pointer to the value of the interface by dereferencing it until the actual value is reached.
func WithinBytes ¶
WithinBytes returns true if the inner slice is within outer slice.
func WriteInt64LE ¶
WriteInt64LE writes the given int64 into the given byte slice.
func ZeroOutBytes ¶
func ZeroOutBytes(bytes []byte)
ZeroOutBytes fills the given bytes slice with zeroes.
Types ¶
type SliceHeader ¶
SliceHeader is the header structure that contains an unsafe.Pointer data field instead of uintptr.
type StringHeader ¶
StringHeader is a safer reflect.StringHeader.
type StructField ¶
type StructInfo ¶
type StructInfo struct { Type reflect.Type Fields []StructField // RawSchema describes the schema of the struct. The description currently // includes field names, though this may change in the future. RawSchema []byte }
func GetStructInfo ¶
func GetStructInfo(typ reflect.Type) *StructInfo
GetStructInfo returns the struct type information for the given struct value. It assumes that typ is a type of a struct and does not do checks.