czlib

package module
v0.0.0-...-7a18459 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: BSD-3-Clause Imports: 8 Imported by: 0

README

cpu: AMD Ryzen 9 5950X 16-Core Processor

std:

BenchmarkGzipCompressUnsafe-32              3225            313061 ns/op          55.44 MB/s
BenchmarkGzipCompress-32                    3172            320519 ns/op          54.15 MB/s
BenchmarkGzipCompressStream-32              3518            340196 ns/op          51.02 MB/s
BenchmarkGzipCompressStdZlib-32             2523            508630 ns/op          34.13 MB/s
BenchmarkGzipDecompressUnsafe-32          246291              4979 ns/op        3485.94 MB/s
BenchmarkGzipDecompress-32                142844              7848 ns/op        2211.71 MB/s
BenchmarkGzipDecompressStream-32           47031             23777 ns/op         729.98 MB/s
BenchmarkGzipDecompressStdZlib-32          76522             17548 ns/op         989.11 MB/s
BenchmarkCompressUnsafe-32                  3745            311609 ns/op          55.70 MB/s
BenchmarkCompress-32                        3709            325418 ns/op          53.34 MB/s
BenchmarkCompressStream-32                  3374            331544 ns/op          52.35 MB/s
BenchmarkCompressStdZlib-32                 2678            488616 ns/op          35.52 MB/s
BenchmarkDecompressUnsafe-32              215644              5870 ns/op        2957.11 MB/s
BenchmarkDecompress-32                    153452              7419 ns/op        2339.60 MB/s
BenchmarkDecompressStream-32               54178             24188 ns/op         717.58 MB/s
BenchmarkDecompressStdZlib-32              64855             18439 ns/op         941.30 MB/s

cloudflare:

BenchmarkGzipCompressUnsafe-32              3289            374924 ns/op          46.29 MB/s
BenchmarkGzipCompress-32                    3212            370161 ns/op          46.89 MB/s
BenchmarkGzipCompressStream-32              3046            387642 ns/op          44.78 MB/s
BenchmarkGzipCompressStdZlib-32             2538            458199 ns/op          37.88 MB/s
BenchmarkGzipDecompressUnsafe-32          538898              3693 ns/op        4700.35 MB/s
BenchmarkGzipDecompress-32                252193              4728 ns/op        3670.96 MB/s
BenchmarkGzipDecompressStream-32           62512             19553 ns/op         887.71 MB/s
BenchmarkGzipDecompressStdZlib-32          66260             17852 ns/op         972.25 MB/s
BenchmarkCompressUnsafe-32                  3296            371052 ns/op          46.78 MB/s
BenchmarkCompress-32                        3192            363557 ns/op          47.74 MB/s
BenchmarkCompressStream-32                  3063            378185 ns/op          45.90 MB/s
BenchmarkCompressStdZlib-32                 2701            467410 ns/op          37.13 MB/s
BenchmarkDecompressUnsafe-32              633384              1791 ns/op        9688.94 MB/s
BenchmarkDecompress-32                    320124              3463 ns/op        5012.67 MB/s
BenchmarkDecompressStream-32               67143             18934 ns/op         916.72 MB/s
BenchmarkDecompressStdZlib-32              63448             18821 ns/op         922.21 MB/s

chromium:

BenchmarkGzipCompressUnsafe-32              3661            336720 ns/op          51.55 MB/s
BenchmarkGzipCompress-32                    3543            344490 ns/op          50.38 MB/s
BenchmarkGzipCompressStream-32              3054            359438 ns/op          48.29 MB/s
BenchmarkGzipCompressStdZlib-32             2260            472063 ns/op          36.77 MB/s
BenchmarkGzipDecompressUnsafe-32          245685              4902 ns/op        3541.00 MB/s
BenchmarkGzipDecompress-32                170240              7471 ns/op        2323.37 MB/s
BenchmarkGzipDecompressStream-32           51409             24155 ns/op         718.56 MB/s
BenchmarkGzipDecompressStdZlib-32          69213             17872 ns/op         971.19 MB/s
BenchmarkCompressUnsafe-32                  3553            332198 ns/op          52.25 MB/s
BenchmarkCompress-32                        3424            332883 ns/op          52.14 MB/s
BenchmarkCompressStream-32                  3391            355412 ns/op          48.84 MB/s
BenchmarkCompressStdZlib-32                 2734            458269 ns/op          37.88 MB/s
BenchmarkDecompressUnsafe-32              207081              5765 ns/op        3010.76 MB/s
BenchmarkDecompress-32                    161613              7337 ns/op        2365.83 MB/s
BenchmarkDecompressStream-32               50998             22794 ns/op         761.47 MB/s
BenchmarkDecompressStdZlib-32              62338             19650 ns/op         883.30 MB/s

Documentation

Index

Constants

View Source
const (
	NoCompression      = flate.NoCompression
	BestSpeed          = flate.BestSpeed
	BestCompression    = flate.BestCompression
	DefaultCompression = flate.DefaultCompression
)

Constants copied from the flate package, so that code that imports czlib does not also have to import "compress/flate".

