mangler

package module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 3 Imported by: 1

README

go-mangler

Documentation.

To put it simply is a bit of an odd library. It aims to provide incredibly fast, unique string outputs for all default supported input data types during a given runtime instance. See mangler.String()for supported types.

It is useful, for example, for use as part of larger abstractions involving hashmaps. That was my particular usecase anyways...

This package does make liberal use of the "unsafe" package.

Benchmarks are below. Please note the more important thing to notice here is the relative difference in benchmark scores, the actual ns/op,B/op,allocs/op accounts for running through ~80 possible test cases, including some not-ideal situations.

The choice of libraries in the benchmark are just a selection of libraries that could be used in a similar manner to this one, i.e. serializing in some manner.

go test -run=none -benchmem -gcflags=all='-l=4' -bench=.*                            
goos: linux
goarch: amd64
pkg: codeberg.org/gruf/go-mangler
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
BenchmarkMangle-8                        1278526               966.0 ns/op             0 B/op          0 allocs/op
BenchmarkMangleKnown-8                   3443587               345.9 ns/op             0 B/op          0 allocs/op
BenchmarkJSON-8                           228962              4717 ns/op            1849 B/op         99 allocs/op
BenchmarkLoosy-8                          307194              3447 ns/op             776 B/op         65 allocs/op
BenchmarkFmt-8                            150254              7405 ns/op            1377 B/op        143 allocs/op
BenchmarkFxmackerCbor-8                   364411              3037 ns/op            1224 B/op        105 allocs/op
BenchmarkMitchellhHashStructure-8         102272             11268 ns/op            8996 B/op       1000 allocs/op
BenchmarkCnfStructhash-8                    6789            168703 ns/op          288301 B/op       5779 allocs/op
PASS
ok      codeberg.org/gruf/go-mangler    11.715s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(b []byte, a any) []byte

Append will append the mangled form of input value 'a' to buffer 'b'. See mangler.String() for more information on mangled output.

func Register

func Register(t reflect.Type, m Mangler)

Register will register the given Mangler function for use with vars of given runtime type. This allows registering performant manglers for existing types not implementing Mangled (e.g. std library types). NOTE: panics if there already exists a Mangler function for given type. Register on init().

func String

func String(a any) string

String will return the mangled format of input value 'a'. This mangled output will be unique for all default supported input types during a single runtime instance. Uniqueness cannot be guaranteed between separate runtime instances (whether running concurrently, or the same application running at different times).

The exact formatting of the output data should not be relied upon, only that it is unique given the above constraints. Generally though, the mangled output is the binary formatted text of given input data.

Uniqueness is guaranteed for similar input data of differing types (e.g. string("hello world") vs. []byte("hello world")) by prefixing mangled output with the input data's runtime type pointer.

Default supported types include: - string - bool - int,int8,int16,int32,int64 - uint,uint8,uint16,uint32,uint64,uintptr - float32,float64 - complex64,complex128 - arbitrary structs - all type aliases of above - all pointers to the above - all slices / arrays of the above

Types

type Mangler

type Mangler func(buf []byte, ptr unsafe.Pointer) []byte

Mangler is a function that will take an input interface value of known type, and append it in mangled serialized form to the given byte buffer. While the value type is an interface, the Mangler functions are accessed by the value's runtime type pointer, allowing the input value type to be known.

func Get

func Get(t reflect.Type) Mangler

Get will fetch the Mangler function for given runtime type. Note that the returned mangler will be a no-op in the case that an incorrect type is passed as the value argument.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL