Skip to content

Commit

Permalink
Ignore needless frames of opts.ParseFrames
Browse files Browse the repository at this point in the history
If the frame can be only one in tag,
don't take into consideration more frames with id.

Huge performance improvement, when opts.ParseFrames is set:
name                   old time/op    new time/op    delta
ParseArtistAndTitle-4    64.4µs ± 1%    19.5µs ±24%  -69.71%  (p=0.000 n=8+10)

name                   old alloc/op   new alloc/op   delta
ParseArtistAndTitle-4      844B ± 0%      812B ± 1%   -3.81%  (p=0.000 n=10+9)

name                   old allocs/op  new allocs/op  delta
ParseArtistAndTitle-4      25.0 ± 0%      17.2 ±13%  -31.20%  (p=0.000 n=10+10)
  • Loading branch information
n10v committed Nov 6, 2017
1 parent c5ebecd commit 2378552
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,36 @@ func (tag *Tag) parseFrames(opts Options) error {
bodyRd := getLimitedReader(tag.reader, bodySize)
defer putLimitedReader(bodyRd)

// If user set opts.ParseFrames, take it into consideration.
// If user set opts.ParseFrames and frame ID is not there,
// skip this frame.
if len(parseIDs) > 0 && !parseIDs[id] {
if err := skipReaderBuf(bodyRd, buf); err != nil {
return err
}
continue
}

// Parse frame body.
frame, err := parseFrameBody(id, bodyRd)
if err != nil && err != io.EOF {
return err
}

// Add frame to tag.
tag.AddFrame(id, frame)

if len(parseIDs) > 0 {
// If the frame can be only one in tag,
// don't take into consideration more frames with id.
if !mustFrameBeInSequence(id) {
delete(parseIDs, id)

// And if it was last ID in parseIDs, we don't need to parse
// other frames, so break the parsing.
if len(parseIDs) == 0 {
break
}
}
}

if err == io.EOF {
break
}
Expand Down

0 comments on commit 2378552

Please sign in to comment.