View Source
const (
	Z_NO_FLUSH      = 0
	Z_PARTIAL_FLUSH = 1
	Z_SYNC_FLUSH    = 2
	Z_FULL_FLUSH    = 3
	Z_FINISH        = 4
	Z_BLOCK         = 5
	Z_TREES         = 6
)

Allowed flush values

View Source
const (
	Z_OK            = 0
	Z_STREAM_END    = 1
	Z_NEED_DICT     = 2
	Z_ERRNO         = -1
	Z_STREAM_ERROR  = -2
	Z_DATA_ERROR    = -3
	Z_MEM_ERROR     = -4
	Z_BUF_ERROR     = -5
	Z_VERSION_ERROR = -6
)

Return codes

View Source
const (
	DEFAULT_COMPRESSED_BUFFER_SIZE = 32 * 1024
)

our default buffer size most go io functions use 32KB as buffer size, so 32KB works well here for compressed data buffer

View Source
const GZipWbits = C.MAX_WBITS | 16
View Source
const MaxWbits = C.MAX_WBITS

Variables

View Source
var (
	// ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
	ErrChecksum = zlib.ErrChecksum
	// ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
	ErrDictionary = zlib.ErrDictionary
	// ErrHeader is returned when reading ZLIB data that has an invalid header.
	ErrHeader = zlib.ErrHeader
)

Functions

func Compress

func Compress(input []byte) ([]byte, error)

func Compress2

func Compress2(input []byte, wbits int) ([]byte, error)

Compress returns the input compressed using zlib, or an error if encountered.

func Decompress

func Decompress(input []byte) ([]byte, error)

Decompress returns the input decompressed using zlib, or an error if encountered.

func NewReader

func NewReader(r io.Reader) (io.ReadCloser, error)

NewReader creates a new io.ReadCloser. Reads from the returned io.ReadCloser read and decompress data from r. The implementation buffers input and may read more data than necessary from r. It is the caller's responsibility to call Close on the ReadCloser when done.

func NewReaderBuffer

func NewReaderBuffer(r io.Reader, bufferSize int) (io.ReadCloser, error)

NewReaderBuffer has the same behavior as NewReader but the user can provides a custom buffer size.

Types

type UnsafeByte

type UnsafeByte []byte

An UnsafeByte is a []byte whose backing array has been allocated in C and thus is not subject to the Go garbage collector. The Unsafe versions of Compress and Decompress return this in order to prevent copying the unsafe memory into collected memory.

func NewUnsafeByte

func NewUnsafeByte(p *C.char, length int) UnsafeByte

NewUnsafeByte creates a []byte from the unsafe pointer without a copy, using the method outlined in this mailing list post:

https://groups.google.com/forum/#!topic/golang-nuts/KyXR0fDp0HA

but amended to use the three-index slices from go1.2 to set the capacity of b correctly:

https://tip.golang.org/doc/go1.2#three_index

This means this code only works in go1.2+.

This shouldn't copy the underlying array; it's just casting it Afterwards, we use reflect to fix the Cap & len of the slice.

func UnsafeCompress

func UnsafeCompress(input []byte) (UnsafeByte, error)

func UnsafeCompress2

func UnsafeCompress2(input []byte, wbits int) (UnsafeByte, error)

UnsafeCompress zips input into an UnsafeByte without copying the result malloced in C. The UnsafeByte returned can be used as a normal []byte but must be manually free'd w/ UnsafeByte.Free()

func UnsafeDecompress

func UnsafeDecompress(input []byte) (UnsafeByte, error)

UnsafeDecompress unzips input into an UnsafeByte without copying the result malloced in C. The UnsafeByte returned can be used as a normal []byte but must be manually free'd w/ UnsafeByte.Free()

func (UnsafeByte) Free

func (b UnsafeByte) Free()

Free the underlying byte array; doing this twice would be bad.

type Writer

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

Writer implements a io.WriteCloser we will call deflateEnd when we set err to a value: - whatever error is returned by the underlying writer - io.EOF if Close was called

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new zlib writer that writes to the underlying writer

func NewWriterLevel

func NewWriterLevel(w io.Writer, level int) (*Writer, error)

NewWriterLevel let the user provide a compression level value

func NewWriterLevelBuffer

func NewWriterLevelBuffer(w io.Writer, level, bufferSize int) (*Writer, error)

NewWriterLevelBuffer let the user provide compression level and buffer size values

func NewWriterLevelBufferWbits

func NewWriterLevelBufferWbits(w io.Writer, level, bufferSize, wbits int) (*Writer, error)

NewWriterLevelBuffer let the user provide compression level and buffer size values

func NewWriterLevelWbits

func NewWriterLevelWbits(w io.Writer, level, wbits int) *Writer

NewWriterLevelWbits returns a new zlib writer that writes to the underlying writer

func (*Writer) Close

func (z *Writer) Close() error

Close closes the zlib buffer but does not close the wrapped io.Writer originally passed to NewWriterX.

func (*Writer) Flush

func (z *Writer) Flush() error

Flush let the user flush the zlib buffer to the underlying writer buffer

func (*Writer) Write

func (z *Writer) Write(p []byte) (n int, err error)

Write implements the io.Writer interface

Jump to

Keyboard shortcuts

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