Skip to content

Commit

Permalink
fse, huff0, zstd: Remove always-nil error returns (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
greatroar committed Sep 9, 2023
1 parent 11e91a1 commit 0e8837c
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 39 deletions.
3 changes: 1 addition & 2 deletions fse/bitwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,11 @@ func (b *bitWriter) flushAlign() {

// close will write the alignment bit and write the final byte(s)
// to the output.
func (b *bitWriter) close() error {
func (b *bitWriter) close() {
// End mark
b.addBits16Clean(1, 1)
// flush until next byte.
b.flushAlign()
return nil
}

// reset and continue writing by appending to out.
Expand Down
3 changes: 2 additions & 1 deletion fse/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ func (s *Scratch) compress(src []byte) error {
c2.flush(s.actualTableLog)
c1.flush(s.actualTableLog)

return s.bw.close()
s.bw.close()
return nil
}

// writeCount will write the normalized histogram count to header.
Expand Down
3 changes: 1 addition & 2 deletions huff0/bitwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ func (b *bitWriter) flushAlign() {

// close will write the alignment bit and write the final byte(s)
// to the output.
func (b *bitWriter) close() error {
func (b *bitWriter) close() {
// End mark
b.addBits16Clean(1, 1)
// flush until next byte.
b.flushAlign()
return nil
}
20 changes: 6 additions & 14 deletions huff0/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ func EstimateSizes(in []byte, s *Scratch) (tableSz, dataSz, reuseSz int, err err
}

func (s *Scratch) compress1X(src []byte) ([]byte, error) {
return s.compress1xDo(s.Out, src)
return s.compress1xDo(s.Out, src), nil
}

func (s *Scratch) compress1xDo(dst, src []byte) ([]byte, error) {
func (s *Scratch) compress1xDo(dst, src []byte) []byte {
var bw = bitWriter{out: dst}

// N is length divisible by 4.
Expand Down Expand Up @@ -260,8 +260,8 @@ func (s *Scratch) compress1xDo(dst, src []byte) ([]byte, error) {
bw.encTwoSymbols(cTable, tmp[1], tmp[0])
}
}
err := bw.close()
return bw.out, err
bw.close()
return bw.out
}

var sixZeros [6]byte
Expand All @@ -283,12 +283,8 @@ func (s *Scratch) compress4X(src []byte) ([]byte, error) {
}
src = src[len(toDo):]

var err error
idx := len(s.Out)
s.Out, err = s.compress1xDo(s.Out, toDo)
if err != nil {
return nil, err
}
s.Out = s.compress1xDo(s.Out, toDo)
if len(s.Out)-idx > math.MaxUint16 {
// We cannot store the size in the jump table
return nil, ErrIncompressible
Expand All @@ -315,7 +311,6 @@ func (s *Scratch) compress4Xp(src []byte) ([]byte, error) {

segmentSize := (len(src) + 3) / 4
var wg sync.WaitGroup
var errs [4]error
wg.Add(4)
for i := 0; i < 4; i++ {
toDo := src
Expand All @@ -326,15 +321,12 @@ func (s *Scratch) compress4Xp(src []byte) ([]byte, error) {

// Separate goroutine for each block.
go func(i int) {
s.tmpOut[i], errs[i] = s.compress1xDo(s.tmpOut[i][:0], toDo)
s.tmpOut[i] = s.compress1xDo(s.tmpOut[i][:0], toDo)
wg.Done()
}(i)
}
wg.Wait()
for i := 0; i < 4; i++ {
if errs[i] != nil {
return nil, errs[i]
}
o := s.tmpOut[i]
if len(o) > math.MaxUint16 {
// We cannot store the size in the jump table
Expand Down
3 changes: 1 addition & 2 deletions zstd/bitwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ func (b *bitWriter) flushAlign() {

// close will write the alignment bit and write the final byte(s)
// to the output.
func (b *bitWriter) close() error {
func (b *bitWriter) close() {
// End mark
b.addBits16Clean(1, 1)
// flush until next byte.
b.flushAlign()
return nil
}

// reset and continue writing by appending to out.
Expand Down
5 changes: 1 addition & 4 deletions zstd/blockenc.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,10 +791,7 @@ func (b *blockEnc) encode(org []byte, raw, rawAllLits bool) error {
ml.flush(mlEnc.actualTableLog)
of.flush(ofEnc.actualTableLog)
ll.flush(llEnc.actualTableLog)
err = wr.close()
if err != nil {
return err
}
wr.close()
b.output = wr.out

// Maybe even add a bigger margin.
Expand Down
13 changes: 4 additions & 9 deletions zstd/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ func (e *Encoder) nextBlock(final bool) error {
DictID: e.o.dict.ID(),
}

dst, err := fh.appendTo(tmp[:0])
if err != nil {
return err
}
dst := fh.appendTo(tmp[:0])
s.headerWritten = true
s.wWg.Wait()
var n2 int
Expand Down Expand Up @@ -483,7 +480,7 @@ func (e *Encoder) EncodeAll(src, dst []byte) []byte {
Checksum: false,
DictID: 0,
}
dst, _ = fh.appendTo(dst)
dst = fh.appendTo(dst)

// Write raw block as last one only.
var blk blockHeader
Expand Down Expand Up @@ -518,10 +515,7 @@ func (e *Encoder) EncodeAll(src, dst []byte) []byte {
if len(dst) == 0 && cap(dst) == 0 && len(src) < 1<<20 && !e.o.lowMem {
dst = make([]byte, 0, len(src))
}
dst, err := fh.appendTo(dst)
if err != nil {
panic(err)
}
dst = fh.appendTo(dst)

// If we can do everything in one block, prefer that.
if len(src) <= e.o.blockSize {
Expand Down Expand Up @@ -581,6 +575,7 @@ func (e *Encoder) EncodeAll(src, dst []byte) []byte {
// Add padding with content from crypto/rand.Reader
if e.o.pad > 0 {
add := calcSkippableFrame(int64(len(dst)), int64(e.o.pad))
var err error
dst, err = skippableFrame(dst, add, rand.Reader)
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions zstd/frameenc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type frameHeader struct {

const maxHeaderSize = 14

func (f frameHeader) appendTo(dst []byte) ([]byte, error) {
func (f frameHeader) appendTo(dst []byte) []byte {
dst = append(dst, frameMagic...)
var fhd uint8
if f.Checksum {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (f frameHeader) appendTo(dst []byte) ([]byte, error) {
default:
panic("invalid fcs")
}
return dst, nil
return dst
}

const skippableFrameHeader = 4 + 4
Expand Down
5 changes: 2 additions & 3 deletions zstd/snappy.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ func (r *SnappyConverter) Convert(in io.Reader, w io.Writer) (int64, error) {
var written int64
var readHeader bool
{
var header []byte
var n int
header, r.err = frameHeader{WindowSize: snappyMaxBlockSize}.appendTo(r.buf[:0])
header := frameHeader{WindowSize: snappyMaxBlockSize}.appendTo(r.buf[:0])

var n int
n, r.err = w.Write(header)
if r.err != nil {
return written, r.err
Expand Down

0 comments on commit 0e8837c

Please sign in to comment.