-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement extra compressed chunks
This allows to keep more data in the circular buffer by compressing old chunks and keeping them around, so the overall memory consumption is smaller. Change is backwards compatible - by default compression is not used. Tests were refactored to use regular *testing.T. Added benchmarks for read and write operations. Writes are allocation-free, even when using compression (compression buffer is re-used): ``` goos: linux goarch: amd64 pkg: github.com/siderolabs/go-circular cpu: AMD Ryzen 9 5950X 16-Core Processor BenchmarkWrite/defaults-32 7848931 150.5 ns/op 0 B/op 0 allocs/op BenchmarkWrite/growing_buffer-32 7116709 144.3 ns/op 0 B/op 0 allocs/op BenchmarkWrite/compression-32 3042698 399.2 ns/op 0 B/op 0 allocs/op ``` Reads are not allocation-free, but each Reader has fixed allocation cost (decompression buffer is re-used): ``` goos: linux goarch: amd64 pkg: github.com/siderolabs/go-circular cpu: AMD Ryzen 9 5950X 16-Core Processor BenchmarkRead/defaults-32 99565 12682 ns/op 80 B/op 1 allocs/op BenchmarkRead/growing_buffer-32 88371 12327 ns/op 80 B/op 1 allocs/op BenchmarkRead/compression-32 3193 372417 ns/op 139425 B/op 3 allocs/op ``` Co-authored-by: Utku Ozdemir <utku.ozdemir@siderolabs.com> Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
- Loading branch information
1 parent
835f04c
commit 3c48c53
Showing
16 changed files
with
1,205 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
# go-circular | ||
|
||
Package circular provides a buffer with circular semantics. | ||
|
||
## Design | ||
|
||
The buffer is split into chunks, the last chunk is not compressed and being written to, and previous chunks | ||
are compressed and immutable. | ||
|
||
The buffer keeps a pointer to the current offset being written which is always incremented, while the index | ||
of compressed chunks keeps the initial offset of each compressed chunk and its decompressed size. | ||
|
||
If the compressed chunk is being read, it is decompressed on the fly by the Reader. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package circular | ||
|
||
type chunk struct { | ||
// compressed data | ||
compressed []byte | ||
// start offset of the chunk, as it was in the circular buffer when the chunk was created | ||
startOffset int64 | ||
// uncompressed size of the chunk | ||
size int64 | ||
// [TODO]: have a unique (incrementing?) chunk ID for file-based storage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.