base14

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: GPL-3.0 Imports: 6 Imported by: 57

README

go-base16384

base16384 interface of golang

Usage

Quick start

package main

import (
	"fmt"

	b14 "github.com/fumiama/go-base16384"
)

func main() {
	str := b14.EncodeString("1234567")
	fmt.Println(str, b14.DecodeString(str))
}

API

func Encode(b []byte) (encd []byte)

func EncodeLen(in int) (out int)

func EncodeTo(b, encd []byte) error

func EncodeToString(b []byte) string

func EncodeFromString(s string) []byte

func EncodeString(s string) string

func DecodeLen(in, offset int) (out int)

func Decode(b []byte) (decd []byte)

func DecodeTo(b []byte, decd []byte) error

func DecodeToString(d []byte) string

func DecodeFromString(s string) []byte

func DecodeString(s string) string

Stream API

package main

import (
	"bytes"
	"crypto/rand"
	"io"

	b14 "github.com/fumiama/go-base16384"
)

func main() {
	buf := make([]byte, 1024*1024+1)
	_, err := rand.Read(buf)
	if err != nil {
		panic(err)
	}
	w := bytes.NewBuffer(make([]byte, 0, 1024*1024+1))
	e := b14.NewEncoder(bytes.NewReader(buf))
	_, err = io.Copy(w, e)
	if err != nil {
		panic(err)
	}
	w2 := bytes.NewBuffer(make([]byte, 0, 1024*1024+1))
	d := b14.NewDecoder(bytes.NewReader(w.Bytes()))
	_, err = io.Copy(w2, d)
	if err != nil {
		panic(err)
	}
	if !bytes.Equal(buf, w2.Bytes()) {
		panic("fail!")
	}
}

Performace Analysis

The performance is optimized by replacing generic encode/decode functions with assembly code.

Encode Speedup by ASM

goos: darwin
goarch: arm64
pkg: github.com/fumiama/go-base16384
cpu: Apple M4 Max
                │   old.txt   │              new.txt               │
                │   sec/op    │   sec/op     vs base               │
EncodeTo/16-16    5.340n ± 1%   5.664n ± 0%   +6.08% (p=0.002 n=6)
EncodeTo/256-16   39.04n ± 1%   34.20n ± 1%  -12.37% (p=0.002 n=6)
EncodeTo/4K-16    537.4n ± 1%   425.6n ± 0%  -20.80% (p=0.002 n=6)
EncodeTo/32K-16   4.228µ ± 1%   3.361µ ± 1%  -20.51% (p=0.002 n=6)
geomean           147.5n        129.0n       -12.54%

                │   old.txt    │               new.txt               │
                │     B/s      │     B/s       vs base               │
EncodeTo/16-16    2.791Gi ± 1%   2.631Gi ± 0%   -5.73% (p=0.002 n=6)
EncodeTo/256-16   6.108Gi ± 1%   6.970Gi ± 1%  +14.12% (p=0.002 n=6)
EncodeTo/4K-16    7.098Gi ± 1%   8.963Gi ± 0%  +26.27% (p=0.002 n=6)
EncodeTo/32K-16   7.218Gi ± 1%   9.079Gi ± 1%  +25.79% (p=0.002 n=6)
geomean           5.436Gi        6.215Gi       +14.33%

Decode Speedup by ASM

goos: darwin
goarch: arm64
pkg: github.com/fumiama/go-base16384
cpu: Apple M4 Max
                │   old.txt   │              new.txt               │
                │   sec/op    │   sec/op     vs base               │
DecodeTo/16-16    5.302n ± 5%   3.525n ± 0%  -33.52% (p=0.002 n=6)
DecodeTo/256-16   46.04n ± 1%   29.91n ± 1%  -35.05% (p=0.002 n=6)
DecodeTo/4K-16    585.6n ± 1%   405.8n ± 0%  -30.70% (p=0.002 n=6)
DecodeTo/32K-16   4.567µ ± 0%   3.197µ ± 0%  -30.00% (p=0.002 n=6)
geomean           159.8n        108.1n       -32.35%

                │   old.txt    │               new.txt                │
                │     B/s      │      B/s       vs base               │
DecodeTo/16-16    3.864Gi ± 5%    5.812Gi ± 1%  +50.40% (p=0.002 n=6)
DecodeTo/256-16   5.987Gi ± 1%    9.219Gi ± 1%  +53.99% (p=0.002 n=6)
DecodeTo/4K-16    7.450Gi ± 1%   10.749Gi ± 0%  +44.29% (p=0.002 n=6)
DecodeTo/32K-16   7.638Gi ± 0%   10.911Gi ± 0%  +42.84% (p=0.002 n=6)
geomean           6.024Gi         8.903Gi       +47.81%

Documentation

Overview

Package base14 base16384 的 go 接口

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString added in v1.3.0

func BytesToString(b []byte) string

BytesToString 没有内存开销的转换

func Decode

func Decode(b []byte) (decd []byte)

func DecodeFromString added in v1.5.0

func DecodeFromString(s string) []byte

func DecodeLen added in v1.4.0

func DecodeLen(in, offset int) (out int)

func DecodeString added in v1.2.0

func DecodeString(s string) string

func DecodeTo added in v1.4.0

func DecodeTo(b []byte, decd []byte) (int, error)

func DecodeToString added in v1.4.0

func DecodeToString(d []byte) string

func Encode

func Encode(b []byte) (encd []byte)

func EncodeFromString added in v1.4.0

func EncodeFromString(s string) []byte

func EncodeLen added in v1.4.0

func EncodeLen(in int) (out int)

func EncodeString added in v1.2.0

func EncodeString(s string) string

func EncodeTo added in v1.4.0

func EncodeTo(b, encd []byte) (int, error)

func EncodeToString added in v1.5.0

func EncodeToString(b []byte) string

func StringToBytes added in v1.3.0

func StringToBytes(s string) (b []byte)

StringToBytes 没有内存开销的转换

func UTF16BE2UTF8 added in v1.4.0

func UTF16BE2UTF8(b []byte) ([]byte, error)

UTF16BE2UTF8 to display the result as string

func UTF82UTF16BE added in v1.4.0

func UTF82UTF16BE(b []byte) ([]byte, error)

UTF82UTF16BE to decode from string

Types

type Decoder added in v1.4.0

type Decoder struct {
	io.Reader
	// contains filtered or unexported fields
}

func NewDecoder added in v1.4.0

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Read added in v1.4.0

func (d *Decoder) Read(p []byte) (n int, err error)

type Encoder added in v1.4.0

type Encoder struct {
	io.WriteCloser
	io.ReaderFrom
	// contains filtered or unexported fields
}

func NewEncoder added in v1.4.0

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Close added in v1.7.1

func (e *Encoder) Close() error

func (*Encoder) ReadFrom added in v1.7.1

func (e *Encoder) ReadFrom(r io.Reader) (int64, error)

func (*Encoder) Write added in v1.7.1

func (e *Encoder) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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