XXX for now we always compress, we should add a config option to avoid compression when local storage is fast compared to compute
(b []byte)
| 64 | // XXX for now we always compress, we should add a config option to |
| 65 | // avoid compression when local storage is fast compared to compute |
| 66 | func compressBuffer(b []byte) (uint8, []byte, error) { |
| 67 | inLen := len(b) |
| 68 | if inLen == 0 { |
| 69 | return CompressionFormatNone, nil, nil |
| 70 | } |
| 71 | zbuf := zbufPool.Get().(*[]byte) |
| 72 | defer zbufPool.Put(zbuf) |
| 73 | // Use inLen-1 so compression will fail if it doesn't result in |
| 74 | // fewer bytes. XXX make the -1 a bigger gap |
| 75 | *zbuf = slices.Grow((*zbuf)[:0], inLen-1)[:inLen-1] |
| 76 | var c lz4.Compressor |
| 77 | zlen, err := c.CompressBlock(b, *zbuf) |
| 78 | if err != nil && err != lz4.ErrInvalidSourceShortBuffer { |
| 79 | return 0, nil, err |
| 80 | } |
| 81 | if zlen > 0 { |
| 82 | // Compression succeeded. Copy bytes... XXX we should |
| 83 | // have a way to stash the buffer in the Primitive and |
| 84 | // release it after written. |
| 85 | bytes := make([]byte, zlen) |
| 86 | copy(bytes, *zbuf) |
| 87 | return CompressionFormatLZ4, bytes, nil |
| 88 | } |
| 89 | return CompressionFormatNone, b, nil |
| 90 | } |