From 2bd9e91588c59e64bcb3642adc07cee75ed50812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 16 Jul 2021 14:16:23 +0200 Subject: [PATCH] exif: Use the stream based API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- resources/images/exif/exif.go | 13 +------------ resources/images/exif/exif_test.go | 5 +++-- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/resources/images/exif/exif.go b/resources/images/exif/exif.go index 929c25bd378..0985ec5fec7 100644 --- a/resources/images/exif/exif.go +++ b/resources/images/exif/exif.go @@ -14,9 +14,7 @@ package exif import ( - "fmt" "io" - "io/ioutil" "math/big" "regexp" "strings" @@ -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 ( @@ -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 } diff --git a/resources/images/exif/exif_test.go b/resources/images/exif/exif_test.go index 555d28c3045..9f649e9f8f1 100644 --- a/resources/images/exif/exif_test.go +++ b/resources/images/exif/exif_test.go @@ -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) {