-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoc.go
More file actions
77 lines (77 loc) · 2.41 KB
/
Copy pathdoc.go
File metadata and controls
77 lines (77 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package fsst provides fast string compression via learned symbol tables.
//
// # Overview
//
// FSST (Fast Static Symbol Table) is a compression algorithm optimized for
// strings with repetitive patterns. It learns symbols (1-8 bytes each)
// from training data and encodes text by replacing matches with codes.
//
// This package provides two implementations:
// - FSST (8-bit codes): Up to 255 symbols, escape mechanism for literals
// - FSST12 (12-bit codes): Up to 3840 symbols, no escape needed
//
// # When to Use FSST
//
// FSST excels at compressing:
// - Structured text: JSON, CSV, logs, XML
// - Repetitive strings: database dumps, API responses
// - Text with common patterns: URLs, email addresses, timestamps
//
// Typical compression ratios: 1.5x to 3x, depending on repetitiveness.
//
// # When NOT to Use FSST
//
// FSST is not suitable for:
// - Binary data (use gzip, zstd, or specialized codecs)
// - Random or encrypted data (incompressible)
// - Datasets without shared patterns across records
// - Single-use compression (training cost exceeds benefit)
//
// # FSST vs FSST12
//
// FSST (8-bit codes):
// - Faster encoding and decoding
// - Uses escape codes for literal bytes
// - Best for text with good symbol coverage
//
// FSST12 (12-bit codes):
// - More symbols available (3840 vs 255)
// - No escape overhead (all bytes have codes)
// - Better for diverse text with many patterns
// - Outputs 12-bit packed codes (1.5 bytes per code)
//
// # Basic Usage
//
// // FSST (8-bit)
// inputs := [][]byte{
// []byte(`{"id":123,"name":"Alice"}`),
// []byte(`{"id":456,"name":"Bob"}`),
// }
// tbl := fsst.Train(inputs)
// compressed := tbl.EncodeAll([]byte(`{"id":789,"name":"Charlie"}`))
// original := tbl.DecodeAll(compressed)
//
// // FSST12 (12-bit)
// tbl12 := fsst.Train12(inputs)
// compressed12 := tbl12.EncodeAll([]byte(`{"id":789,"name":"Charlie"}`))
// original12 := tbl12.DecodeAll(compressed12)
//
// // Serialize for reuse
// data, _ := tbl.MarshalBinary()
// var tbl2 fsst.Table
// tbl2.UnmarshalBinary(data)
//
// # Performance Characteristics
//
// Training: O(n × k) where n is input size, k is number of rounds (5)
//
// FSST (8-bit):
// - Encoding: ~130-185 MB/s
// - Decoding: ~1.1-1.4 GB/s with buffer reuse
// - Table size: ~140KB in memory
//
// FSST12 (12-bit):
// - Encoding: ~250 MB/s
// - Decoding: ~500 MB/s
// - Table size: ~550KB in memory
package fsst