Clarify the emitLiteral and emitCopy preconditions.
This allows deleting some redundant code.
name old speed new speed delta
WordsEncode1e1-8 679MB/s ± 0% 678MB/s ± 0% ~ (p=0.087 n=5+5)
WordsEncode1e2-8 87.5MB/s ± 0% 87.5MB/s ± 0% ~ (p=0.579 n=5+5)
WordsEncode1e3-8 258MB/s ± 0% 257MB/s ± 1% ~ (p=1.000 n=5+5)
WordsEncode1e4-8 243MB/s ± 0% 247MB/s ± 1% +1.77% (p=0.008 n=5+5)
WordsEncode1e5-8 185MB/s ± 1% 186MB/s ± 0% ~ (p=0.095 n=5+5)
WordsEncode1e6-8 210MB/s ± 2% 211MB/s ± 0% ~ (p=0.222 n=5+5)
RandomEncode-8 13.2GB/s ± 1% 13.1GB/s ± 2% ~ (p=0.286 n=4+5)
_ZFlat0-8 429MB/s ± 0% 433MB/s ± 0% +0.83% (p=0.016 n=4+5)
_ZFlat1-8 274MB/s ± 0% 276MB/s ± 0% +0.91% (p=0.016 n=4+5)
_ZFlat2-8 13.6GB/s ± 2% 13.8GB/s ± 1% ~ (p=0.095 n=5+5)
_ZFlat3-8 169MB/s ± 0% 170MB/s ± 0% +0.38% (p=0.032 n=4+5)
_ZFlat4-8 3.09GB/s ± 0% 3.09GB/s ± 1% ~ (p=0.905 n=4+5)
_ZFlat5-8 419MB/s ± 5% 427MB/s ± 1% +1.83% (p=0.032 n=5+5)
_ZFlat6-8 189MB/s ± 1% 190MB/s ± 0% +0.68% (p=0.016 n=4+5)
_ZFlat7-8 181MB/s ± 1% 182MB/s ± 0% +0.60% (p=0.008 n=5+5)
_ZFlat8-8 199MB/s ± 1% 200MB/s ± 0% +0.68% (p=0.008 n=5+5)
_ZFlat9-8 175MB/s ± 0% 175MB/s ± 0% ~ (p=0.095 n=5+5)
_ZFlat10-8 507MB/s ± 0% 507MB/s ± 0% ~ (p=0.222 n=5+5)
_ZFlat11-8 277MB/s ± 0% 278MB/s ± 0% +0.38% (p=0.008 n=5+5)
diff --git a/encode.go b/encode.go
index 5759357..d816a7d 100644
--- a/encode.go
+++ b/encode.go
@@ -22,6 +22,10 @@
}
// emitLiteral writes a literal chunk and returns the number of bytes written.
+//
+// It assumes that:
+// dst is long enough to hold the encoded bytes
+// 1 <= len(lit) && len(lit) <= 65536
func emitLiteral(dst, lit []byte) int {
i, n := 0, uint(len(lit)-1)
switch {
@@ -32,34 +36,21 @@
dst[0] = 60<<2 | tagLiteral
dst[1] = uint8(n)
i = 2
- case n < 1<<16:
+ default:
dst[0] = 61<<2 | tagLiteral
dst[1] = uint8(n)
dst[2] = uint8(n >> 8)
i = 3
- case n < 1<<24:
- dst[0] = 62<<2 | tagLiteral
- dst[1] = uint8(n)
- dst[2] = uint8(n >> 8)
- dst[3] = uint8(n >> 16)
- i = 4
- case int64(n) < 1<<32:
- dst[0] = 63<<2 | tagLiteral
- dst[1] = uint8(n)
- dst[2] = uint8(n >> 8)
- dst[3] = uint8(n >> 16)
- dst[4] = uint8(n >> 24)
- i = 5
- default:
- panic("snappy: source buffer is too long")
}
- if copy(dst[i:], lit) != len(lit) {
- panic("snappy: destination buffer is too short")
- }
- return i + len(lit)
+ return i + copy(dst[i:], lit)
}
// emitCopy writes a copy chunk and returns the number of bytes written.
+//
+// It assumes that:
+// dst is long enough to hold the encoded bytes
+// 1 <= offset && offset <= 65535
+// 4 <= length && length <= 65535
func emitCopy(dst []byte, offset, length int) int {
i := 0
// The maximum length for a single tagCopy1 or tagCopy2 op is 64 bytes. The