• Jed Denlea's avatar
    image/gif: write fewer, bigger blocks · 8b220d8e
    Jed Denlea authored
    The indexed bitmap of a frame is encoded into a GIF by first LZW
    compression, and then packaged by a simple block mechanism.  Each block
    of up-to-256 bytes starts with one byte, which indicates the size of the
    block (0x01-0xff). The sequence of blocks is terminated by a 0x00.
    
    While the format supports it, there is no good reason why any particular
    image should be anything but a sequence of 255-byte blocks with one last
    block less than 255-bytes.
    
    The old blockWriter implementation would not buffer between Write()s,
    meaning if the lzw Writer needs to flush more than one chunk of data via
    a Write, multiple short blocks might exist in the middle of a stream.
    
    Separate but related, the old implementation also forces lzw.NewWriter
    to allocate a bufio.Writer because the blockWriter is not an
    io.ByteWriter itself.  But, even though it doesn't effectively buffer
    data between Writes, it does make extra copies of sub-blocks during the
    course of writing them to the GIF's writer.
    
    Now, the blockWriter shall continue to use the encoder's [256]byte buf,
    but use it to effectively buffer a series of WriteByte calls from the
    lzw Writer.  Once a WriteByte fills the buffer, the staged block is
    Write()n to the underlying GIF writer.  After the lzw Writer is Closed,
    the blockWriter should also be closed, which will flush any remaining
    block along with the block terminator.
    
    BenchmarkEncode indicates slight improvements:
    
    name      old time/op    new time/op    delta
    Encode-8    7.71ms ± 0%    7.38ms ± 0%   -4.27%  (p=0.008 n=5+5)
    
    name      old speed      new speed      delta
    Encode-8   159MB/s ± 0%   167MB/s ± 0%   +4.46%  (p=0.008 n=5+5)
    
    name      old alloc/op   new alloc/op   delta
    Encode-8    84.1kB ± 0%    80.0kB ± 0%   -4.94%  (p=0.008 n=5+5)
    
    name      old allocs/op  new allocs/op  delta
    Encode-8      9.00 ± 0%      7.00 ± 0%  -22.22%  (p=0.008 n=5+5)
    
    Change-Id: I9eb9367d41d7c3d4d7f0adc9b720fc24fb50006a
    Reviewed-on: https://go-review.googlesource.com/68351Reviewed-by: 's avatarNigel Tao <nigeltao@golang.org>
    8b220d8e
Name
Last commit
Last update
..
archive Loading commit data...
bufio Loading commit data...
builtin Loading commit data...
bytes Loading commit data...
cmd Loading commit data...
compress Loading commit data...
container Loading commit data...
context Loading commit data...
crypto Loading commit data...
database/sql Loading commit data...
debug Loading commit data...
encoding Loading commit data...
errors Loading commit data...
expvar Loading commit data...
flag Loading commit data...
fmt Loading commit data...
go Loading commit data...
hash Loading commit data...
html Loading commit data...
image Loading commit data...
index/suffixarray Loading commit data...
internal Loading commit data...
io Loading commit data...
log Loading commit data...
math Loading commit data...
mime Loading commit data...
net Loading commit data...
os Loading commit data...
path Loading commit data...
plugin Loading commit data...
reflect Loading commit data...
regexp Loading commit data...
runtime Loading commit data...
sort Loading commit data...
strconv Loading commit data...
strings Loading commit data...
sync Loading commit data...
syscall Loading commit data...
testing Loading commit data...
text Loading commit data...
time Loading commit data...
unicode Loading commit data...
unsafe Loading commit data...
vendor/golang_org/x Loading commit data...
Make.dist Loading commit data...
all.bash Loading commit data...
all.bat Loading commit data...
all.rc Loading commit data...
androidtest.bash Loading commit data...
bootstrap.bash Loading commit data...
buildall.bash Loading commit data...
clean.bash Loading commit data...
clean.bat Loading commit data...
clean.rc Loading commit data...
cmp.bash Loading commit data...
iostest.bash Loading commit data...
make.bash Loading commit data...
make.bat Loading commit data...
make.rc Loading commit data...
naclmake.bash Loading commit data...
nacltest.bash Loading commit data...
race.bash Loading commit data...
race.bat Loading commit data...
run.bash Loading commit data...
run.bat Loading commit data...
run.rc Loading commit data...