Skip to content

Commit

Permalink
exif: Use the stream based API
Browse files Browse the repository at this point in the history
Also, do not return an error when input format isn't supported. Just return `nil` exif.

Using the stream API makes it a little faster:

```
name           old time/op    new time/op    delta
DecodeExif-16    1.94ms ± 1%    1.81ms ± 1%   -6.74%  (p=0.029 n=4+4)

name           old alloc/op   new alloc/op   delta
DecodeExif-16    1.51MB ± 0%    1.13MB ± 0%  -25.32%  (p=0.029 n=4+4)

name           old allocs/op  new allocs/op  delta
DecodeExif-16     12.9k ± 0%     12.9k ± 0%   -0.15%  (p=0.029 n=4+4)
```

It's still much slower than what we had:

```
name           old time/op    new time/op    delta
DecodeExif-16     108µs ± 1%    1857µs ± 2%  +1624.52%  (p=0.029 n=4+4)

name           old alloc/op   new alloc/op   delta
DecodeExif-16     184kB ± 0%    1130kB ± 0%   +513.72%  (p=0.029 n=4+4)

name           old allocs/op  new allocs/op  delta
DecodeExif-16     1.20k ± 0%    12.91k ± 0%   +972.82%  (p=0.029 n=4+4)
```

See #8586
  • Loading branch information
bep committed Jul 16, 2021
1 parent 0bb01ba commit 2bd9e91
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
13 changes: 1 addition & 12 deletions resources/images/exif/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
package exif

import (
"fmt"
"io"
"io/ioutil"
"math/big"
"regexp"
"strings"
Expand All @@ -26,7 +24,6 @@ import (

_exif "github.com/dsoprea/go-exif/v3"
exifcommon "github.com/dsoprea/go-exif/v3/common"
png "github.com/dsoprea/go-png-image-structure"
)

const (
Expand Down Expand Up @@ -110,17 +107,9 @@ func NewDecoder(options ...func(*Decoder) error) (*Decoder, error) {
}

func (d *Decoder) Decode(r io.Reader) (*Exif, error) {
rawBytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
rawExif, err := _exif.SearchAndExtractExif(rawBytes)
rawExif, err := _exif.SearchAndExtractExifWithReader(r)

if err != nil {
parsed := png.NewPngMediaParser()
if parsed.LooksLikeFormat(rawBytes) {
return nil, fmt.Errorf("type not supported")
}
// No Exif data
return nil, nil
}
Expand Down
5 changes: 3 additions & 2 deletions resources/images/exif/exif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ func TestExifPNG(t *testing.T) {

d, err := NewDecoder()
c.Assert(err, qt.IsNil)
_, err = d.Decode(f)
c.Assert(err, qt.Not(qt.IsNil))
x, err := d.Decode(f)
c.Assert(err, qt.IsNil)
c.Assert(x, qt.IsNil)
}

func TestIssue8079(t *testing.T) {
Expand Down

0 comments on commit 2bd9e91

Please sign in to comment.