fast

package module
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2025 License: MIT Imports: 11 Imported by: 13

Documentation

Overview

Borrowed from jsoniter (https://github.com/json-iterator/go)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToPointer

func BytesToPointer[T any](b []byte) *T

func BytesToString

func BytesToString(b []byte) string

func MakeNoZero

func MakeNoZero(l int) []byte

MakeNoZero makes a slice of length and capacity l without zeroing the bytes. It is the caller's responsibility to ensure uninitialized bytes do not leak to the end user.

Example
b := MakeNoZero(16)
fmt.Printf("Length %d, capacity %d", len(b), cap(b))
Output:

Length 16, capacity 16

func MakeNoZeroCap

func MakeNoZeroCap(l int, c int) []byte

MakeNoZero makes a slice of length l and capacity c without zeroing the bytes. It is the caller's responsibility to ensure uninitialized bytes do not leak to the end user.

Example
b := MakeNoZeroCap(0, 16)
fmt.Printf("Length %d, capacity %d", len(b), cap(b))
Output:

Length 0, capacity 16

func Nanotime added in v0.16.0

func Nanotime() int64

Returns a fast, monotonic time suitable for measuring time taken between two calls.

Example
start := Nanotime()
time.Sleep(100 * time.Millisecond)
end := Nanotime()

// This will be ~100 ms (most likely 101 ms)
taken := end - start

fmt.Println(taken / int64(time.Millisecond))
Output:

101

func NanotimeSince added in v0.16.0

func NanotimeSince(ts int64) time.Duration

Returns the duration since a given timestamp acquired from Nanotime.

func Noescape

func Noescape[T any](p T) T

func NoescapeUnsafe added in v0.17.0

func NoescapeUnsafe(p unsafe.Pointer) unsafe.Pointer

noescape hides a pointer from escape analysis. It is the identity function but escape analysis doesn't think the output depends on the input. noescape is inlined and currently compiles down to zero instructions. USE CAREFULLY! This was copied from the runtime; see issues 23382 and 7921.

func PointerToBytes

func PointerToBytes[T any](val *T, length int) []byte

func PointerToBytesOffset

func PointerToBytesOffset[T any](val *T, length, offset int) []byte

func SliceToSlice

func SliceToSlice[From any, To any](from []From, toLength int) []To

func SliceUnsafePointer

func SliceUnsafePointer[T any](slice []T) unsafe.Pointer

func StringToBytes

func StringToBytes(s string) []byte

Types

type AtomicInt32Pair

type AtomicInt32Pair struct {
	// contains filtered or unexported fields
}
Example
var v AtomicInt32Pair

for i := 0; i < 100; i++ {
	v.Add(1, -1)
}

fmt.Println(v.Load())
Output:

100 -100

func (*AtomicInt32Pair) Add

func (x *AtomicInt32Pair) Add(deltaA, deltaB int32) (newA, newB int32)

Add atomically adds delta to x and returns the new value.

func (*AtomicInt32Pair) CompareAndSwap

func (x *AtomicInt32Pair) CompareAndSwap(oldA, oldB, newA, newB int32) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*AtomicInt32Pair) Load

func (x *AtomicInt32Pair) Load() (int32, int32)

Load atomically loads and returns the value stored in x.

func (*AtomicInt32Pair) Store

func (x *AtomicInt32Pair) Store(a, b int32)

Store atomically stores val into x.

func (*AtomicInt32Pair) Swap

func (x *AtomicInt32Pair) Swap(newA, newB int32) (oldA, oldB int32)

Swap atomically stores new into x and returns the previous value.

type BinaryAppender added in v0.11.0

type BinaryAppender interface {
	// AppendBinary appends the binary representation of itself to the end of b
	// (allocating a larger slice if necessary) and returns the updated slice.
	//
	// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
	AppendBinary(b []byte) ([]byte, error)
}

type Clock

type Clock struct {
	// contains filtered or unexported fields
}

func NewClock

func NewClock(ctx context.Context) *Clock

func (*Clock) Now

func (c *Clock) Now() time.Time

func (*Clock) Unix

func (c *Clock) Unix() int64

type JsonAppender added in v0.13.0

