klauspost/compress 1.20: LZW Streaming and Microsoft XPRESS Decoding in GoArticle · digest for October 13
System archive unpacking and acceleration in Go with native decoding of LZW and Microsoft XPRESS streams on an ARM64 chip.
Article4 sections≈ 5 min

klauspost/compress 1.20: LZW Streaming and Microsoft XPRESS Decoding in Go

In the Go backend ecosystem, Klaus Post's klauspost/compress library has long been the gold standard for high-speed compression. When services need to compress traffic on the fly, store terabytes of time-series data in InfluxDB, distribute blocks in CockroachDB, or serve objects in MinIO, teams routinely swap out the standard library's compress/* packages for Klaus Post's implementations. Heavy use of AVX2, AVX-512, and ARM NEON instructions squeezes maximum gigabytes per second out of every CPU core.

The klauspost/compress 1.20 release broadens the library beyond modern formats like zstd, S2, and snappy to cover legacy OS protocols and specialized archive formats. It introduces a native LZW implementation, a Microsoft XPRESS (MS-XCA) decoder, and major assembly improvements for arm64.

Native LZW Support and Microsoft XPRESS Decompression

Although Lempel-Ziv-Welch (LZW) dates back to the 1980s, it still appears in GIFs, classic Unix .Z archives, and industrial network protocols. Go's built-in compress/lzw package works for basic cases, but it creates noticeable intermediate allocations and lags under heavy streaming I/O.

Version 1.20 adds github.com/klauspost/compress/lzw (PR #1189 by Klaus Post). It provides optimized Reader and Writer types with lower memory overhead, configurable bit ordering (LSB/MSB), and literal widths from 6 to 8 bits.

The second major addition is github.com/klauspost/compress/xpress (PR #1195 by Gowtham), which implements Microsoft's official [MS-XCA]: Xpress Compression Algorithm specification. XPRESS underpins a wide range of Windows enterprise protocols:

  • Windows RPC remote procedure calls;
  • Active Directory directory replication (MS-DRSR);
  • NTFS file compression via the Windows Overlay Filter (WOF);
  • Windows Imaging Format (WIM) disk and update images.

Both Microsoft variants are supported: plain LZ77 streaming and the hybrid LZ77 + Huffman prefix-coding mode. Previously, Go developers building network forensics tools, registry dump parsers, or domain controllers had to wrap C libraries through CGO, breaking static builds and hurting throughput. Now all of it runs in pure Go.

arm64 Assembly and Cooperative Scheduler Protection

Beyond new formats, 1.20 brings low-level speedups for ARM servers such as AWS Graviton, Ampere Altra, and Apple Silicon:

  • S2 assembly via avo: Liz Fong-Jones ported the S2 encoder generators from amd64 to arm64 using the avo toolkit (PR #1187), closing a long-standing throughput gap on ARM;
  • Faster table lookups in zstd and huff0: replacing paired load instructions with MOVWQZX and clearing register dependencies in the decoder pipeline boosted arm64 speeds by 8–12%;
  • Goroutine scheduler safety in xxhash: assembly checksum loops now cap how much data they process without yielding. Previously, long uninterrupted assembly loops could pin OS threads, stop the Go scheduler from switching goroutines, and trigger stop-the-world GC pauses—an area closely tied to upcoming runtime work in Go 1.27.

Practical Example: Streaming LZW and Decoding an XPRESS Buffer

The first snippet shows a full streaming round-trip using lzw.Writer and lzw.Reader in memory:

package main

import (
    "bytes"
    "fmt"
    "io"
    "log"

    "github.com/klauspost/compress/lzw"
)

func main() {
    var buffer bytes.Buffer
    payload := []byte("Streaming LZW compression with minimal allocations in klauspost/compress")

    // 1. Initialize streaming encoder (LSB bit order, 8-bit literal width)
    writer := lzw.NewWriter(&buffer, lzw.LSB, 8)
    if _, err := writer.Write(payload); err != nil {
        log.Fatalf("LZW compression failed: %v", err)
    }
    if err := writer.Close(); err != nil {
        log.Fatalf("Stream close failed: %v", err)
    }

    // 2. Read and decompress the byte stream
    reader := lzw.NewReader(&buffer, lzw.LSB, 8)
    defer reader.Close()

    decompressed, err := io.ReadAll(reader)
    if err != nil {
        log.Fatalf("LZW decompression failed: %v", err)
    }

    fmt.Printf("Decompressed %d bytes: %s\n", len(decompressed), string(decompressed))
}

The second snippet demonstrates decoding a binary payload compressed with Microsoft XPRESS:

package main

import (
    "fmt"
    "log"

    "github.com/klauspost/compress/xpress"
)

// DecompressPayload decodes an MS-XCA buffer without CGO
func DecompressPayload(compressed []byte, expectedSize int) []byte {
    // Allocate target slice for decompressed output
    destination := make([]byte, expectedSize)

    // Decode buffer (supports both LZ77 and Huffman MS-XCA variants)
    bytesWritten, err := xpress.Decode(destination, compressed)
    if err != nil {
        log.Fatalf("Microsoft XPRESS decode failed: %v", err)
    }

    fmt.Printf("Recovered %d bytes from XPRESS payload\n", bytesWritten)
    return destination[:bytesWritten]
}

Adoption Notes and Verdict

Before upgrading to klauspost/compress 1.20, note that the xpress package currently supports decompression only. MS-XCA compression isn't implemented yet, though read-only support is enough for inspecting Windows protocols.

For projects running on cloud ARM instances or integrating with Microsoft infrastructure, this update is an easy win: you get free arm64 speedups with zero application changes and a clean pure-Go stack free of CGO headaches.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2