-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify and reduce code in Write funcs Benchmarks: name old time/op new time/op delta ParseAllFrames-4 284µs ± 1% 286µs ± 2% +0.62% (p=0.628 n=6+7) ParseArtistAndTitle-4 39.2µs ±26% 40.3µs ±17% +2.69% (p=0.805 n=7+7) UTF8TextFrame-4 903ns ± 1% 1039ns ± 2% +15.13% (p=0.001 n=7+7) UTF16TextFrame-4 1.95µs ± 2% 2.07µs ± 1% +6.41% (p=0.001 n=6+7) ParseAndWrite-4 10.5ms ±17% 10.2ms ± 3% -3.06% (p=0.836 n=7+6) ReuseTag-4 2.77µs ± 1% 2.85µs ± 4% +2.70% (p=0.181 n=6+7) WriteBytesSize-4 27.3ns ± 2% 44.7ns ± 2% +63.64% (p=0.001 n=6+7) ParseSize-4 8.20ns ± 2% 8.19ns ± 2% ~ (p=1.000 n=7+7) name old alloc/op new alloc/op delta ParseAllFrames-4 525kB ± 0% 525kB ± 0% +0.01% (p=0.710 n=7+7) ParseArtistAndTitle-4 809B ± 2% 813B ± 1% +0.48% (p=0.829 n=7+7) UTF8TextFrame-4 418B ± 0% 418B ± 0% ~ (all equal) UTF16TextFrame-4 1.35kB ± 0% 1.35kB ± 0% ~ (all equal) ParseAndWrite-4 270kB ± 1% 271kB ± 0% +0.64% (p=0.008 n=7+6) ReuseTag-4 2.29kB ± 0% 2.29kB ± 0% ~ (all equal) WriteBytesSize-4 10.0B ± 0% 0.0B -100.00% (p=0.001 n=7+7) ParseSize-4 0.00B 0.00B ~ (all equal) name old allocs/op new allocs/op delta ParseAllFrames-4 97.0 ± 0% 97.6 ± 1% +0.59% (p=0.070 n=7+7) ParseArtistAndTitle-4 17.0 ±12% 17.3 ±10% +1.68% (p=0.837 n=7+7) UTF8TextFrame-4 5.00 ± 0% 5.00 ± 0% ~ (all equal) UTF16TextFrame-4 20.0 ± 0% 20.0 ± 0% ~ (all equal) ParseAndWrite-4 100 ± 1% 100 ± 1% ~ (p=1.000 n=7+7) ReuseTag-4 20.0 ± 0% 20.0 ± 0% ~ (all equal) WriteBytesSize-4 0.00 0.00 ~ (all equal) ParseSize-4 0.00 0.00 ~ (all equal)
- Loading branch information
Showing
16 changed files
with
181 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package id3v2 | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
) | ||
|
||
type bufWriter struct { | ||
err error | ||
w *bufio.Writer | ||
written int64 | ||
} | ||
|
||
func newBufWriter(w io.Writer) *bufWriter { | ||
return &bufWriter{w: bufio.NewWriter(w)} | ||
} | ||
|
||
func (bw *bufWriter) EncodeAndWriteText(src string, to Encoding) { | ||
if bw.err != nil { | ||
return | ||
} | ||
bw.err = encodeWriteText(bw, src, to) | ||
} | ||
|
||
func (bw *bufWriter) Flush() error { | ||
if bw.err != nil { | ||
return bw.err | ||
} | ||
return bw.w.Flush() | ||
} | ||
|
||
func (bw *bufWriter) Reset(w io.Writer) { | ||
bw.err = nil | ||
bw.written = 0 | ||
bw.w.Reset(w) | ||
} | ||
|
||
func (bw *bufWriter) WriteByte(c byte) { | ||
if bw.err != nil { | ||
return | ||
} | ||
bw.err = bw.w.WriteByte(c) | ||
if bw.err == nil { | ||
bw.written += 1 | ||
} | ||
} | ||
|
||
func (bw *bufWriter) WriteBytesSize(size uint) { | ||
if bw.err != nil { | ||
return | ||
} | ||
bw.err = writeBytesSize(bw, size) | ||
} | ||
|
||
func (bw *bufWriter) WriteString(s string) { | ||
if bw.err != nil { | ||
return | ||
} | ||
|
||
var n int | ||
n, bw.err = bw.w.WriteString(s) | ||
bw.written += int64(n) | ||
} | ||
|
||
func (bw *bufWriter) Write(p []byte) (n int, err error) { | ||
if bw.err != nil { | ||
return 0, bw.err | ||
} | ||
n, err = bw.w.Write(p) | ||
bw.written += int64(n) | ||
bw.err = err | ||
return n, err | ||
} | ||
|
||
func (bw *bufWriter) Written() int64 { | ||
return bw.written | ||
} | ||
|
||
func useBufWriter(w io.Writer, f func(*bufWriter)) (int64, error) { | ||
var writtenBefore int64 | ||
bw, ok := w.(*bufWriter) | ||
if ok { | ||
writtenBefore = bw.Written() | ||
} else { | ||
bw = getBufWriter(w) | ||
defer putBufWriter(bw) | ||
} | ||
|
||
f(bw) | ||
|
||
return bw.Written() - writtenBefore, bw.Flush() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.