type JsonAppender interface {
	// AppendJson appends the JSON representation of itself to the end of b
	// (allocating a larger slice if necessary) and returns the updated slice.
	//
	// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
	AppendJson(b []byte) ([]byte, error)
}

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool[T any](cb ...func(*T)) *Pool[T]

Created a new pool and accepts two (2) optional callbacks. The first is a initializer, and will be called whenever a new item (T) is created. The last is a resetter, and will be called whenever an item is released back to the pool.

func NewStringBufferPool

func NewStringBufferPool() *Pool[StringBuffer]

func (*Pool[T]) Acquire

func (p *Pool[T]) Acquire() *T

Acquires an item from the pool.

func (*Pool[T]) Release

func (p *Pool[T]) Release(v *T)

Releases an item back to the pool. The item cannot be used after release.

type StringBuffer

type StringBuffer struct {
	// contains filtered or unexported fields
}

A StringBuffer is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero StringBuffer.

Example
var b StringBuffer
b.WriteString("hello")
b.WriteByte(' ')
b.WriteInt(123)
fmt.Println(b)

b.Reset()

b.WriteFloat64Lossy(123.4567891)
b.WriteByte(' ')
b.WriteBool(true)
fmt.Println(b)
Output:

hello 123
123.456789 true

func NewStringBuffer

func NewStringBuffer(cap int) *StringBuffer

func (StringBuffer) Bytes

func (b StringBuffer) Bytes() []byte

String returns the accumulated string as bytes.

func (*StringBuffer) Cap

func (b *StringBuffer) Cap() int

Cap returns the capacity of the StringBuffer's underlying byte slice. It is the total space allocated for the string being built and includes any bytes already written.

func (*StringBuffer) Grow

func (b *StringBuffer) Grow(n int)

Grow grows b's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to b without another allocation. If n is negative, Grow panics.

func (*StringBuffer) Len

func (b *StringBuffer) Len() int

Len returns the number of accumulated bytes; b.Len() == len(b.String()).

func (*StringBuffer) Reset

func (b *StringBuffer) Reset()

Reset resets the StringBuffer to be empty.

func (StringBuffer) String

func (b StringBuffer) String() string

String returns the accumulated string.

func (*StringBuffer) Write

func (b *StringBuffer) Write(p []byte) (int, error)

Write appends the contents of p to b's buffer. Write always returns len(p), nil.

func (*StringBuffer) WriteBool

func (b *StringBuffer) WriteBool(v bool)

func (*StringBuffer) WriteByte

func (b *StringBuffer) WriteByte(c byte) error

WriteByte appends the byte c to b's buffer. The returned error is always nil.

func (*StringBuffer) WriteEnc

func (b *StringBuffer) WriteEnc(v StringEncoder)

Write a type that implements StringEncoder

func (*StringBuffer) WriteFloat32

func (b *StringBuffer) WriteFloat32(val float32)

Write float32

func (*StringBuffer) WriteFloat32Lossy

func (b *StringBuffer) WriteFloat32Lossy(val float32)

Write float32 with ONLY 6 digits precision although much much faster

func (*StringBuffer) WriteFloat64

func (b *StringBuffer) WriteFloat64(val float64)

Write float64

func (*StringBuffer) WriteFloat64Lossy

func (b *StringBuffer) WriteFloat64Lossy(val float64)

Write float64 with ONLY 6 digits precision although much much faster

func (*StringBuffer) WriteInt

func (b *StringBuffer) WriteInt(val int)

Write int

func (*StringBuffer) WriteInt16

func (b *StringBuffer) WriteInt16(nval int16)

Write int16

func (*StringBuffer) WriteInt32

func (b *StringBuffer) WriteInt32(nval int32)

Write int32

func (*StringBuffer) WriteInt64

func (b *StringBuffer) WriteInt64(nval int64)

Write int64

func (*StringBuffer) WriteInt8

func (b *StringBuffer) WriteInt8(nval int8)

Write int8

func (*StringBuffer) WriteRune

func (b *StringBuffer) WriteRune(r rune) (int, error)

WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer. It returns the length of r and a nil error.

func (*StringBuffer) WriteString

func (b *StringBuffer) WriteString(s string) (int, error)

WriteString appends the contents of s to b's buffer. It returns the length of s and a nil error.

func (*StringBuffer) WriteUint

func (b *StringBuffer) WriteUint(val uint)

Write uint

