From b44d2100db9b169a854af075ffe94bc280b81acc Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Wed, 25 Mar 2015 19:09:34 -0700 Subject: [PATCH] cache decode matrix tables and matrix --- pkg/encoding/erasure/erasure_decode.go | 27 ++++++++++++++------------ pkg/encoding/erasure/erasure_encode.go | 10 +++++----- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pkg/encoding/erasure/erasure_decode.go b/pkg/encoding/erasure/erasure_decode.go index e3fbba596..158e3f0c8 100644 --- a/pkg/encoding/erasure/erasure_decode.go +++ b/pkg/encoding/erasure/erasure_decode.go @@ -37,9 +37,6 @@ import ( // blocks. // "dataLen" is the length of original source data func (e *Encoder) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData []byte, err error) { - var decodeMatrix *C.uint8_t - var decodeTbls *C.uint8_t - var decodeIndex *C.uint32_t var source, target **C.uint8_t k := int(e.params.K) @@ -82,9 +79,19 @@ func (e *Encoder) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData [ } } - // Initialzie decoder - C.minio_init_decoder(missingEncodedBlocksC, C.int(k), C.int(n), C.int(missingEncodedBlocksCount-1), - e.encodeMatrix, &decodeMatrix, &decodeTbls, &decodeIndex) + // If not already initialized, recompute and cache + if e.decodeMatrix == nil || e.decodeTbls == nil || e.decodeIndex == nil { + var decodeMatrix, decodeTbls *C.uint8_t + var decodeIndex *C.uint32_t + + C.minio_init_decoder(missingEncodedBlocksC, C.int(k), C.int(n), C.int(missingEncodedBlocksCount-1), + e.encodeMatrix, &decodeMatrix, &decodeTbls, &decodeIndex) + + // cache this for future needs + e.decodeMatrix = decodeMatrix + e.decodeTbls = decodeTbls + e.decodeIndex = decodeIndex + } // Make a slice of pointers to encoded blocks. Necessary to bridge to the C world. pointers := make([]*byte, n) @@ -94,13 +101,13 @@ func (e *Encoder) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData [ // Get pointers to source "data" and target "parity" blocks from the output byte array. ret := C.minio_get_source_target(C.int(missingEncodedBlocksCount-1), C.int(k), C.int(m), missingEncodedBlocksC, - decodeIndex, (**C.uint8_t)(unsafe.Pointer(&pointers[0])), &source, &target) + e.decodeIndex, (**C.uint8_t)(unsafe.Pointer(&pointers[0])), &source, &target) if int(ret) == -1 { return nil, errors.New("Unable to decode data") } // Decode data - C.ec_encode_data(C.int(encodedBlockLen), C.int(k), C.int(missingEncodedBlocksCount-1), decodeTbls, + C.ec_encode_data(C.int(encodedBlockLen), C.int(k), C.int(missingEncodedBlocksCount-1), e.decodeTbls, source, target) // Allocate buffer to output buffer @@ -109,9 +116,5 @@ func (e *Encoder) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData [ decodedData = append(decodedData, encodedDataBlocks[i]...) } - // TODO cache this if necessary - e.decodeMatrix = decodeMatrix - e.decodeTbls = decodeTbls - return decodedData[:dataLen], nil } diff --git a/pkg/encoding/erasure/erasure_encode.go b/pkg/encoding/erasure/erasure_encode.go index 98f9b63ac..f6664ce5e 100644 --- a/pkg/encoding/erasure/erasure_encode.go +++ b/pkg/encoding/erasure/erasure_encode.go @@ -56,11 +56,10 @@ type EncoderParams struct { // Encoder is an object used to encode and decode data. type Encoder struct { - params *EncoderParams - encodeMatrix, - encodeTbls, - decodeMatrix, - decodeTbls *C.uint8_t + params *EncoderParams + encodeMatrix, encodeTbls *C.uint8_t + decodeMatrix, decodeTbls *C.uint8_t + decodeIndex *C.uint32_t } // ParseEncoderParams creates an EncoderParams object. @@ -114,6 +113,7 @@ func NewEncoder(ep *EncoderParams) *Encoder { encodeTbls: encodeTbls, decodeMatrix: nil, decodeTbls: nil, + decodeIndex: nil, } }