Skip to content

proposal: compress: add Compress and Decompress convenience functions #16504

Closed
@dsnet

Description

@dsnet

Filing on behalf of an internal user, who requests that we add convenience wrappers of Compress and Decompress:

func Decompress(b []byte) ([]byte, error) {
    rd, err := NewReader(bytes.NewReader(b))
    if err != nil {
        return nil, err
    }
    d, err := ioutil.ReadAll(rd)
    if cerr := rd.Close(); err == nil {
        err = cerr
    }
    return d, err
}

What are people's thoughts?

Activity

nhooyr

nhooyr commented on Jul 26, 2016

@nhooyr
Contributor

If we do this, we should use a sync.Pool for the readers.

bradfitz

bradfitz commented on Jul 26, 2016

@bradfitz
Contributor

Maybe things have changed, but I would expect pushback for sync.Pool use.

odeke-em

odeke-em commented on Jul 27, 2016

@odeke-em
Member

Just like net/http has:

  • Get.
  • Post.
  • Head.
  • PostForm

etc
that underlyingly use the default client's methods perhaps each compress package could have its own Compress and Decompress convenience functions, instead of a default of zlib?

  • compress/zlib.[Compress, Decompress]
  • compress/lzw.[Compress, Decompress]
  • compress/gzip.[Compress, Decompress]
  • compress/bzip2.[Compress, Decompress]

Apologies if am preaching to the choir but it wasn't straight forward for me from your example if the convenience functions would be in the top level compress or in each package.

dsnet

dsnet commented on Jul 27, 2016

@dsnet
MemberAuthor

@odeke-em, the suggestion in the original post is for exactly what you listed out. Thanks for the asking to clarify.

added this to the Unplanned milestone on Jul 28, 2016
ulikunitz

ulikunitz commented on Jul 29, 2016

@ulikunitz
Contributor

If convenience functions are required for compression packages I suggest the alternatives:

func Compress(w io.Writer, data []byte) error
func Decompress(r io.Reader) (data []byte, err error)

These functions would have more use cases and are very easy to use with a bytes.Buffer or a bytes.Reader.

var buf bytes.Buffer
err := Compress(&buf, data)
data, err := Decompress(bytes.NewReader(compressedData))

BTW Decompression doesn't need the Close call. It doesn't do anything useful. Depending on the compression package Close for a reader either sets the reader to closed or returns the last returned error if its not io.EOF. IMHO returning a io.ReadCloser for a decompressor is a design error. The bzip2 package does the right thing and returns only an io.Reader.

bradfitz

bradfitz commented on Sep 12, 2016

@bradfitz
Contributor

Yeah, I don't think helpers which slurp everything to memory are helpful. You're compressing it in the first place because it's large, so pulling it all into memory when it's almost certainly large is not a good idea.

I think the existing Reader- & Writer-based interfaces are the right answer.

Maybe we just need more documentation?

bradfitz

bradfitz commented on Sep 12, 2016

@bradfitz
Contributor

Sorry, going to close this one. We already have @ulikunitz's func Decompress in the form of ioutil.ReadAll anyway.

locked and limited conversation to collaborators on Sep 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bradfitz@odeke-em@dsnet@ulikunitz@gopherbot

        Issue actions

          proposal: compress: add Compress and Decompress convenience functions · Issue #16504 · golang/go