mangler

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 5 Imported by: 2

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.

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. Those with missing values panicked during our set of benchmarks, usually a case of not handling nil values elegantly. 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 over 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
BenchmarkMangle-8                         877761              1323 ns/op               0 B/op          0 allocs/op
BenchmarkMangleKnown
BenchmarkMangleKnown-8                   1462954               814.5 ns/op             0 B/op          0 allocs/op
BenchmarkJSON
BenchmarkJSON-8                           199930              5910 ns/op            2698 B/op        119 allocs/op
BenchmarkLoosy
BenchmarkLoosy-8                          307575              3718 ns/op             664 B/op         53 allocs/op
BenchmarkBinary
BenchmarkBinary-8                         413216              2640 ns/op            3824 B/op        116 allocs/op
BenchmarkFmt
BenchmarkFmt-8                            133429              8568 ns/op            3010 B/op        207 allocs/op
BenchmarkFxmackerCbor
BenchmarkFxmackerCbor-8                   258562              4268 ns/op            2118 B/op        134 allocs/op
BenchmarkMitchellhHashStructure
BenchmarkMitchellhHashStructure-8          88941             13049 ns/op           10269 B/op       1096 allocs/op
BenchmarkCnfStructhash
BenchmarkCnfStructhash-8                    5586            179537 ns/op          290373 B/op       5863 allocs/op
PASS
ok      codeberg.org/gruf/go-mangler    12.469s

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 - time.Time{} - url.URL{} - net.IPAddr{} - netip.Addr{}, netip.AddrPort{} - mangler.Mangled{} - fmt.Stringer{} - json.Marshaler{} - encoding.BinaryMarshaler{} - encoding.TextMarshaler{} - all pointers to the above - all slices / arrays of the above - all map keys / values of the above

Types

type Mangled added in v1.1.0

type Mangled interface{ Mangle(buf []byte) []byte }

Mangled is an interface that allows any type to implement a custom Mangler function to improve performance when mangling this type.

type Mangler

type Mangler func(buf []byte, value any) []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