| 1 |
# Reed-Solomon |
| 2 |
[](https://pkg.go.dev/github.com/klauspost/reedsolomon) [](https://github.com/klauspost/reedsolomon/actions/workflows/go.yml) |
| 3 |
|
| 4 |
Reed-Solomon Erasure Coding in Go, with speeds exceeding 1GB/s/cpu core implemented in pure Go. |
| 5 |
|
| 6 |
This is a Go port of the [JavaReedSolomon](https://github.com/Backblaze/JavaReedSolomon) library released by |
| 7 |
[Backblaze](http://backblaze.com), with some additional optimizations. |
| 8 |
|
| 9 |
For an introduction on erasure coding, see the post on the [Backblaze blog](https://www.backblaze.com/blog/reed-solomon/). |
| 10 |
|
| 11 |
For encoding high shard counts (>256) a Leopard implementation is used. |
| 12 |
For most platforms this performs close to the original Leopard implementation in terms of speed. |
| 13 |
|
| 14 |
Package home: https://github.com/klauspost/reedsolomon |
| 15 |
|
| 16 |
Godoc: https://pkg.go.dev/github.com/klauspost/reedsolomon |
| 17 |
|
| 18 |
# Installation |
| 19 |
To get the package use the standard: |
| 20 |
```bash |
| 21 |
go get -u github.com/klauspost/reedsolomon |
| 22 |
``` |
| 23 |
|
| 24 |
Using Go modules is recommended. |
| 25 |
|
| 26 |
# Usage |
| 27 |
|
| 28 |
This section assumes you know the basics of Reed-Solomon encoding. |
| 29 |
A good start is this [Backblaze blog post](https://www.backblaze.com/blog/reed-solomon/). |
| 30 |
|
| 31 |
This package performs the calculation of the parity sets. The usage is therefore relatively simple. |
| 32 |
|
| 33 |
First of all, you need to choose your distribution of data and parity shards. |
| 34 |
A 'good' distribution is very subjective, and will depend a lot on your usage scenario. |
| 35 |
|
| 36 |
To create an encoder with 10 data shards (where your data goes) and 3 parity shards (calculated): |
| 37 |
```Go |
| 38 |
enc, err := reedsolomon.New(10, 3) |
| 39 |
``` |
| 40 |
This encoder will work for all parity sets with this distribution of data and parity shards. |
| 41 |
|
| 42 |
If you will primarily be using it with one shard size it is recommended to use |
| 43 |
[`WithAutoGoroutines(shardSize)`](https://pkg.go.dev/github.com/klauspost/reedsolomon?tab=doc#WithAutoGoroutines) |
| 44 |
as an additional parameter. This will attempt to calculate the optimal number of goroutines to use for the best speed. |
| 45 |
It is not required that all shards are this size. |
| 46 |
|
| 47 |
Then you send and receive data that is a simple slice of byte slices; `[][]byte`. |
| 48 |
In the example above, the top slice must have a length of 13. |
| 49 |
|
| 50 |
```Go |
| 51 |
data := make([][]byte, 13) |
| 52 |
``` |
| 53 |
You should then fill the 10 first slices with *equally sized* data, |
| 54 |
and create parity shards that will be populated with parity data. In this case we create the data in memory, |
| 55 |
but you could for instance also use [mmap](https://github.com/edsrzf/mmap-go) to map files. |
| 56 |
|
| 57 |
```Go |
| 58 |
// Create all shards, size them at 50000 each |
| 59 |
for i := range input { |
| 60 |
data[i] := make([]byte, 50000) |
| 61 |
} |
| 62 |
|
| 63 |
// The above allocations can also be done by the encoder: |
| 64 |
// data := enc.(reedsolomon.Extended).AllocAligned(50000) |
| 65 |
|
| 66 |
// Fill some data into the data shards |
| 67 |
for i, in := range data[:10] { |
| 68 |
for j:= range in { |
| 69 |
in[j] = byte((i+j)&0xff) |
| 70 |
} |
| 71 |
} |
| 72 |
``` |
| 73 |
|
| 74 |
To populate the parity shards, you simply call `Encode()` with your data. |
| 75 |
```Go |
| 76 |
err = enc.Encode(data) |
| 77 |
``` |
| 78 |
The only cases where you should get an error is, if the data shards aren't of equal size. |
| 79 |
The last 3 shards now contain parity data. You can verify this by calling `Verify()`: |
| 80 |
|
| 81 |
```Go |
| 82 |
ok, err = enc.Verify(data) |
| 83 |
``` |
| 84 |
|
| 85 |
The final (and important) part is to be able to reconstruct missing shards. |
| 86 |
For this to work, you need to know which parts of your data is missing. |
| 87 |
The encoder *does not know which parts are invalid*, so if data corruption is a likely scenario, |
| 88 |
you need to implement a hash check for each shard. |
| 89 |
|
| 90 |
If a byte has changed in your set, and you don't know which it is, there is no way to reconstruct the data set. |
| 91 |
|
| 92 |
To indicate missing data, you set the shard to nil before calling `Reconstruct()`: |
| 93 |
|
| 94 |
```Go |
| 95 |
// Delete two data shards |
| 96 |
data[3] = nil |
| 97 |
data[7] = nil |
| 98 |
|
| 99 |
// Reconstruct the missing shards |
| 100 |
err := enc.Reconstruct(data) |
| 101 |
``` |
| 102 |
The missing data and parity shards will be recreated. If more than 3 shards are missing, the reconstruction will fail. |
| 103 |
|
| 104 |
If you are only interested in the data shards (for reading purposes) you can call `ReconstructData()`: |
| 105 |
|
| 106 |
```Go |
| 107 |
// Delete two data shards |
| 108 |
data[3] = nil |
| 109 |
data[7] = nil |
| 110 |
|
| 111 |
// Reconstruct just the missing data shards |
| 112 |
err := enc.ReconstructData(data) |
| 113 |
``` |
| 114 |
|
| 115 |
If you don't need all data shards you can use `ReconstructSome()`: |
| 116 |
|
| 117 |
```Go |
| 118 |
// Delete two data shards |
| 119 |
data[3] = nil |
| 120 |
data[7] = nil |
| 121 |
|
| 122 |
// Reconstruct just the shard 3 |
| 123 |
err := enc.ReconstructSome(data, []bool{false, false, false, true, false, false, false, false}) |
| 124 |
``` |
| 125 |
|
| 126 |
So to sum up reconstruction: |
| 127 |
* The number of data/parity shards must match the numbers used for encoding. |
| 128 |
* The order of shards must be the same as used when encoding. |
| 129 |
* You may only supply data you know is valid. |
| 130 |
* Invalid shards should be set to nil. |
| 131 |
|
| 132 |
For complete examples of an encoder and decoder see the |
| 133 |
[examples folder](https://github.com/klauspost/reedsolomon/tree/master/examples). |
| 134 |
|
| 135 |
# Splitting/Joining Data |
| 136 |
|
| 137 |
You might have a large slice of data. |
| 138 |
To help you split this, there are some helper functions that can split and join a single byte slice. |
| 139 |
|
| 140 |
```Go |
| 141 |
bigfile, _ := os.Readfile("myfile.data") |
| 142 |
|
| 143 |
// Split the file |
| 144 |
split, err := enc.Split(bigfile) |
| 145 |
``` |
| 146 |
This will split the file into the number of data shards set when creating the encoder and create empty parity shards. |
| 147 |
|
| 148 |
An important thing to note is that you have to *keep track of the exact input size*. |
| 149 |
If the size of the input isn't divisible by the number of data shards, extra zeros will be inserted in the last shard. |
| 150 |
|
| 151 |
To join a data set, use the `Join()` function, which will join the shards and write it to the `io.Writer` you supply: |
| 152 |
```Go |
| 153 |
// Join a data set and write it to io.Discard. |
| 154 |
err = enc.Join(io.Discard, data, len(bigfile)) |
| 155 |
``` |
| 156 |
|
| 157 |
## Aligned Allocations |
| 158 |
|
| 159 |
For AMD64 aligned inputs can make a big speed difference. |
| 160 |
|
| 161 |
This is an example of the speed difference when inputs are unaligned/aligned: |
| 162 |
|
| 163 |
``` |
| 164 |
BenchmarkEncodeUnaligned100x20x10000-32 100198 36085 ns/op 33254.75 MB/s |
| 165 |
BenchmarkEncodeAligned100x20x10000-32 102711 35207 ns/op 34084.15 MB/s |
| 166 |
``` |
| 167 |
|
| 168 |
This is mostly the case when dealing with odd-sized shards. |
| 169 |
|
| 170 |
To facilitate this the package provides an `AllocAligned(shards, each int) [][]byte`. |
| 171 |
This will allocate a number of shards, each with the size `each`. |
| 172 |
Each shard will then be aligned to a 64 byte boundary. |
| 173 |
|
| 174 |
Each encoder also has a `AllocAligned(each int) [][]byte` as an extended interface which will return the same, |
| 175 |
but with the shard count configured in the encoder. |
| 176 |
|
| 177 |
It is not possible to re-aligned already allocated slices, for example when using `Split`. |
| 178 |
When it is not possible to write to aligned shards, you should not copy to them. |
| 179 |
|
| 180 |
# Progressive encoding |
| 181 |
|
| 182 |
It is possible to encode individual shards using EncodeIdx: |
| 183 |
|
| 184 |
```Go |
| 185 |
// EncodeIdx will add parity for a single data shard. |
| 186 |
// Parity shards should start out as 0. The caller must zero them. |
| 187 |
// Data shards must be delivered exactly once. There is no check for this. |
| 188 |
// The parity shards will always be updated and the data shards will remain the same. |
| 189 |
EncodeIdx(dataShard []byte, idx int, parity [][]byte) error |
| 190 |
``` |
| 191 |
|
| 192 |
This allows progressively encoding the parity by sending individual data shards. |
| 193 |
There is no requirement on shards being delivered in order, |
| 194 |
but when sent in order it allows encoding shards one at the time, |
| 195 |
effectively allowing the operation to be streaming. |
| 196 |
|
| 197 |
The result will be the same as encoding all shards at once. |
| 198 |
There is a minor speed penalty using this method, so send |
| 199 |
shards at once if they are available. |
| 200 |
|
| 201 |
## Example |
| 202 |
|
| 203 |
```Go |
| 204 |
func test() { |
| 205 |
// Create an encoder with 7 data and 3 parity slices. |
| 206 |
enc, _ := reedsolomon.New(7, 3) |
| 207 |
|
| 208 |
// This will be our output parity. |
| 209 |
parity := make([][]byte, 3) |
| 210 |
for i := range parity { |
| 211 |
parity[i] = make([]byte, 10000) |
| 212 |
} |
| 213 |
|
| 214 |
for i := 0; i < 7; i++ { |
| 215 |
// Send data shards one at the time. |
| 216 |
_ = enc.EncodeIdx(make([]byte, 10000), i, parity) |
| 217 |
} |
| 218 |
|
| 219 |
// parity now contains parity, as if all data was sent in one call. |
| 220 |
} |
| 221 |
``` |
| 222 |
|
| 223 |
# Progressive decoding |
| 224 |
|
| 225 |
For advanced use cases, you can progressively decode missing shards (data or parity) using `DecodeIdx`. |
| 226 |
This allows you to: |
| 227 |
|
| 228 |
* Reconstruct shards from multiple sources arriving at different times |
| 229 |
* Merge partial reconstructions from different nodes in a distributed system |
| 230 |
* Incrementally add input shards as they become available |
| 231 |
|
| 232 |
To access progressive decoding, cast your encoder to the `Extensions` interface: |
| 233 |
|
| 234 |
```Go |
| 235 |
// Cast to Extensions interface |
| 236 |
ext := enc.(reedsolomon.Extensions) |
| 237 |
``` |
| 238 |
|
| 239 |
## Basic Usage |
| 240 |
|
| 241 |
`DecodeIdx` works with three parameters: |
| 242 |
* `dst [][]byte` - Destination slices for reconstructed data (pre-allocated, initially zeroed) |
| 243 |
* `expectInput []bool` - Which shards you expect to receive (true = expected) |
| 244 |
* `input [][]byte` - The actual input shards for this call |
| 245 |
|
| 246 |
<details> |
| 247 |
|
| 248 |
<summary>Click to see example</summary> |
| 249 |
|
| 250 |
```Go |
| 251 |
func doReconstruct() { |
| 252 |
// Create encoder: |
| 253 |
enc, _ := reedsolomon.New(5, 3) |
| 254 |
ext := enc.(reedsolomon.Extensions) |
| 255 |
|
| 256 |
// Set up reconstruction - we want to reconstruct shards 1 and 4 |
| 257 |
dst := make([][]byte, 5+3) |
| 258 |
dst[1] = make([]byte, shardSize) // Will reconstruct shard 1 |
| 259 |
dst[4] = make([]byte, shardSize) // Will reconstruct shard 4 |
| 260 |
|
| 261 |
// Mark which shards we expect to receive |
| 262 |
expectInput := []bool{ |
| 263 |
0: true, |
| 264 |
1: false, // Reconstructing this shard. |
| 265 |
2: true, |
| 266 |
3: true, |
| 267 |
4: false, // Reconstructing this shard. |
| 268 |
5: true, |
| 269 |
6: true, |
| 270 |
7: false, // We only need to supply 6 shards, so we skip this. |
| 271 |
} |
| 272 |
|
| 273 |
// First call - provide some shards |
| 274 |
input1 := make([][]byte, 5+3) |
| 275 |
input1[0] = shard0data |
| 276 |
input1[2] = shard2data |
| 277 |
|
| 278 |
err := ext.DecodeIdx(dst, expectInput, input1) |
| 279 |
|
| 280 |
// Second call - provide remaining shards |
| 281 |
input2 := make([][]byte, 5+3) |
| 282 |
input2[3] = shard3data |
| 283 |
input2[5] = shard5data |
| 284 |
input2[6] = shard6data |
| 285 |
|
| 286 |
err = ext.DecodeIdx(dst, expectInput, input2) |
| 287 |
// dst[1] and dst[4] now contain the reconstructed data |
| 288 |
} |
| 289 |
``` |
| 290 |
|
| 291 |
</details> |
| 292 |
|
| 293 |
## Merging Partial DecodeIdx Results |
| 294 |
|
| 295 |
You can also merge partial reconstructions from different nodes using merge mode (`expectInput == nil`): |
| 296 |
|
| 297 |
<details> |
| 298 |
|
| 299 |
<summary>Click to see example</summary> |
| 300 |
|
| 301 |
```Go |
| 302 |
func progressiveReconstruct() { |
| 303 |
// Create encoder: 5 data + 3 parity = 8 total shards |
| 304 |
enc, _ := reedsolomon.New(5, 3) |
| 305 |
ext := enc.(reedsolomon.Extensions) |
| 306 |
|
| 307 |
// Assume we lost shards 1 and 3, need to reconstruct them |
| 308 |
dst1 := make([][]byte, 8) |
| 309 |
dst1[1] = make([]byte, shardSize) // Reconstruct shard 1 |
| 310 |
dst1[3] = make([]byte, shardSize) // Reconstruct shard 3 |
| 311 |
|
| 312 |
// We need 5 valid shards total - mark which ones we expect |
| 313 |
expectInput := make([]bool, 8) |
| 314 |
expectInput[0] = true // Have shard 0 |
| 315 |
expectInput[2] = true // Have shard 2 |
| 316 |
expectInput[4] = true // Have shard 4 |
| 317 |
expectInput[5] = true // Have shard 5 (parity) |
| 318 |
expectInput[6] = true // Have shard 6 (parity) |
| 319 |
|
| 320 |
// First source provides shards 0, 2 |
| 321 |
input1 := make([][]byte, 8) |
| 322 |
input1[0] = availableShards[0] |
| 323 |
input1[2] = availableShards[2] |
| 324 |
ext.DecodeIdx(dst1, expectInput, input1) |
| 325 |
|
| 326 |
// Second source provides shard 4, 5 and 6 |
| 327 |
dst2 := make([][]byte, 8) |
| 328 |
dst2[1] = make([]byte, shardSize) // Reconstruct shard 1 |
| 329 |
dst2[3] = make([]byte, shardSize) // Reconstruct shard 3 |
| 330 |
|
| 331 |
input2 := make([][]byte, 8) |
| 332 |
input2[4] = availableShards[4] |
| 333 |
input2[5] = availableShards[5] |
| 334 |
input2[6] = availableShards[6] |
| 335 |
ext.DecodeIdx(dst2, expectInput, input2) |
| 336 |
|
| 337 |
// Merge the dst2 partial result into dst1 |
| 338 |
// These can come from different machines. |
| 339 |
ext.DecodeIdx(dst1, nil, dst2) |
| 340 |
|
| 341 |
// dst1[1] and dst1[3] now contain the reconstructed data |
| 342 |
} |
| 343 |
``` |
| 344 |
</details> |
| 345 |
|
| 346 |
|
| 347 |
## Important Notes |
| 348 |
|
| 349 |
* **Consistency**: `expectInput` must be the same across all calls for a reconstruction |
| 350 |
* **Completeness**: You must provide exactly the shards marked in `expectInput`, one time each |
| 351 |
* **Automatic shard selection**: When more than `dataShards` positions are marked as `true` in `expectInput`, only the first `dataShards` will be used for reconstruction. |
| 352 |
* **Size matching**: All shards must be the same size |
| 353 |
* **Zero initialization**: Destination shards should start as zeros on the first call |
| 354 |
|
| 355 |
Progressive decoding is particularly useful for distributed storage systems, network reconstruction scenarios, and cases where input data arrives incrementally. |
| 356 |
|
| 357 |
While this can be used for regular reconstruction, typically that will be slightly faster and easier to use. |
| 358 |
|
| 359 |
# Streaming/Merging |
| 360 |
|
| 361 |
It might seem like a limitation that all data should be in memory, |
| 362 |
but an important property is that *as long as the number of data/parity shards are the same, |
| 363 |
you can merge/split data sets*, and they will remain valid as a separate set. |
| 364 |
|
| 365 |
```Go |
| 366 |
// Split the data set of 50000 elements into two of 25000 |
| 367 |
splitA := make([][]byte, 13) |
| 368 |
splitB := make([][]byte, 13) |
| 369 |
|
| 370 |
// Merge into a 100000 element set |
| 371 |
merged := make([][]byte, 13) |
| 372 |
|
| 373 |
for i := range data { |
| 374 |
splitA[i] = data[i][:25000] |
| 375 |
splitB[i] = data[i][25000:] |
| 376 |
|
| 377 |
// Concatenate it to itself |
| 378 |
merged[i] = append(make([]byte, 0, len(data[i])*2), data[i]...) |
| 379 |
merged[i] = append(merged[i], data[i]...) |
| 380 |
} |
| 381 |
|
| 382 |
// Each part should still verify as ok. |
| 383 |
ok, err := enc.Verify(splitA) |
| 384 |
if ok && err == nil { |
| 385 |
log.Println("splitA ok") |
| 386 |
} |
| 387 |
|
| 388 |
ok, err = enc.Verify(splitB) |
| 389 |
if ok && err == nil { |
| 390 |
log.Println("splitB ok") |
| 391 |
} |
| 392 |
|
| 393 |
ok, err = enc.Verify(merge) |
| 394 |
if ok && err == nil { |
| 395 |
log.Println("merge ok") |
| 396 |
} |
| 397 |
``` |
| 398 |
|
| 399 |
This means that if you have a data set that may not fit into memory, you can split processing into smaller blocks. |
| 400 |
For the best throughput, don't use too small blocks. |
| 401 |
|
| 402 |
This also means that you can divide big input up into smaller blocks, and do reconstruction on parts of your data. |
| 403 |
This doesn't give the same flexibility of a higher number of data shards, but it will be much more performant. |
| 404 |
|
| 405 |
# Streaming API |
| 406 |
|
| 407 |
There has been added support for a streaming API, to help perform fully streaming operations, |
| 408 |
which enables you to do the same operations, but on streams. |
| 409 |
To use the stream API, use [`NewStream`](https://godoc.org/github.com/klauspost/reedsolomon#NewStream) function |
| 410 |
to create the encoding/decoding interfaces. |
| 411 |
|
| 412 |
You can use [`WithConcurrentStreams`](https://godoc.org/github.com/klauspost/reedsolomon#WithConcurrentStreams) |
| 413 |
to ready an interface that reads/writes concurrently from the streams. |
| 414 |
|
| 415 |
You can specify the size of each operation using |
| 416 |
[`WithStreamBlockSize`](https://godoc.org/github.com/klauspost/reedsolomon#WithStreamBlockSize). |
| 417 |
This will set the size of each read/write operation. |
| 418 |
|
| 419 |
Input is delivered as `[]io.Reader`, output as `[]io.Writer`, and functionality corresponds to the in-memory API. |
| 420 |
Each stream must supply the same amount of data, similar to how each slice must be similar size with the in-memory API. |
| 421 |
If an error occurs in relation to a stream, |
| 422 |
a [`StreamReadError`](https://godoc.org/github.com/klauspost/reedsolomon#StreamReadError) |
| 423 |
or [`StreamWriteError`](https://godoc.org/github.com/klauspost/reedsolomon#StreamWriteError) |
| 424 |
will help you determine which stream was the offender. |
| 425 |
|
| 426 |
There is no buffering or timeouts/retry specified. If you want to add that, you need to add it to the Reader/Writer. |
| 427 |
|
| 428 |
For complete examples of a streaming encoder and decoder see the |
| 429 |
[examples folder](https://github.com/klauspost/reedsolomon/tree/master/examples). |
| 430 |
|
| 431 |
GF16 (more than 256 shards) is not supported by the streaming interface. |
| 432 |
|
| 433 |
# Advanced Options |
| 434 |
|
| 435 |
You can modify internal options which affects how jobs are split between and processed by goroutines. |
| 436 |
|
| 437 |
To create options, use the WithXXX functions. You can supply options to `New`, `NewStream`. |
| 438 |
If no Options are supplied, default options are used. |
| 439 |
|
| 440 |
Example of how to supply options: |
| 441 |
|
| 442 |
```Go |
| 443 |
enc, err := reedsolomon.New(10, 3, WithMaxGoroutines(25)) |
| 444 |
``` |
| 445 |
|
| 446 |
# Leopard Compatible GF16 |
| 447 |
|
| 448 |
When you encode more than 256 shards the library will switch to a [Leopard-RS](https://github.com/catid/leopard) implementation. |
| 449 |
|
| 450 |
This allows encoding up to 65536 shards (data+parity) with the following limitations, similar to leopard: |
| 451 |
|
| 452 |
* The original and recovery data must not exceed 65536 pieces. |
| 453 |
* The shard size *must* each be a multiple of 64 bytes. |
| 454 |
* Each buffer should have the same number of bytes. |
| 455 |
* Even the last shard must be rounded up to the block size. |
| 456 |
|
| 457 |
| | Regular | Leopard | |
| 458 |
|-----------------|---------|---------| |
| 459 |
| Encode | ✓ | ✓ | |
| 460 |
| EncodeIdx | ✓ | - | |
| 461 |
| Verify | ✓ | ✓ | |
| 462 |
| Reconstruct | ✓ | ✓ | |
| 463 |
| ReconstructData | ✓ | ✓ | |
| 464 |
| ReconstructSome | ✓ | ✓ (+) | |
| 465 |
| Update | ✓ | - | |
| 466 |
| Split | ✓ | ✓ | |
| 467 |
| Join | ✓ | ✓ | |
| 468 |
|
| 469 |
* (+) Same as calling `ReconstructData`. |
| 470 |
|
| 471 |
The Split/Join functions will help to split an input to the proper sizes. |
| 472 |
|
| 473 |
Speed can be expected to be `O(N*log(N))`, compared to the `O(N*N)`. |
| 474 |
Reconstruction matrix calculation is more time-consuming, |
| 475 |
so be sure to include that as part of any benchmark you run. |
| 476 |
|
| 477 |
For now SSSE3, AVX2 and AVX512 assembly are available on AMD64 platforms. |
| 478 |
|
| 479 |
Leopard mode currently always runs as a single goroutine, since multiple |
| 480 |
goroutines doesn't provide any worthwhile speedup. |
| 481 |
|
| 482 |
## Leopard GF8 |
| 483 |
|
| 484 |
It is possible to replace the default reed-solomon encoder with a leopard compatible one. |
| 485 |
This will typically be faster when dealing with more than 20-30 shards. |
| 486 |
Note that the limitations listed above also applies to this mode. |
| 487 |
See table below for speed with different number of shards. |
| 488 |
|
| 489 |
To enable Leopard GF8 mode use `WithLeopardGF(true)`. |
| 490 |
|
| 491 |
Benchmark Encoding and Reconstructing *1KB* shards with variable number of shards. |
| 492 |
All implementation use inversion cache when available. |
| 493 |
Speed is total shard size for each operation. Data shard throughput is speed/2. |
| 494 |
|
| 495 |
| Encoder | Shards | Encode | Recover All | Recover One | |
| 496 |
|--------------|-------------|-----------------|---------------|-----------------| |
| 497 |
| Cauchy | 4+4 | 59636.23 MB/s | 9494.17 MB/s | 21352.14 MB/s | |
| 498 |
| Cauchy | 8+8 | 89924.23 MB/s | 15361.86 MB/s | 51814.79 MB/s | |
| 499 |
| Cauchy | 16+16 | 11599.80 MB/s | 7232.95 MB/s | 51413.06 MB/s | |
| 500 |
| Cauchy | 32+32 | 6060.57 MB/s | 4731.84 MB/s | 62880.86 MB/s | |
| 501 |
| Cauchy | 64+64 | 3099.28 MB/s | 2746.79 MB/s | 72923.38 MB/s | |
| 502 |
| Cauchy | 128+128 | 1576.93 MB/s | 1472.20 MB/s | 82504.67 MB/s | |
| 503 |
| Leopard GF8 | 4+4 | 55138.94 MB/s | 7695.13 MB/s | 12775.18 MB/s | |
| 504 |
| Leopard GF8 | 8+8 | 52264.75 MB/s | 9425.42 MB/s | 13794.19 MB/s | |
| 505 |
| Leopard GF8 | 16+16 | 64844.90 MB/s | 7709.28 MB/s | 10713.69 MB/s | |
| 506 |
| Leopard GF8 | 32+32 | 39757.60 MB/s | 7671.30 MB/s | 9575.71 MB/s | |
| 507 |
| Leopard GF8 | 64+64 | 40143.15 MB/s | 6733.38 MB/s | 9210.23 MB/s | |
| 508 |
| Leopard GF8 | 128+128 | 30116.72 MB/s | 5937.93 MB/s | 8806.07 MB/s | |
| 509 |
| Leopard GF16 | 256+256 | 27153.59 MB/s | 1187.09 MB/s | 1269.02 MB/s | |
| 510 |
| Leopard GF16 | 512+512 | 22315.40 MB/s | 1888.36 MB/s | 2136.33 MB/s | |
| 511 |
| Leopard GF16 | 1024+1024 | 19596.76 MB/s | 2466.09 MB/s | 3068.25 MB/s | |
| 512 |
| Leopard GF16 | 2048+2048 | 15700.86 MB/s | 3003.09 MB/s | 4048.59 MB/s | |
| 513 |
| Leopard GF16 | 4096+4096 | 15134.19 MB/s | 3190.63 MB/s | 4524.92 MB/s | |
| 514 |
| Leopard GF16 | 8192+8192 | 13246.07 MB/s | 3067.94 MB/s | 4062.52 MB/s | |
| 515 |
| Leopard GF16 | 16384+16384 | 8403.47 MB/s | 1837.53 MB/s | 2744.81 MB/s | |
| 516 |
| Leopard GF16 | 32768+32768 | 5558.16 MB/s | 1317.53 MB/s | 1958.46 MB/s | |
| 517 |
|
| 518 |
"Traditional" encoding is faster until somewhere between 8 and 16 shards. |
| 519 |
Leopard provides fast encoding in all cases, but shows a significant overhead for reconstruction. |
| 520 |
|
| 521 |
Calculating the reconstruction matrix takes a significant amount of computation. |
| 522 |
With bigger shards that will be smaller. Arguably, fewer shards typically also means bigger shards. |
| 523 |
Due to the high shard count caching reconstruction matrices generally isn't feasible for Leopard. |
| 524 |
|
| 525 |
# Performance |
| 526 |
|
| 527 |
Performance depends mainly on the number of parity shards. |
| 528 |
In rough terms, doubling the number of parity shards will double the encoding time. |
| 529 |
|
| 530 |
Here are the throughput numbers with some different selections of data and parity shards. |
| 531 |
For reference each shard is 1MB random data, and 32 threads are used for encoding. |
| 532 |
|
| 533 |
| Data | Parity | Go MB/s | SSSE3 MB/s | AVX2 MB/s | GFNI MB/s | |
| 534 |
|------|--------|---------|------------|-----------|-----------| |
| 535 |
| 5 | 2 | 40,690 | 97,827 | 202,283 | 241,747 | |
| 536 |
| 8 | 8 | 14,244 | 62,412 | 128,343 | 271,680 | |
| 537 |
| 10 | 4 | 17,999 | 80,093 | 152,673 | 273,628 | |
| 538 |
| 50 | 20 | 3,520 | 24,184 | 44,734 | 47,706 | |
| 539 |
|
| 540 |
The throughput numbers here is the size of the encoded data and parity shards. |
| 541 |
|
| 542 |
If `runtime.GOMAXPROCS()` is set to a value higher than 1, |
| 543 |
the encoder will use multiple goroutines to perform the calculations in `Verify`, `Encode` and `Reconstruct`. |
| 544 |
|
| 545 |
|
| 546 |
Benchmarking `Reconstruct()` (=`all`) versus just calling `ReconstructData()` (=`data`) gives the following result: |
| 547 |
``` |
| 548 |
benchmark all MB/s data MB/s speedup |
| 549 |
BenchmarkReconstruct10x2x10000-32 122764.16 110366.81 0.90x |
| 550 |
BenchmarkReconstruct50x5x50000-32 116532.21 115657.88 0.99x |
| 551 |
BenchmarkReconstruct10x2x1M-32 269362.91 271178.89 1.01x |
| 552 |
BenchmarkReconstruct5x2x1M-32 236882.30 244961.33 1.03x |
| 553 |
BenchmarkReconstruct10x4x1M-32 298882.25 308258.61 1.03x |
| 554 |
BenchmarkReconstruct50x20x1M-32 75888.83 76367.32 1.01x |
| 555 |
BenchmarkReconstruct10x4x16M-32 62017.48 63305.76 1.02x |
| 556 |
``` |
| 557 |
|
| 558 |
The package will use [GFNI](https://en.wikipedia.org/wiki/AVX-512#GFNI) instructions combined with AVX512 when these are available. |
| 559 |
This further improves speed by up to 2x over AVX2 code paths. |
| 560 |
|
| 561 |
## ARM64 NEON |
| 562 |
|
| 563 |
By exploiting NEON instructions the performance for ARM has been accelerated. |
| 564 |
Below are the performance numbers for a single core on an EC2 m6g.16xlarge (Graviton2) instance (Amazon Linux 2): |
| 565 |
|
| 566 |
``` |
| 567 |
BenchmarkGalois128K-64 119562 10028 ns/op 13070.78 MB/s |
| 568 |
BenchmarkGalois1M-64 14380 83424 ns/op 12569.22 MB/s |
| 569 |
BenchmarkGaloisXor128K-64 96508 12432 ns/op 10543.29 MB/s |
| 570 |
BenchmarkGaloisXor1M-64 10000 100322 ns/op 10452.13 MB/s |
| 571 |
``` |
| 572 |
|
| 573 |
# Performance on ppc64le |
| 574 |
|
| 575 |
The performance for ppc64le has been accelerated. |
| 576 |
This gives roughly a 10x performance improvement on this architecture as can be seen below: |
| 577 |
|
| 578 |
``` |
| 579 |
benchmark old MB/s new MB/s speedup |
| 580 |
BenchmarkGalois128K-160 948.87 8878.85 9.36x |
| 581 |
BenchmarkGalois1M-160 968.85 9041.92 9.33x |
| 582 |
BenchmarkGaloisXor128K-160 862.02 7905.00 9.17x |
| 583 |
BenchmarkGaloisXor1M-160 784.60 6296.65 8.03x |
| 584 |
``` |
| 585 |
|
| 586 |
# Legal |
| 587 |
|
| 588 |
> None of section below is legal advice. Seek your own legal counsel. |
| 589 |
> As stated by the [LICENSE](LICENSE) the authors will not be held reliable for any use of this library. |
| 590 |
> Users are encouraged to independently verify they comply with all legal requirements. |
| 591 |
|
| 592 |
As can be seen in [recent news](https://www.datanami.com/2023/10/16/cloudera-hit-with-240-million-judgement-over-erasure-coding/) |
| 593 |
there has been lawsuits related to possible patents of aspects of erasure coding functionality. |
| 594 |
|
| 595 |
As a possible mitigation it is possible to use the tag `nopshufb` when compiling any code which includes this package. |
| 596 |
This will remove all inclusion and use of `PSHUFB` and equivalent on other platforms. |
| 597 |
|
| 598 |
This is done by adding `-tags=nopshufb` to `go build` and similar commands that produce binary output. |
| 599 |
|
| 600 |
The removed code may not be infringing and even after `-tags=nopshufb` there may still be infringing code left. |
| 601 |
|
| 602 |
# Links |
| 603 |
* [Backblaze Open Sources Reed-Solomon Erasure Coding Source Code](https://www.backblaze.com/blog/reed-solomon/). |
| 604 |
* [JavaReedSolomon](https://github.com/Backblaze/JavaReedSolomon). Compatible java library by Backblaze. |
| 605 |
* [ocaml-reed-solomon-erasure](https://gitlab.com/darrenldl/ocaml-reed-solomon-erasure). Compatible OCaml implementation. |
| 606 |
* [reedsolomon-c](https://github.com/jannson/reedsolomon-c). C version, compatible with output from this package. |
| 607 |
* [Reed-Solomon Erasure Coding in Haskell](https://github.com/NicolasT/reedsolomon). Haskell port of the package with similar performance. |
| 608 |
* [reed-solomon-erasure](https://github.com/darrenldl/reed-solomon-erasure). Compatible Rust implementation. |
| 609 |
* [go-erasure](https://github.com/somethingnew2-0/go-erasure). A similar library using cgo, slower in my tests. |
| 610 |
* [Screaming Fast Galois Field Arithmetic](https://www.snia.org/sites/default/files/files2/files2/SDC2013/presentations/NewThinking/EthanMiller_Screaming_Fast_Galois_Field%20Arithmetic_SIMD%20Instructions.pdf). Basis for SSE3 optimizations. |
| 611 |
* [Leopard-RS](https://github.com/catid/leopard) C library used as basis for GF16 implementation. |
| 612 |
* [reed-solomon-simd](https://github.com/AndersTrier/reed-solomon-simd) Leopard-RS Rust implementation. |
| 613 |
|
| 614 |
# License |
| 615 |
|
| 616 |
This code, as the original [JavaReedSolomon](https://github.com/Backblaze/JavaReedSolomon) is published under an MIT license. See LICENSE file for more information. |