dedup

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dedup splits a JSON request body into a skeleton and content-addressed pieces, and rebuilds the exact bytes from them. Agent clients resend the whole conversation every turn, so storing each unique message, tool definition and system prompt once removes most of a payload store's volume.

Split works on raw bytes and never re-serialises JSON: a piece is the verbatim bytes of one array element (or one whole value), and the skeleton is the body with those bytes cut out. Rebuild must reproduce the input byte for byte; Split checks that itself and falls back to storing the body as a single piece when it cannot, so a caller never stores something it cannot read back.

The split rule knows no wire shape: every top-level array is split per element, every other top-level value of at least minValuePiece bytes is one piece, and the rest stays in the skeleton.

Out of scope: storage.

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingPiece = errors.New("dedup: missing piece")

ErrMissingPiece is returned by Rebuild when lookup has no piece for a hash.

Functions

func CommonPrefix

func CommonPrefix(a, b []Hash) int

CommonPrefix returns how many leading hashes a and b share. For two requests of one conversation, the first differing index is where a prompt-cache prefix stops matching.

func Rebuild

func Rebuild(skeleton []byte, fields []Field, lookup func(Hash) ([]byte, bool)) ([]byte, error)

Rebuild reassembles a body from its skeleton, fields and a piece lookup.

Types

type Field

type Field struct {
	// Name is the top-level key, empty for a body stored whole.
	Name string
	// Array is true when the value was an array split per element; the
	// pieces are rejoined with commas. Otherwise the value is one piece.
	Array bool
	// Offset is the byte position in the skeleton where the pieces go.
	Offset int
	// Hashes lists the field's pieces in order, repeats included.
	Hashes []Hash
}

Field is where one top-level field's pieces go back into the skeleton.

type Hash

type Hash [16]byte

Hash is the first 16 bytes of the SHA-256 of a piece's bytes. Pieces may be shared across owners, so the hash must resist construction: an accidental collision needs ~2^64 pieces, and corrupting another caller's piece needs a second preimage (~2^128 work).

type Piece

type Piece struct {
	Hash Hash
	Body []byte
}

Piece is one unique content-addressed fragment of a body. Body aliases the input passed to Split.

type Split

type Split struct {
	Skeleton []byte
	// Fields are in ascending Offset order.
	Fields []Field
	// Pieces holds each distinct piece once, in first-seen order.
	Pieces []Piece
}

Split is a body broken into a skeleton plus pieces.

func SplitBody

func SplitBody(body []byte) Split

SplitBody splits body. A body that is not a JSON object (truncated, binary, an array) or that would not rebuild exactly is returned as one piece.

func (Split) Whole

func (s Split) Whole() bool

Whole reports whether the body was stored as a single piece.

Jump to

Keyboard shortcuts

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