Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ReconstructSome for leopard 8+16 #272

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions leopard.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,17 @@ func (r *leopardFF16) Split(data []byte) ([][]byte, error) {

func (r *leopardFF16) ReconstructSome(shards [][]byte, required []bool) error {
if len(required) == r.totalShards {
return r.reconstruct(shards, true)
return r.reconstruct(shards, true, required)
}
return r.reconstruct(shards, false)
return r.reconstruct(shards, false, required)
}

func (r *leopardFF16) Reconstruct(shards [][]byte) error {
return r.reconstruct(shards, true)
return r.reconstruct(shards, true, nil)
}

func (r *leopardFF16) ReconstructData(shards [][]byte) error {
return r.reconstruct(shards, false)
return r.reconstruct(shards, false, nil)
}

func (r *leopardFF16) Verify(shards [][]byte) (bool, error) {
Expand Down Expand Up @@ -375,8 +375,8 @@ func (r *leopardFF16) Verify(shards [][]byte) (bool, error) {
return true, nil
}

func (r *leopardFF16) reconstruct(shards [][]byte, recoverAll bool) error {
if len(shards) != r.totalShards {
func (r *leopardFF16) reconstruct(shards [][]byte, recoverAll bool, required []bool) error {
if len(shards) != r.totalShards || required != nil && len(required) < r.dataShards {
return ErrTooFewShards
}

Expand All @@ -388,15 +388,19 @@ func (r *leopardFF16) reconstruct(shards [][]byte, recoverAll bool) error {
// nothing to do.
numberPresent := 0
dataPresent := 0
missingRequired := 0
for i := 0; i < r.totalShards; i++ {
if len(shards[i]) != 0 {
numberPresent++
if i < r.dataShards {
dataPresent++
}
} else if required != nil && required[i] {
missingRequired++
}
}
if numberPresent == r.totalShards || !recoverAll && dataPresent == r.dataShards {
if numberPresent == r.totalShards || !recoverAll && dataPresent == r.dataShards ||
required != nil && missingRequired == 0 {
// Cool. All of the shards have data. We don't
// need to do anything.
return nil
Expand Down Expand Up @@ -426,21 +430,18 @@ func (r *leopardFF16) reconstruct(shards [][]byte, recoverAll bool) error {
for i := 0; i < r.parityShards; i++ {
if len(shards[i+r.dataShards]) == 0 {
errLocs[i] = 1
if LEO_ERROR_BITFIELD_OPT && recoverAll {
if LEO_ERROR_BITFIELD_OPT && recoverAll && (required == nil || required[i+r.dataShards]) {
errorBits.set(i)
}
}
}
for i := r.parityShards; i < m; i++ {
errLocs[i] = 1
if LEO_ERROR_BITFIELD_OPT && recoverAll {
errorBits.set(i)
}
}
for i := 0; i < r.dataShards; i++ {
if len(shards[i]) == 0 {
errLocs[i+m] = 1
if LEO_ERROR_BITFIELD_OPT {
if LEO_ERROR_BITFIELD_OPT && (required == nil || required[i]) {
errorBits.set(i + m)
}
}
Expand Down Expand Up @@ -541,7 +542,7 @@ func (r *leopardFF16) reconstruct(shards [][]byte, recoverAll bool) error {
end = r.totalShards
}
for i := 0; i < end; i++ {
if len(shards[i]) != 0 {
if len(shards[i]) != 0 || required != nil && !required[i] {
continue
}
if cap(shards[i]) >= shardSize {
Expand Down
100 changes: 45 additions & 55 deletions leopard8.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ type leopardFF8 struct {
totalShards int // Total number of shards. Calculated, and should not be modified.

workPool sync.Pool
inversion map[[inversion8Bytes]byte]leopardGF8cache
inversion map[errorBitfieldLevel8][order8]ffe8
inversionMu sync.Mutex

o options
}

const inversion8Bytes = 256 / 8

type leopardGF8cache struct {
errorLocs [256]ffe8
bits *errorBitfield8
}

// newFF8 is like New, but for the 8-bit "leopard" implementation.
func newFF8(dataShards, parityShards int, opt options) (*leopardFF8, error) {
initConstants8()
Expand All @@ -59,7 +54,7 @@ func newFF8(dataShards, parityShards int, opt options) (*leopardFF8, error) {
if opt.inversionCache && (r.totalShards <= 64 || opt.forcedInversionCache) {
// Inversion cache is relatively ineffective for big shard counts and takes up potentially lots of memory
// r.totalShards is not covering the space, but an estimate.
r.inversion = make(map[[inversion8Bytes]byte]leopardGF8cache, r.totalShards)
r.inversion = make(map[errorBitfieldLevel8][order8]ffe8, r.totalShards)
}
return r, nil
}
Expand Down Expand Up @@ -370,17 +365,17 @@ func (r *leopardFF8) Split(data []byte) ([][]byte, error) {

func (r *leopardFF8) ReconstructSome(shards [][]byte, required []bool) error {
if len(required) == r.totalShards {
return r.reconstruct(shards, true)
return r.reconstruct(shards, true, required)
}
return r.reconstruct(shards, false)
return r.reconstruct(shards, false, required)
}

func (r *leopardFF8) Reconstruct(shards [][]byte) error {
return r.reconstruct(shards, true)
return r.reconstruct(shards, true, nil)
}

func (r *leopardFF8) ReconstructData(shards [][]byte) error {
return r.reconstruct(shards, false)
return r.reconstruct(shards, false, nil)
}

func (r *leopardFF8) Verify(shards [][]byte) (bool, error) {
Expand Down Expand Up @@ -411,8 +406,8 @@ func (r *leopardFF8) Verify(shards [][]byte) (bool, error) {
return true, nil
}

func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool) error {
if len(shards) != r.totalShards {
func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool, required []bool) error {
if len(shards) != r.totalShards || required != nil && len(required) < r.dataShards {
return ErrTooFewShards
}

Expand All @@ -424,15 +419,19 @@ func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool) error {
// nothing to do.
numberPresent := 0
dataPresent := 0
missingRequired := 0
for i := 0; i < r.totalShards; i++ {
if len(shards[i]) != 0 {
numberPresent++
if i < r.dataShards {
dataPresent++
}
} else if required != nil && required[i] {
missingRequired++
}
}
if numberPresent == r.totalShards || !recoverAll && dataPresent == r.dataShards {
if numberPresent == r.totalShards || !recoverAll && dataPresent == r.dataShards ||
required != nil && missingRequired == 0 {
// Cool. All of the shards have data. We don't
// need to do anything.
return nil
Expand All @@ -459,55 +458,38 @@ func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool) error {

// Fill in error locations.
var errorBits errorBitfield8
var errLocs [order8]ffe8
var errLocBits errorBitfieldLevel8
for i := 0; i < r.parityShards; i++ {
if len(shards[i+r.dataShards]) == 0 {
errLocs[i] = 1
if LEO_ERROR_BITFIELD_OPT && recoverAll {
errLocBits.set(i)
if LEO_ERROR_BITFIELD_OPT && recoverAll && (required == nil || required[i+r.dataShards]) {
errorBits.set(i)
}
}
}
for i := r.parityShards; i < m; i++ {
errLocs[i] = 1
if LEO_ERROR_BITFIELD_OPT && recoverAll {
errorBits.set(i)
}
errLocBits.set(i)
}
for i := 0; i < r.dataShards; i++ {
if len(shards[i]) == 0 {
errLocs[i+m] = 1
if LEO_ERROR_BITFIELD_OPT {
errLocBits.set(i + m)
if LEO_ERROR_BITFIELD_OPT && (required == nil || required[i]) {
errorBits.set(i + m)
}
}
}

var gotInversion bool
var errLocs [order8]ffe8
if LEO_ERROR_BITFIELD_OPT && r.inversion != nil {
cacheID := errorBits.cacheID()
r.inversionMu.Lock()
if inv, ok := r.inversion[cacheID]; ok {
r.inversionMu.Unlock()
errLocs = inv.errorLocs
if inv.bits != nil && useBits {
errorBits = *inv.bits
useBits = true
} else {
useBits = false
}
gotInversion = true
} else {
r.inversionMu.Unlock()
}
errLocs, gotInversion = r.inversion[errLocBits]
r.inversionMu.Unlock()
}

if !gotInversion {
// No inversion...
if LEO_ERROR_BITFIELD_OPT && useBits {
errorBits.prepare()
}

errLocs = errLocBits.ErrLocs()
// Evaluate error locator polynomial8
fwht8(&errLocs, order8, m+r.dataShards)

Expand All @@ -518,21 +500,16 @@ func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool) error {
fwht8(&errLocs, order8, order8)

if r.inversion != nil {
c := leopardGF8cache{
errorLocs: errLocs,
}
if useBits {
// Heap alloc
var x errorBitfield8
x = errorBits
c.bits = &x
}
r.inversionMu.Lock()
r.inversion[errorBits.cacheID()] = c
r.inversion[errLocBits] = errLocs
r.inversionMu.Unlock()
}
}

if LEO_ERROR_BITFIELD_OPT && useBits {
errorBits.prepare()
}

var work [][]byte
if w, ok := r.workPool.Get().([][]byte); ok {
work = w
Expand Down Expand Up @@ -566,7 +543,7 @@ func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool) error {

// Add output
for i, sh := range shards {
if !recoverAll && i >= r.dataShards {
if !recoverAll && i >= r.dataShards || required != nil && !required[i] {
continue
}
if len(sh) == 0 {
Expand Down Expand Up @@ -657,7 +634,7 @@ func (r *leopardFF8) reconstruct(shards [][]byte, recoverAll bool) error {
}
// Restore
for i := 0; i < end; i++ {
if len(sh[i]) != 0 {
if len(sh[i]) != 0 || required != nil && !required[i] {
continue
}

Expand Down Expand Up @@ -1162,11 +1139,11 @@ const kWords8 = order8 / 64
// errorBitfield contains progressive errors to help indicate which
// shards need reconstruction.
type errorBitfield8 struct {
Words [7][kWords8]uint64
Words [7]errorBitfieldLevel8
}

func (e *errorBitfield8) set(i int) {
e.Words[0][(i/64)&3] |= uint64(1) << (i & 63)
e.Words[0].set(i)
}

func (e *errorBitfield8) cacheID() [inversion8Bytes]byte {
Expand Down Expand Up @@ -1267,3 +1244,16 @@ func (e *errorBitfield8) fftDIT8(work [][]byte, mtrunc, m int, skewLUT []ffe8, o
}
}
}

type errorBitfieldLevel8 [kWords8]uint64

func (e *errorBitfieldLevel8) set(i int) {
e[(i/64)&3] |= uint64(1) << (i & 63)
}

func (e *errorBitfieldLevel8) ErrLocs() (errLocs [order8]ffe8) {
for i := range errLocs {
errLocs[i] = ffe8((e[i/64] >> (i & 63)) & 1)
}
return
}
Loading
Loading