Efficient Go Serialization of Struct to Disk
Problem:
Optimizing serialization performance by minimizing bloat in the output when encoding structs to disk using gob.
Proposed Solution:
While the gob package provides efficient serialization, it includes type information in the encoding, resulting in an initial overhead of 26 bytes per unique type. For structures with multiple instances, this overhead can be amortized across them.
Alternative:
For applications where even this overhead is unacceptable, consider using compression techniques like flate, zlib, gzip, or bzip2 to further reduce the output size by 50-80%.
Example:
The following code demonstrates the overhead of a single Entry using gob:
package main import ( "bytes" "encoding/gob" "fmt" ) type Entry struct { Key string Val string } func main() { var buf bytes.Buffer enc := gob.NewEncoder(&buf) e := Entry{"k1", "v1"} enc.Encode(e) fmt.Println(buf.Len()) // Prints 48 bytes }
If multiple instances of Entry are serialized, the overhead is amortized:
for i := 0; i < 1000; i++ { e.Key = fmt.Sprintf("k%3d", i) e.Val = fmt.Sprintf("v%3d", i) enc.Encode(e) } fmt.Println(buf.Len()) // Prints 16036 bytes = 16.04 bytes/Entry
To achieve the desired compact serialization format, additional compression techniques can be used:
import ( "compress/bzip2" "compress/flate" "compress/gzip" "compress/zlib" ) var out io.Writer switch name { case "Naked": out = buf case "flate": out, _ = flate.NewWriter(buf, flate.DefaultCompression) case "zlib": out, _ = zlib.NewWriterLevel(buf, zlib.DefaultCompression) case "gzip": out = gzip.NewWriter(buf) case "bzip2": out, _ = bzip2.NewWriter(buf, nil) }
The resulting output sizes with compression techniques:
Technique | Output Size | Average / Entry |
---|---|---|
Naked | 16036 bytes | 16.04 bytes |
flate | 4120 bytes | 4.12 bytes |
zlib | 4126 bytes | 4.13 bytes |
gzip | 4138 bytes | 4.14 bytes |
bzip2 | 2042 bytes | 2.04 bytes |
The above is the detailed content of How Can I Efficiently Serialize Go Structs to Disk and Minimize File Size?. For more information, please follow other related articles on the PHP Chinese website!