func (*StringBuffer) WriteUint16

func (b *StringBuffer) WriteUint16(val uint16)

Write uint16

func (*StringBuffer) WriteUint32

func (b *StringBuffer) WriteUint32(val uint32)

Write uint32

func (*StringBuffer) WriteUint64

func (b *StringBuffer) WriteUint64(val uint64)

Write uint64

func (*StringBuffer) WriteUint8

func (b *StringBuffer) WriteUint8(val uint8)

Write uint8

func (*StringBuffer) WriteVal

func (b *StringBuffer) WriteVal(val any)

type StringEncoder

type StringEncoder interface {
	EncodeString(b *StringBuffer)
}

type StringWriter added in v0.11.0

type StringWriter struct {
	// contains filtered or unexported fields
}

func NewStringWriter added in v0.11.0

func NewStringWriter(w io.Writer, buf ...*StringBuffer) (sw StringWriter)

func (StringWriter) Available added in v0.11.0

func (w StringWriter) Available() int

func (StringWriter) AvailableBuffer added in v0.11.0

func (w StringWriter) AvailableBuffer() []byte

func (StringWriter) Close added in v0.11.0

func (w StringWriter) Close() (err error)

Close implements io.WriteCloser.

func (StringWriter) Flush added in v0.11.0

func (w StringWriter) Flush() (err error)

func (StringWriter) Write added in v0.11.0

func (w StringWriter) Write(p []byte) (n int, err error)

func (StringWriter) WriteBool added in v0.11.0

func (w StringWriter) WriteBool(v bool)

func (StringWriter) WriteByte added in v0.11.0

func (w StringWriter) WriteByte(c byte) error

func (StringWriter) WriteFloat32 added in v0.11.0

func (w StringWriter) WriteFloat32(val float32)

func (StringWriter) WriteFloat32Lossy added in v0.11.0

func (w StringWriter) WriteFloat32Lossy(val float32)

func (StringWriter) WriteFloat64 added in v0.11.0

func (w StringWriter) WriteFloat64(val float64)

func (StringWriter) WriteFloat64Lossy added in v0.11.0

func (w StringWriter) WriteFloat64Lossy(val float64)

func (StringWriter) WriteInt added in v0.11.0

func (w StringWriter) WriteInt(val int)

func (StringWriter) WriteInt16 added in v0.11.0

func (w StringWriter) WriteInt16(val int16)

func (StringWriter) WriteInt32 added in v0.11.0

func (w StringWriter) WriteInt32(val int32)

func (StringWriter) WriteInt64 added in v0.11.0

func (w StringWriter) WriteInt64(val int64)

func (StringWriter) WriteInt8 added in v0.11.0

func (w StringWriter) WriteInt8(val int8)

func (StringWriter) WriteRune added in v0.11.0

func (w StringWriter) WriteRune(r rune) (int, error)

func (StringWriter) WriteString added in v0.11.0

func (w StringWriter) WriteString(s string) (int, error)

func (StringWriter) WriteTo added in v0.11.0

func (w StringWriter) WriteTo(w2 io.Writer) (int64, error)

WriteTo implements io.WriterTo.

func (StringWriter) WriteUint added in v0.11.0

func (w StringWriter) WriteUint(val uint)

func (StringWriter) WriteUint16 added in v0.11.0

func (w StringWriter) WriteUint16(val uint16)

func (StringWriter) WriteUint32 added in v0.11.0

func (w StringWriter) WriteUint32(val uint32)

func (StringWriter) WriteUint64 added in v0.11.0

func (w StringWriter) WriteUint64(val uint64)

func (StringWriter) WriteUint8 added in v0.11.0

func (w StringWriter) WriteUint8(val uint8)

func (StringWriter) WriteVal added in v0.11.0

func (w StringWriter) WriteVal(val any) (err error)

type TextAppender added in v0.11.0

type TextAppender interface {
	// AppendText appends the textual representation of itself to the end of b
	// (allocating a larger slice if necessary) and returns the updated slice.
	//
	// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
	AppendText(b []byte) ([]byte, error)
}

Directories

Path Synopsis
Borrowed from jsoniter (https://github.com/json-iterator/go)
Borrowed from jsoniter (https://github.com/json-iterator/go)

Jump to

Keyboard shortcuts

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