Skip to content

Commit

Permalink
Use interface to get bytes (#309)
Browse files Browse the repository at this point in the history
Use interface to detect bytes.Byffer.
Move generator.
  • Loading branch information
klauspost authored Jan 12, 2021
1 parent b45090e commit dedf03c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 270 deletions.
4 changes: 2 additions & 2 deletions flate/gen_inflate.go → flate/_gen/gen_inflate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build generate

//go:generate go run $GOFILE && gofmt -w inflate_gen.go
//go:generate go run $GOFILE && gofmt -w ../inflate_gen.go

package main

Expand All @@ -10,7 +10,7 @@ import (
)

func main() {
f, err := os.Create("inflate_gen.go")
f, err := os.Create("../inflate_gen.go")
if err != nil {
panic(err)
}
Expand Down
265 changes: 0 additions & 265 deletions flate/gen.go

This file was deleted.

7 changes: 7 additions & 0 deletions s2/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ func (w *Writer) ReadFrom(r io.Reader) (n int64, err error) {
return 0, err
}
}
if br, ok := r.(byter); ok {
buf := br.Bytes()
if err := w.EncodeBuffer(buf); err != nil {
return 0, err
}
return int64(len(buf)), w.Flush()
}
for {
inbuf := w.buffers.Get().([]byte)[:w.blockSize+obufHeaderLen]
n2, err := io.ReadFull(r, inbuf[obufHeaderLen:])
Expand Down
7 changes: 7 additions & 0 deletions s2/s2.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
package s2

import (
"bytes"
"hash/crc32"
)

Expand Down Expand Up @@ -127,3 +128,9 @@ func literalExtraSize(n int64) int64 {
return 5
}
}

type byter interface {
Bytes() []byte
}

var _ byter = &bytes.Buffer{}
7 changes: 4 additions & 3 deletions zstd/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package zstd

import (
"bytes"
"errors"
"io"
"sync"
Expand Down Expand Up @@ -179,11 +178,13 @@ func (d *Decoder) Reset(r io.Reader) error {
}

// If bytes buffer and < 1MB, do sync decoding anyway.
if bb, ok := r.(*bytes.Buffer); ok && bb.Len() < 1<<20 {
if bb, ok := r.(byter); ok && bb.Len() < 1<<20 {
var bb2 byter
bb2 = bb
if debug {
println("*bytes.Buffer detected, doing sync decode, len:", bb.Len())
}
b := bb.Bytes()
b := bb2.Bytes()
var dst []byte
if cap(d.current.b) > 0 {
dst = d.current.b
Expand Down
8 changes: 8 additions & 0 deletions zstd/zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package zstd

import (
"bytes"
"errors"
"log"
"math"
Expand Down Expand Up @@ -146,3 +147,10 @@ func load64(b []byte, i int) uint64 {
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
}

type byter interface {
Bytes() []byte
Len() int
}

var _ byter = &bytes.Buffer{}

0 comments on commit dedf03c

Please sign in to comment.